0

I'm trying to display a different chat bubble color per user in one conversation.

This works:

<?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 } ;
?>

But the previous statement is not quite what is needed. User needs to keep same bubble across conversation. The query below is correct but does not work. What I am doing wrong?

<?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 } ;
?>
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Sebastian Farham
  • 815
  • 2
  • 13
  • 27
  • from your code I think your second condition (`elseif`) will never execute – B. Desai May 05 '17 at 05:47
  • @B.Desai: I know but why? – Sebastian Farham May 05 '17 at 05:48
  • 2
    because in your query condyion is `c.fromUserId='$loginid_session' OR c.toUserId='$loginid_session'` so from this your first condition always become true. Will you more soecific about you exoected output – B. Desai May 05 '17 at 05:50
  • @B.Desai: Makes sense. I'm trying to attribute one bubble per user. Say for example the first bubble is blue and second bubble is green. I need all messages from the user with the $loginid_session to always display messages in blue while the other user will display in the green. Thing is fromUserId and toUserId changes based on who sends or receive the message. – Sebastian Farham May 05 '17 at 05:59
  • Why are you adding while($chatmsg= mysqli_fetch_array($chatmsgresult)){;?> semi colon here and – smarttechy May 05 '17 at 06:33
  • Remove the `;` after the while start brace. You should bind variable insted of concatenating Check this url http://stackoverflow.com/questions/16612251/how-to-bind-multiple-parameters-to-mysqli-query#answer-16612474 – Aman Rawat May 05 '17 at 06:36
  • Provide an example of your desired output and what you are getting from your code. – Aman Rawat May 05 '17 at 06:43

2 Answers2

0

At first, you need to change SQL query:

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

Becuase if you use after and condition c.fromUserId='$loginid_session' OR c.toUserId='$loginid_session' then all query come for that user, not all user then by this query result it will be never executed second statement.

Could you please try without elseif condition:

<?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 } ;?>
rowmoin
  • 698
  • 2
  • 8
  • 17
  • But if I have 200 users chatting at the same time, I need to know who is chatting with whom. – Sebastian Farham May 05 '17 at 06:03
  • I know but I have try to find out why your second else if statement not working and I found that if you use that query then your second else if statement does not execute ever because you just fetch query result for only that user, not all user. If you ask me what will be logic if you want to know who is chatting with whom then I saw you fetch * data from that query then use query result user_id, user_name then I think you can find out. – rowmoin May 05 '17 at 06:12
0

Turns out I had the solution all along... For strange reasons the first time I ran the code below it gave a different result but then after trying multiple times it worked.

<?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 } ;
?>
Sebastian Farham
  • 815
  • 2
  • 13
  • 27