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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…