Reputation: 41
I'm trying to add 'NEW' to unread messages in the inbox. I don't think I can use an if statement within an echo and I have tried and failed to understand the proper use of ternary operators. I would appreciate any help. Thanks a lot.
If read_yet = 'no' I want to add this code as another echo statement:
echo '<td>NEW</td>' ;
Here is my current code for the inbox.
$sql = mysqli_query($con, "select * from messages
WHERE deleted = 'no' AND to_user = '" . $_GET['username'] . "'
ORDER BY id DESC");
while ($row = mysqli_fetch_array($sql)){
echo '<tr>';
echo '<td><a href="profile.php?username=' ;
echo $row['from_user'] ;
echo '">';
echo '<img id="usersimage" src="images/profileimg/thumbnail_'.$row['from_user'];
echo '.jpg"</a></td>' ;
echo '<td><a href="users.php?username=' ;
echo $row['from_user'] ;
echo '">';
echo $row['from_user'];
echo "</a></td><td><a href='messageid.php?id=".$row['id']."'><font color='#b2ccdb'>" .$row['subject'] ;
echo '</a></font></td>';
echo '</tr>';
}
Upvotes: 1
Views: 493
Reputation: 360772
A ternary like this:
$foo = ($bar) ? 'true result' : 'false result';
is functionally equivalent to:
if ($bar) {
$foo = 'true result';
} else {
$foo = 'false result';
}
Upvotes: 2
Reputation: 73031
If you are adding another column, I would not suggest using the ternary operator and instead simply using if
:
if ($row['read_yet' == 'no') {
echo '<td>NEW</td>';
}
However, if you always output this column and need to change the display, you could use the ternary operator.
echo '<td>' . ($row['read_yet' == 'no' ? 'NEW' : 'READ') . '</td>';
Note: The ternary can create line noise. As such, I prefer a temporary variable, helper function, or traditional if
.
Upvotes: 2