我正在messages.php
中列出一个列表,其中显示了登录用户($username
)拥有的所有活动对话。
什么被归类为"主动对话"?(什么应该列在 li..)
- 假设我以爱丽丝的身份登录。爱丽丝向弗雷德发送消息,这是一个主动对话,
$username
(爱丽丝)正在向$user
(弗雷德)发送消息。 - 假设我以爱丽丝的身份登录。弗雷德给爱丽丝发了一条信息。这也是一次积极的对话。
- 摘要:应列出发往
$username
(登录用户)的任何消息和来自$username
的任何消息,仅列出一次。
当前行为:
目前,使用下面显示的代码,正在为我发送的每条消息(private_messages
表中与$username
相关的每一行)生成一个li
。
例如:
- 同样,假设我以爱丽丝的身份登录。
- 爱丽丝给弗雷德发了一条信息,说——"你好弗雷德"。
- 生成一个列表元素,说明 Fred 的名字。
- 如果作为爱丽丝,我向弗雷德发送另一条消息,它仍然会显示一个
li
元素以供Fred
- 这很好,这就是我想要的,作为登录用户,我想看到我正在带走的用户。 - 但是,如果我注销,登录
Fred
,然后回复 Alice,它将生成另一个li
元素,但这次使用登录的用户凭据,因此将生成一个li
,说明 Fred 的名字等。 - 我需要的是所有这些都在一个
li
,因为对话是在两个人之间进行的。
这是我当前的代码:
<?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
?>
你需要用这样的东西让其他用户:
$other_user = ($msg_to == $username) ? $msg_from : $msg_to;
完整的代码将是这样的:
<?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
?>