Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

php - people are shown twice on the chat page because of query

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.

I have created this SQLfiddle

So here is my query function:

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

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> ';
                   }
              } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...