2012-07-07 59 views
0

我想为我的社区网站建立通报制度,我试图用一个while循环来获取数据,当曾经在一个条件if语句内得到满足while循环,它应该显示/打印数据到页面。出于某种原因,它只显示一个结果,不知道为什么。if和else if while循环中的语句不能正常工作

我的数据库的结构:

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



?> 

任何帮助赞赏

回答

3

$DisplayNotification实际显示?这当然不在你的循环体内。

通过循环每次分配$DisplayNotification一个新的价值,当然,它取代了旧的价值。当你完成时,不管发生了什么,最近的变化是唯一剩下的变化。

最有可能的,我怀疑你的意思是像做

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

.=将继续在整个循环添加新的文本,以相同的变量。

或者你可以使用一个数组,在这种情况下,你会怎么做

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

然后,你可以显示在结束所有项目,但是你会喜欢。

+0

谢谢,这么多的固定它现在使用的$ DisplayNotification =“用户添加你为好友\ n”;和它的工作就像一个魅力笑 – user1509217 2012-07-07 20:51:37

+0

我很高兴,这是有帮助的。如果此答案解决了您的问题,请点击左侧的复选标记。 – VoteyDisciple 2012-07-08 02:52:50

0

它看起来像你运行陈述充分之前实际上倾销变量$DisplayNotification。如果是这种情况,您只需在循环中切换变量值。您可能需要将要转储的值存储在数组中或将其转储到循环中。 。