WHILE 循环中的 IF 和 ELSE IF 语句无法正常工作


IF and ELSE IF statements within a WHILE loop not working properly

我正在尝试为我的社区网站创建通知系统,正在尝试使用while循环来获取数据,当在while循环中满足if语句中的条件时,它应该显示/打印数据到页面。出于某种原因,它只显示一个结果,不知道为什么。

我的数据库结构:

CREATE TABLE IF NOT EXISTS `notifications` (
  `notification_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `to_id` int(11) NOT NULL,
  `notification_identifier` enum('1','2','3','4','5','6') NOT NULL,
  `notify_id` int(11) NOT NULL,
  `opened` enum('0','1') NOT NULL,
  `timestamp` datetime NOT NULL,
  PRIMARY KEY (`notification_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

notification_identifier告诉我它是什么类型的通知(例如个人资料评论、状态更新、喜欢),notify_id告诉我需要检查的每个特定表的 ID。

我的代码:

<?
$DisplayNotification ="";
$unread = "0";
$mynotify = mysql_query("SELECT * FROM notifications WHERE to_id='$logOptions_id' AND opened='$unread'") or die (mysql_error());
$notify_Count = mysql_num_rows($mynotify);
if($notify_Count>0){
        while($row = mysql_fetch_array($mynotify)){
        $notification_id = $row["notification_id"];
        $memb_id = $row["user_id"];
        $identifier = $row["notification_identifier"];
        $notify_id =$row["notify_id"];
        $timestamp = $row["timestamp"];
        $convertedTime = ($myObject -> convert_datetime($timestamp));
        $when_notify = ($myObject -> makeAgo($convertedTime));

        if($identifier == 1){// condition 1
            $DisplayNotification ='user added you as a friend';
        }else if ($identifier == 2) {//condition 2
            $DisplayNotification ='user commented on your post';
        }

        }

 }else{// End of $notify
    $DisplayNotification ='You have no new notifications.';
 }

?>

任何帮助表示赞赏

$DisplayNotification实际显示在哪里?它肯定不在你的循环体内。

每次通过循环,您$DisplayNotification分配一个新值,该值当然会替换旧值。当你完成时,无论发生什么,最新的更改是唯一剩下的更改。

很可能我怀疑你的意思是做类似的事情

$DisplayNotification .= "User added you as a friend'n";

.=将在整个循环中继续向同一变量添加新文本。

或者,也许您可以使用数组,在这种情况下,您会这样做

$DisplayNotifications[] = "User added you as a friend";

然后,您可以根据需要在末尾显示所有项目。

看起来您在实际转储变量$DisplayNotification之前完全运行了 while 语句。如果是这种情况,您只需在循环期间切换变量的值。您要么需要将要转储的值存储在数组中,要么只需将它们转储到循环中。