繁体   English   中英

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语句。 如果是这种情况,则只需在循环过程中切换变量的值即可。 您需要将要转储的值存储在数组中,也可以仅将其转储到循环中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM