我在messages.php
中创建了一个列表,其中显示了登录用户($username
)拥有的所有活动对话。
什么被归类为“积极对话”? (李应该列出什么......)
$username
(Alice)正在向$user
(Fred)发送消息。$username
(已登录用户)的所有邮件以及来自$username
的所有邮件,仅限ONCE。当前行为:
目前,使用下面显示的代码,我发送的每封邮件都会生成li
(private_messages
表格中与$username
相关的每一行)。
例如:
li
的一个Fred
元素 - 这很好,这就是我想要的,作为登录用户,我我希望看到我正在使用的用户。Fred
并回复Alice,它会生成另一个li
元素,但这次使用登录的用户凭据,所以{{{}将生成1}}说明弗雷德的名字等。li
,因为对话是在两个人之间。这是我当前的代码:
li
答案 0 :(得分:1)
您需要让其他用户使用以下内容:
$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
?>