停止else语句中的文本循环


Stop looping of text in else statement

我已经在while循环中添加了一个if语句,以向特定用户显示特定数据,但是我想添加一个echo"No Notifications";的ELSE语句。只有在我要求循环在SQL查询中打印结果的次数中,它才会一遍又一遍地打印此文本。我怎样才能停止它,让它只在<div class="notif_ui">div中显示else语句呢?

while($notificationchant=mysqli_fetch_assoc($chant)){
       if($_SESSION['id']==$notificationchant['notification_targetuser']){
            ?>
            <div class="notif_ui">
            <div class="notif_text">
    <div id="notif_actual_text-<? echo $notificationchant['notification_id'] ?>" class="notif_actual_text">
            <?
             echo "<img border='"1'" src='"userimages/cropped".$notificationchant['notification_triggeredby'].".jpg?photo_time=" . time()."'" onerror='this.src='"userimages/no_profile_img.jpeg'"' width='"40'" height='"40'"><a href='".$notificationchant['notification_throughurl']."'>".$notificationchant['notification_content']." </a><br />";
            echo "".Agotime($notificationchant['notification_time']).""; 
            ?>
            </div>
            </div>
            </div>
          <? }}?>

创建一个新变量并设置为0

$didEcho = 0;
检查else 中的变量是否为0
else if ($didEcho == 0){
    echo 'No Notification';
    $didEcho = 1;    
}

然后在else中把它改成1