Reputation: 189
Explanation & Problem
Okay we have the following columns in our table,
[username] - [message] - [status] - [id]
In some specify PHP page, we use a while loop to post ALL of the rows that are stored in the database.
Okay, but now I want to check if there are any rows in the that table, if not, echo an error.
It's okay, simple as this:
$check = $fetch->rowCount();
and then
if ($check == 0) { }
But now, I want to check if column 'Status' is 0.
So if there are no rows with columns 'status' with value 0, it will echo a message. If there are no rows at all, it will echo a message.
If there are rows with column 'status' with value 0, it will echo all rows.
But there are some problems atleast for me, doing so.
Problem
I can't do all of the IF statements checks inside a loop, because a loop can't run if there are no rows.
But okay, we can do the check if there are rows outside of the loop, but how would I check if there are rows + they are not with status column holding value higher than '0'?
I got it to work somehow, but not like I wanted..
If there are rows with column 'status' with value 0 + rows with column status with value higher than 0 at same time, it will echo the message + show the rows, which shouldn't work like this!
This is the code I've tried to build:
$check = $fetch->rowCount();
if ($check == 0) {
echo '<br /> EMPTY';
}
while($row = $fetch->fetch( PDO::FETCH_ASSOC )) {
if ($check > 0 && $row['status'] == 0) {
echo
'<div class="background">'.$row['email'].' <span style="margin-left: 10px;"><font color="#ff0000">UNANSWERED</font></span><span id="right"><a href="index.php?recover='.$row['recover_id'].'">Read More</a></span></div><br />
';
} else {
echo 'There are no rows!';
}
}
Thank you for your help!
Upvotes: 2
Views: 1111
Reputation: 46900
But now, I want to check if column 'Status' is 0.
So if there are no rows with columns 'status' with value 0, it will echo a message. If there are no rows at all, it will echo a message.
If there are rows with column 'status' with value 0, it will echo all rows.
Why dont you send that condition to database in your WHERE
clause
SELECT username,message,status,id FROM yourTABLE where status=0
So when there are such rows, it will display them. If there are no such rows then you will get your rowcount as 0 and then you can echo the message accordingly
Tip
Fetching all rows from a table and then bringing those to PHP only to discard most of them is just like going to a fruit shop buying all the available fruits, bringing them home and then saying Uh no i don't need Oranges,Apples,Apricot etc etc etc. You have to send conditions to your Database, and not bring all data without any condition and then put conditions in PHP.
Upvotes: 1
Reputation: 8431
try this:
$check = $fetch->rowCount();
if ($check == 0) {
echo '<br /> EMPTY';
}
else
{
while($row = $fetch->fetch( PDO::FETCH_ASSOC )) {
if ($check > 0 && $row['status'] == 0) {
echo "status 0";
// or echo "<br/> BLANK!";
}
else
{
echo '<div class="background">'.$row['email'].' <span style="margin-left: 10px;"><font color="#ff0000">UNANSWERED</font></span><span id="right"><a href="index.php?recover='.$row['recover_id'].'">Read More</a></span></div><br />';
}
}
}
Upvotes: 1