0

I am making a list in messages.php which displays all the active conversations the logged in user ($username) has.

What is classed as an "active conversation"? (What should be listed in the li..)

  • Assume I am logged in as Alice. Alice sends a message to Fred, this is an active conversation where the $username (Alice) is sending a message to $user (Fred).
  • Assume I am logged in as Alice. Fred sends a message to Alice. This is also an active conversation.
  • Summary: Any messages to $username (logged in user) and any messages from $usernameshould be listed, only ONCE.

Current behavior:

Currently, with the code displayed below, an li is being generated for every message I send (every row in the private_messages table relating to the $username).

For example:

  • Again, assume I am logged in as Alice.
  • Alice sends a message to Fred saying - "Hello Fred".
  • One list element is generated stating Fred's firstname.
  • If, as Alice, I send another message to Fred, it will still display one li element for Fred - Which is good, this is what I want, as the logged in user, I want to see the user I am taking with.
  • However, if I log off, sign in a Fred, and reply back to Alice, it will generate another li element, but this time with the logged in users credentials, so an li would be generated stating Fred's first name etc.
  • What I need is all this to be in one li since the conversation is between two people.

This is my current code:

<?php
$displayed = [];
// get number of messages from a specific user to the logged in user
$get_mess = mysqli_query ($connect, "SELECT * FROM private_messages WHERE message_to = '$username' AND message_from ='$user'");
$num_msgs = mysqli_num_rows($get_mess);

// getting all the conversations which concern the user logged on.
$con = mysqli_query ($connect, "SELECT * FROM private_messages WHERE message_from='$username' OR message_to='$username'");
while ($get_con = mysqli_fetch_assoc($con)){
    $msg_from = $get_con['message_from'];
    $msg_to = $get_con['message_to'];

    // get other persons firstname    
    $u_name = mysqli_query($connect, "SELECT * FROM users WHERE username ='$msg_to'");
    $get_cu = mysqli_fetch_assoc($u_name);
    $got_ufn = $get_cu['first_name'];
    $got_uln = $get_cu['last_name'];    

    if ($msg_to == $username || $msg_from == $username){
        if(!in_array($msg_to, $displayed)) {
            echo "<li class='list' role='presentation'>
                      <div class='parent'>
                          <div class='disp_pic'>
                              <img class='img-rounded' src='$profile_pic2'/>
                          </div>
                          <div class='user_d'>
                              <a href='messages.php?u=$msg_from'> $got_ufn  $got_uln</a>
                          </div>";
            if ($num_msgs == 0){
                // dont display badge
            }else {
                echo "<span id='num_of_msgs_from' class='badge'>"; 
                if ($user == $user){
                    $num_msgs == 0;
                    echo "$num_msgs </span>";
                }else {
                    echo " $num_msgs </span>";
                }
            }
            echo"
                 </div>
             </li>";
             $displayed[] = $msg_to;
        }
    }
} // while closed
?>
3
  • 1
    Explain what doesn't work and try to produce a minimal reproducible example. Commented Mar 12, 2016 at 17:41
  • 1
    It could be that the other (not logged in) username is in $msg_from or $msg_to. But most of your code assumes it is in $msg_to. For example, SELECT * FROM users WHERE username ='$msg_to'. My suggestion would be to make a variable called something like $other_user and have logic at the top of your script to determine whether that should be set to $msg_from or $msg_to. Commented Mar 12, 2016 at 17:45
  • Also, this line if ($msg_to == $username || $msg_from == $username) does nothing because you're iterating a result set where that is always true. Commented Mar 12, 2016 at 17:46

1 Answer 1

1

You need to get other user with something like this:

$other_user = ($msg_to == $username) ? $msg_from : $msg_to;

And full code will be like this:

<?php
$displayed = [];
// get number of messages from a specific user to the logged in user
$get_mess = mysqli_query ($connect, "SELECT * FROM private_messages ".
    "WHERE message_to = '$username' AND message_from ='$user'");
$num_msgs = mysqli_num_rows($get_mess);

// getting all the conversations which concern the user logged on.
$con = mysqli_query ($connect, "SELECT * FROM private_messages ".
    "WHERE message_from='$username' OR message_to='$username'");
while ($get_con = mysqli_fetch_assoc($con)){
    $msg_from = $get_con['message_from'];
    $msg_to = $get_con['message_to'];

    $other_user = ($msg_to == $username) ? $msg_from : $msg_to;

    // get other persons firstname    
    $u_name = mysqli_query($connect, "SELECT * FROM users ".
        "WHERE username ='$other_user'");
    $get_cu = mysqli_fetch_assoc($u_name);
    $got_ufn = $get_cu['first_name'];
    $got_uln = $get_cu['last_name'];

    if ($msg_to == $username || $msg_from == $username){
        if(!in_array($other_user, $displayed)) {
            echo "<li class='list' role='presentation'>
                      <div class='parent'>
                          <div class='disp_pic'>
                              <img class='img-rounded' src='$profile_pic2'/>
                          </div>
                          <div class='user_d'>
                              <a href='messages.php?u=$other_user'> $got_ufn  $got_uln</a>
                          </div>";
            if ($num_msgs == 0){
                // dont display badge
            }else {
                echo "<span id='num_of_msgs_from' class='badge'>";
                if ($user == $user){
                    $num_msgs == 0;
                    echo "$num_msgs </span>";
                }else {
                    echo " $num_msgs </span>";
                }
            }
            echo"
                 </div>
             </li>";
            $displayed[] = $other_user;
        }
    }
} // while closed
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, this has resolved some of my issues. Thing is, it is still displaying the same conversation with the other user in seperate li' elements. For example, Alice sends an message to Fred, one li generated. Fred then replies to Alice, which generates another li, even though it is the same conversation?
So you need to display each interlocutor only once?
I change answer with this code: $displayed[] = $other_user;
Yes, this worked, but again, unfortunately, another issue arises. $displayed[] = $other_user; works, it makes one li, but say for example, Alice sends two messages to another user known as Conor. She messages and receives a message from Conor, this results into two li elements being generated. Im guessing $displayed[] = $other_user; only works for the person you are currently talking to?
you are right, I've forgot to fix if condition to this: if(!in_array($other_user, $displayed)) {
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.