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
lielement forFred- 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 anotherlielement, but this time with the logged in users credentials, so anliwould be generated stating Fred's first name etc. - What I need is all this to be in one
lisince 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
?>
$msg_fromor$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_userand have logic at the top of your script to determine whether that should be set to$msg_fromor$msg_to.if ($msg_to == $username || $msg_from == $username)does nothing because you're iterating a result set where that is always true.