codeispoetry
codeispoetry

Reputation: 373

if() else function doesn't work in a loop

I have this table

cityreportcomment :
-----------------
reportID (fK)
cityID (fK)
comment

EDIT: if city has no comment, there is no row in the table: a row (for a city) exists only if that city has a comment.

for every report, I print 13 cities (that are in another table). I would like to check if city has a comment, then echo comment, if not, echo 'different'; but my code doesn't work.

for ($i = 0; $i < 13; ++$i) {
    
    $stmt = $conn->prepare("SELECT * FROM cityreportcomment WHERE reportID=? AND cityID=?");
    if (!$stmt) {
        die(printf("Error: %s.\n", mysqli_stmt_error($stmt)));
    } else if (!$stmt->bind_param('ii', $reportID, $selectcityID_array_unique[$i])) {
        die(printf("Error: %s.\n", mysqli_stmt_error($stmt)));
    } else if (!$stmt->execute()) {
        die(printf("Error execute from ereportcomment table: %s.\n", mysqli_stmt_error($stmt)));
    } else {
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            if (isset($row['cityID']) && $row['cityID'] == $selectcityID_array_unique[$i]) {
                $selectcomment2[] = 'same' . $row['cityID'] . (isset($row['comment']) ? $row['comment'] : "");
            } elseif (!isset($row['cityID']) || $row['cityID'] != $selectcityID_array_unique[$i]) {
                $selectcomment2[] = 'different';
            }
        }
    }
    /* end else */
    
    $stmt->close();
    
    echo $i . ' ' . $selectcomment2[$i] . '<br>';
    
} //end for

PROBLEM:

if $i=1 has comment, $i=2 no comment, $i=3 has comment, $i=4 no comment, my echo results is

1 comment1

2 comment3

3

4

5

.....

It should be


1 comment1

2 different

3 comment3

4 different

5 different

.....

Upvotes: 2

Views: 103

Answers (3)

Dave Chen
Dave Chen

Reputation: 10975

If the comment is empty, it won't be !isset.

This code should do what you want:

I would like to check if city has a comment, then echo comment, if not, echo 'different'

$stmt = $conn->prepare("SELECT * FROM cityreportcomment WHERE reportID=? AND cityID=?");
$key = 0;
$stmt->bind_param('ii', $reportID, $key);

for ($i = 0; $i < 13; ++$i) {
    $key = $selectcityID_array_unique[$i];
    $stmt->execute();

    $result = $stmt->get_result();

    echo '<br>Results for cityID = ' . $key . '<br><br>';

    while ($row = $result->fetch_assoc())
        if (empty($row['comment']))
            echo 'different<br>';
        else
            echo $row['comment'] . '<br>';
}

Upvotes: 1

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41810

You have this SQL:

"SELECT * FROM cityreportcomment WHERE reportID=? AND cityID=?"

You are binding $selectcityID_array_unique[$i] to the cityID placeholder, so every record returned will have a cityID equal to $selectcityID_array_unique[$i].

Then you are checking this:

if (isset($row['cityID']) && $row['cityID'] == $selectcityID_array_unique[$i]) {

Naturally, it will always be true. If no records are returned from the query, you will not see different, because the while loop will execute zero times.

You can fix it like this:

// remove the if from the while loop, as it really has no effect
while ($row = $result->fetch_assoc()) {
    // you don't need the isset checks; if $row is set, then each value in it will be set
    $selectcomment2[] = 'same' . $row['cityID'] . $row['comment']);
}
// if there's nothing in the comments after the loop, then put 'different' in it
if (!$selectcomment2) {
    $selectcomment2[] = 'different';
}

Upvotes: 1

peixotorms
peixotorms

Reputation: 1283

Use this instead:

while ($row = $result->fetch_assoc()) {
    if (!empty($row['cityID']) && $row['cityID'] == $selectcityID_array_unique[$i]) {
        $selectcomment2[] = 'same' . $row['cityID'] . (!empty($row['comment']) ? $row['comment'] : "");
    } elseif (empty($row['cityID']) || $row['cityID'] != $selectcityID_array_unique[$i]) {
                    $selectcomment2[] = 'different';
    } else {
    continue; # do nothing
    }
}

Your if/else doesn't work because you are checking if $row['cityID'] "is set", in other words, if it exists or not... However, because you are using it on a mysql loop where you are returning those fields, they will always be set (exist). I suspect that what you want to check is, if $row['cityID'] is empty or not, which in that case you could use !empty() instead.

https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

Upvotes: 2

Related Questions