Issue
I have this php code:
if(mysqli_num_rows($query) > 0) {
while($row = mysqli_fetch_array($query)) {
if ($row['status'] == "Opened") {
$test = "1";
} else {
$test = "0";
}
}
}
echo $test;
The problem occurs when there are multiple entries in the database and one of those entries does not have status = "Opened" therefore $test returns "0". I am trying to accomplish that if ANY of the entries have status = "Opened", $test will return "1".
Solution
It might be better to directly select the data you need from the database instead of filtering it out with PHP.
SELECT id, message, etc FROM tickets WHERE status = 'Opened'
// if it fetches data, you have open tickets and have directly access to the data of the open tickets.
SELECT count(*) AS total FROM tickets WHERE status = 'Opened'
// the result of this is a number of the total amount of opened tickets, perhaps better for this specific use.
But on the subject on how to fix your loop you can do the following:
$open = false;
while($row = mysqli_fetch_array($query)) {
if ($row['status'] == "Opened") {
$open = true;
break;
}
}
As this will quit the while loop, setting $open for futher use:
if($open){
// do your stuff;
}
Answered By - Xorifelse Answer Checked By - Dawn Plyler (Python.Easybugfix Volunteer)
Post a Comment
Post a Comment