dongzhan7909 2013-09-10 17:13
浏览 28
已采纳

正确使用三元算子

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>';

}
  • 写回答

2条回答 默认 最新

  • doucan9079 2013-09-10 17:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存