I am having a bit of an issue if there is no categories I need to echo no categories if there is I need it to echo There are categories. it shows if there is categories, but don't show if there is not.
<tr>
<?php
$db = dbconnect();
$stmt = $db->prepare("SELECT * FROM discussion_categories");
$stmt->execute();
$result = $stmt->get_result();
while (($row = mysqli_fetch_assoc($result)) == true) {
$CategoryID = $row['CategoryID'];
$Name = $row['Name'];
$Description = $row['Description'];
$Photo = $row['Photo'];
if(!empty($CategoryID['CategoryID'])){
echo "<td>No categories</td>";
} else {
echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>";
echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>";
echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>";
}
}
?>
</tr>
That's an easy to solve problem.
If there is no category the while loop gets never executed because there are now rows in your db result.
Try and check the row amount first:
if (mysqli_num_rows($result) == 0) {
//there are no categories
echo('no categories');
} else {
//there are categories
echo('there are categories');
//in case you want to loop through your categories now
while (($row = mysqli_fetch_assoc($result)) == true) {
$CategoryID = $row['CategoryID'];
echo($CategoryID);
//your code
}
}
This should help you!
If there are no categories, then this condition:
while (($row = mysqli_fetch_assoc($result)) == true)
Which is a long-winded way of writing this:
while ( $row = mysqli_fetch_assoc($result) )
Will never be true, so you'll enter the loop zero times - there will never be a "truthy" value for $row
.
If we write out your code as pseudo-code, we get:
inspect each result
if the result has a non-empty CategoryID, echo "No categories"
if the result has an empty CategoryID, echo "There are categories"
end of loop
The two if checks are the wrong way around, but more importantly they are inside the loop.
What you probably meant was something like this:
set found_results flag to false
inspect each result
if the result has a non-empty CategoryID, set found_results flag to true
perform other operations on the result, or use "break;" to end the loop early
end of loop
if found_results flag is true, echo "There are categories"
if found_results flag is false, echo "No categories"
I'll leave it to you to translate that back into PHP. :)
Of course, if you really only need to know if there are results or not, you can write this much neater by:
mysqli_num_rows
SELECT COUNT(*)
in your SQL instead of SELECT *
, possibly also with a WHERE CategoryId IS NOT NULL
clauseIf I understand it correctly, you are should use empty() not !empty(). The code run like:
if(!empty($CategoryID['CategoryID'])){ //if cat is not empty it says no cat
//notice the ! in front of empty tag
echo "<td>No categories</td>";
} else {
echo "<td>There are categories</td>";
}
according to your code, if the categories are empty, it will display there are categories.
By calling the While loop you are saying that, if the query return at least one category in the result.
The loop is thus confused when there is no category. Try replacing the empty() with count of CategoryID greater than zero and see what happens.
if > 0
exist
else
does not exist
if you have no results, then it wont even go in the while loop, so your conditional statement is redundant (which is why you get no output).
Youre better off, as mentioned in some of the comments to check for the results before you try to do anything with them.
if($result->num_rows > 0) {
// you now know there are results
while ($row = mysqli_fetch_assoc($result)) {
// do your business in here as you would have, but you dont need to worry about nothing to process
}
} else {
// do something in here to send back a null result, or whatever you like
}
while (($row = mysqli_fetch_assoc($result)) == true)
According to the above part of the code. It will only go inside the while loop if there is any data returned or fetched from the database else if no data has been fetched it will not enter the loop.
If there is no categories then it will go out of the loop and so this echo "<td>No categories</td>";
will never be shown.
Also since you have put an echo saying 'No Categories', I assume that it means you want to echo an output if there is no category. But your if condition is wrong since, if u want to check if some variable is empty you need to do as below
if(empty($CategoryID['CategoryID'])){
echo "<td>No categories</td>";
} else {
echo "<td colspan='4'><img class='profile-photo' src='" . ROOT_URI . "/uploads/" . $Photo . "'></td>";
echo "<td colspan='4'><a href='discussions.php?view={$CategoryID}'>{$Name}</a></td>";
echo "<td><a href='managecategories.php?delete={$CategoryID}'>delete</a></td>";
}
where if(empty())
will be true if it is empty and if(!empty())
will be true if it is not empty.