如果Else Statement显示错误的div

时间:2017-05-05 05:37:51

标签: php if-statement

我试图在一次对话中为每个用户显示不同的聊天气泡颜色。

这有效:

<?php
    $chatmsgQ="SELECT * FROM ve_chat c 
               WHERE c.isActive='1' AND c.fromUserId='$loginid_session' 
               OR c.toUserId='$loginid_session'";
    $chatmsgresult=  mysqli_query($db,$chatmsgQ);
    while($chatmsg=  mysqli_fetch_array($chatmsgresult)){;?>

       <?php if($chatmsg['fromUserId']==$loginid_session):?>
       <!-- user one -->
    <p class='bubble pull-left'><?=$chatmsg['message'];?></p>

    <?php elseif($chatmsg['fromUserId']!=$loginid_session):?>
      <!-- user two-->
    <p class='bubbleother pull-right'><?=$chatmsg['message'];?></p>
    <?php endif;?>
    <?php } ;
?>

但之前的陈述并不完全是需要的。用户需要在对话中保持相同的泡沫。下面的查询是正确的,但不起作用。我做错了什么?

<?php
    $chatmsgQ="SELECT * FROM ve_chat c 
     WHERE c.isActive='1' AND c.fromUserId='$loginid_session' 
     OR c.toUserId='$loginid_session'";
    $chatmsgresult=  mysqli_query($db,$chatmsgQ);
    while($chatmsg=  mysqli_fetch_array($chatmsgresult)){;?>

       <?php if($chatmsg['fromUserId']==$loginid_session OR $chatmsg['toUserId']==$loginid_session):?>
       <!-- user one -->
   <p class='bubble pull-left'><?=$chatmsg['message'];?></p>

   <?php elseif($chatmsg['fromUserId']!=$loginid_session OR $chatmsg['toUserId']!=$loginid_session):?>
    <!-- user two-->
   <p class='bubbleother pull-right'><?=$chatmsg['message'];?></p>
   <?php endif;?>
    <?php } ;
?>

2 个答案:

答案 0 :(得分:0)

首先,您需要更改SQL查询:

     $chatmsgQ="SELECT * FROM ve_chat c 
     WHERE c.isActive='1'";

如果您使用after和条件c.fromUserId =&#39; $ loginid_session&#39;      或者c.toUserId =&#39; $ loginid_session&#39;然后所有查询都来自该用户,而不是所有用户然后通过此查询结果它将永远不会执行第二个语句。

请你在没有条件的情况下尝试:

<?php
$chatmsgQ="SELECT * FROM ve_chat c 
 WHERE c.isActive='1' AND c.fromUserId='$loginid_session' 
 OR c.toUserId='$loginid_session'";
$chatmsgresult=  mysqli_query($db,$chatmsgQ);
while($chatmsg=  mysqli_fetch_array($chatmsgresult)){;?>

      <?php if($chatmsg['fromUserId']==$loginid_session OR $chatmsg['toUserId']==$loginid_session):?>
       <!-- user one -->
<p class='bubble pull-left'><?=$chatmsg['message'];?></p>

 <?php else:?>
   <!-- user two-->
<p class='bubbleother pull-right'><?=$chatmsg['message'];?></p>
   <?php endif;?>
    <?php } ;?>

答案 1 :(得分:0)

原来我一直都有解决方案......出于奇怪的原因,我第一次运行下面的代码会产生不同的结果但是经过多次尝试后它才有效。

<?php
    $chatmsgQ="SELECT * FROM ve_chat c 
               WHERE c.isActive='1' AND c.fromUserId='$loginid_session' 
               OR c.toUserId='$loginid_session'";
    $chatmsgresult=  mysqli_query($db,$chatmsgQ);
    while($chatmsg=  mysqli_fetch_array($chatmsgresult)){;?>

       <?php if($chatmsg['fromUserId']==$loginid_session):?>
       <!-- user one -->
    <p class='bubble pull-left'><?=$chatmsg['message'];?></p>

    <?php elseif($chatmsg['fromUserId']!=$loginid_session):?>
      <!-- user two-->
    <p class='bubbleother pull-right'><?=$chatmsg['message'];?></p>
    <?php endif;?>
    <?php } ;
?>