Reputation: 1517
Please could someone explain to me how could I adapt my code to make it so that if a record / value doesnt exist in the mysql table it will echo a piece of text? Thank you.
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\"></a>";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
?>
Upvotes: 0
Views: 1087
Reputation: 18685
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews = mysql_fetch_array($reviews_set))>=1)
{
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\"></a>";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
}
} else {
echo 'No reviews available';
}
?>
Upvotes: 0
Reputation: 2021
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews_set) > 0) {
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<a href=\"profile.php?id={$reviews['from_user_id']}\"><img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\"></a>";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
<?
}
}else{
echo "No Data";
}
?>
I hope this helps
Upvotes: 0
Reputation: 10236
If you want to check if table has rows, use following:
$num_rows = mysql_num_rows($reviews_set);
$num_rows
will contain number of rows.
Upvotes: 0
Reputation: 14025
Check your variables like that :
<?php (isset($reviews['display_name']) ? $reviews['display_name'] : "entry doesn't exists"; ?>
Upvotes: 1
Reputation: 2967
Use the ternary operator '?:'
Sample:
$you_var ?: 'you_text_if_not_exists'
Upvotes: 1