【问题标题】:people are shown twice on the chat page because of query由于查询,人们在聊天页面上显示了两次
【发布时间】:2018-08-27 08:36:49
【问题描述】:

我正在尝试使用 php 制作一个 facebook 风格的聊天页面。但是我在显示旧对话时遇到了问题。您知道 facebook 左侧边栏显示对话,当您创建新消息时,用户可以在他的消息页面上看到您的消息。 问题:当我向某人发送消息时,他们从左侧边栏中看到我的消息两次。我也从左侧边栏中看到我的消息两次。

我创建了这个SQLfiddle

这是我的查询函数:

public function ChatUserList($uid){
   $uid = mysqli_real_escape_string($this->db,$uid); 
   $ChatUserListQuery = mysqli_query($this->db,"SELECT DISTINCT C.to_user_id,
   C.from_user_id,U.user_name, U.user_fullname 
   FROM chat C INNER JOIN users U 
   ON U.userid = C.from_user_id WHERE U.userid = '$uid' 
   AND (C.from_user_id = '$uid' OR C.to_user_id = '$uid')") 
   or die(mysqli_error($this->db));
   while($row=mysqli_fetch_array($ChatUserListQuery)) {
        // Store the result into array
        $data[]=$row;
     }
     if(!empty($data)) {
        // Store the result into array
        return $data;
     }
}

注意:$uid 以用户 ID 登录。赞$uid = '2';

然后我像这样在屏幕上打印出来。

$ConversationUserList = $Get->ChatUserList($uid);
 if($ConversationUserList){
   foreach($ConversationUserList as $cData){ 
     $conversationUserID = $cData['to_user_id'];   
     $conversationUserAvatar = $Get->UserAvatar($conversationUserID, $base_url);  
     $conversationLastMessage = $Get->ChatLastMessage($uid,$conversationUserID);
     $conversationUserFullName = $Get->UserFullName($conversationUserID);
     $conversationUserName = $Get->GetUserName($conversationUserID);
   if($conversationUserID == $uid){
     $conversationUserID = $cData['from_user_id'];
     $conversationUserAvatar = $Get->UserAvatar($conversationUserID, $base_url);  
     $conversationLastMessage = $Get->ChatLastMessage($uid,$conversationUserID);
     $conversationUserFullName = $Get->UserFullName($conversationUserID);
     $conversationUserName = $Get->GetUserName($conversationUserID);
     }
     $time = $conversationLastMessage['message_created_time']; 
     $conversationLastMessageTime = date("c", $time);
     $getConversationLastMessage = $conversationLastMessage['message_text'];
     echo '
     <!---->
     <li class="_5l-3 _1ht1 _1ht2 getuse" data-id="'.$conversationUserID.'" data-user="'.$conversationUserName.'">
     <img src="'.$conversationUserAvatar.'" class="img_avatar"> 
         <span>
     <abbr class="_1ht7 timeago" id="time'.$conversationUserID.'" title="'.$conversationLastMessageTime.'">
     '.$conversationLastMessageTime.'</abbr>
     </span>
     <span class="_1qt5 _5l-3"> 
     <span class="txt_st12" id="msg'.$conversationUserID.'">
      '.htmlcode($getConversationLastMessage).'
     </span>
     </span>
     </li> ';
                   }
              } 

【问题讨论】:

  • 添加一些示例表数据和预期结果为格式化文本,而不是图像。
  • 你得到的结果有什么“错误”?
  • 我认为您的查询没问题,但您遇到了 foreach 循环问题

标签: php mysql sql


【解决方案1】:

行为正常,但应限制为 1 并按时间排序以选择最近的消息。

在你的 SQL 小提琴中,我猜你正在从一个用户到另一个用户混在一起。

【讨论】:

    【解决方案2】:

    实际查询是正确的,因为您有 3-2 和 4-2。要获得单个用户的唯一列表,您需要从选择中删除 from 和 to,以便您的 distinct 仅适用于您感兴趣的用户,而不适用于其中包含所有其他内容的元组。

    【讨论】:

    • 那么您将不会获得不同的用户!
    • 那么您需要 2 个查询(一个用于此,一个用于“其他事物”)或更改您的循环以检测并跳过此结果中的重复项。
    猜你喜欢
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2011-02-04
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多