I’m trying to display the following code only once while the rest of the code loops threw. How should my code look like and where should I put it. As of right now the bottom code will not display but if I put it in the loop it will display multiple times (not what I want).
//Display only once.
if (!empty($row['category']) && !empty($row['url'])){
echo '<h2>List of Links</h2>';
}
Here is the full code.
// Query member data from the database and ready it for display
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT users.*, categories.*, users_categories.* FROM users_categories INNER JOIN users ON users_categories.user_id = users.user_id INNER JOIN categories ON users_categories.category_id = categories.id WHERE users_categories.user_id=3");
if (!$dbc) {
// There was an error...do something about it here...
print mysqli_error($mysqli);
}
//Display only once.
if (!empty($row['category']) && !empty($row['url'])){
echo '<h2>List of Links</h2>';
}
$ctr = 0;
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['category']) && !empty($row['url'])) {
if ($ctr%3 == 0) {
echo '<div>';
}
$ctr ++;
echo '<p"><a href="' . $row['url'] . '" title="' . $row['category'] . ' Category Link">' . $row['category'] . '</a></p>';
if ($ctr%3 == 0) {
echo '</div>';
}
}
}
if ($ctr%3 != 0) {
echo '</div>';
}
Perhaps you’re looking for:
By replacing the code above with your current if statement, the header will be displayed when rows are returned in the query, and not when the query returns nothing.
Thus, the full code would be: