PHP聊天系统(显示所有消息按日期排序)

时间:2018-03-13 06:05:47

标签: php

我遇到了问题,但他没有在日期之后对所有邮件进行排序。当然,最新消息应始终排在最前面。

在我的页面上观看示例:http://test.liscon.bplaced.net/messages.php

<?php 
$user = $pdo->prepare("SELECT user.id, user.username, user.image FROM user;");
if(!$user->execute()) {
    print_r($user->errorInfo());
}           
?>
<?php

     while($row = $user->fetch(PDO::FETCH_ASSOC)) { ?>
     <form class="left" action="" method="GET">
     <?php                  
         if($stmt = $pdo->prepare("SELECT user.id, msg.id, 
             msg.user_id, msg.sender_id, msg.message, msg.date                      
             FROM msg
             INNER JOIN user
                 ON msg.user_id = user.id || 
                    msg.sender_id = user.id                     
             WHERE msg.user_id = :user_id AND sender_id = :sender_id || 
                   msg.user_id = :sender_id AND msg.sender_id = :user_id 
             ORDER BY date DESC LIMIT 1;")
           )
           {
               $stmt->BindParam(':user_id', $_SESSION['id']);
               $stmt->BindParam(':sender_id', $row['id']);                      

               if(!$stmt->execute()) {
                   print_r($stmt->errorInfo());
               }
           }
      ?>

Table structur

1 个答案:

答案 0 :(得分:0)

您可以尝试更改下面的查询吗?

ON (msg.user_id = user.id || msg.sender_id = user.id)
    WHERE (msg.user_id = :user_id AND sender_id = :sender_id) OR (msg.user_id = :sender_id AND msg.sender_id = :user_id) ORDER BY `msg.date` DESC LIMIT 1

让我知道它是否有效?

谢谢, Smit Raval