我想显示两个用户之间的聊天消息,例如 user1 和 admin(即我当前是我)。这是我的数据库结构:
到目前为止我尝试过的事情:
我有一个消息框,其中显示消息,单击消息后会将用户名(即当前 user1 )发送到php以获取与该用户的聊天记录。随聊天一起显示。
$user = $_POST['user'];
$db_messages = $db_readAll("messages","sender,recipient,sending_time,read_time,recipient_status","WHERE (sender='admin' OR sender=$user) AND (recipient='admin' OR recipient=$user)","ORDER BY sending_time DESC");
$uchatMsgs = '';
foreach($db_messages as $key => $message) {
$sender=$recipient='';
if($message['sender']== $_SESSION['session']){
$name = $message['recipient'];
$sender .= '<li class="m0 p0"><div class="text-center"><small>'.$ToReadableTime('@'.$message['sending_time']).'</small></div><div class="list-group-item b0 m0 mb5" style="padding:10px 10px 0 10px !important;"><div class="chatContent themed-background b-rad-30 p10 text-light">sender</div></div></li>';
}
if($message['recipient']== $_SESSION['session']){
$name = $message['sender'];
$recipient .= '<li class="m0 p0"><div class="text-center"><small>'.$ToReadableTime('@'.$message['sending_time']).'</small></div><div class="list-group-item b0 m0" style="padding:10px 10px 0 10px !important;"><div class="online-users pull-left mr5"><img src="../uploads/userprofile/default/avatar.jpg" onerror="this.onerror=null;imgDefault(this);" alt="avatar" class="online-users-img"><span title="online"></span></div><div class="chatContent bg-lightgrey b-rad-30 p10">receiver</div></div></li>';
}
$uchatMsgs = $recipient.$sender;
}
echo json_encode(['uchatName'=>$name,'uchatMsgs' => $uchatMsgs]);
HTML:
<div class="widget shadow chatui chatui-window remove-radius-bottom chatui-window-close display-none">
<div class="widget-content widget-content-mini themed-background">
<a href="javascript:void(0)" class="chatui-action-open text-dark-op pull-right"><i class="fa fa-window-restore"></i></a>
<a href="javascript:void(0)" class="chatui-action-close text-dark-op pull-right"><i class="fa fa-window-minimize" style="vertical-align:top;"></i></a>
<a href="javascript:void(0)" class="text-dark-op pull-right"><i class="fa fa-cog"></i></a>
<a href="../profiles.php" class="text-light"><strong id="uchatName"></strong></a>
</div>
<div class="widget-content widget-content-mini chatui-talk p0">
<ul id="uchatMsgs"> </ul>
</div>
<div class="widget-content widget-content-full chatui-form remove-radius-bottom">
<form action="page_app_social.html" method="post" class="m0">
<input type="text" id="chat-message" name="chat-message" class="form-control" placeholder="Type a message and hit enter..">
</form>
</div>
</div>
jQuery:
function vchat_MSGS(user){
$.ajax({
type: 'POST',
url: '../core/ajaxpdo.class.php',
dataType: 'json',
data: {page:'page_header',action:'vchat_MSGS',user:user},
success: function(data) {
console.log(data);
$('#uchatName').text(data.uchatName);
$('#uchatMsgs').html(data.uchatMsgs);
$('.chatui-window').removeClass('display-none chatui-window-close');
}
});
return false;
}
问题:
以上是我试图在逻辑上做的但没有得到预期结果的事情。 任何人都可以通过代码帮助我,接下来该怎么做才能显示聊天记录?