由于查询,人们在聊天页面上显示了两次 [英] people are shown twice on the chat page because of query

查看:52
本文介绍了由于查询,人们在聊天页面上显示了两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a facebook style chat page using php. But i have a problem to showing old conversations. You know that facebook left sidebar showing conversations and when you create new message user can see your message on his message page. PROBLEM: when i send message to someone they see my message two times from the left sidebar. Also i see my message two times from the left sidebar.

我创建了这个 SQLfiddle

I have created this 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';

Note: The $uid is loged in user id. Like $uid = '2';

我像这样在屏幕上打印出来.

And I print it on the screen like this.

$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> ';
                   }
              } 

推荐答案

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

The behavior is normal, but you should limit by 1 and sort by time to select the most recent message.

同样在你的 SQL 小提琴中,我猜你在用户和用户之间混淆.

Also in your SQL fiddle I guess you're mixing up from user and to user.

这篇关于由于查询,人们在聊天页面上显示了两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆