doushou1298 2018-06-09 17:49
浏览 47
已采纳

如何在左右两侧显示数据库中的消息?

'I made a simple chat box with php but the problem is i want to display my messages on the right and the other user messages on the left i made two while loops but they didn't work. My table is like this (Sender - reciever - msg) I want to display each message ordered by time and on different places right side and left side

if (isset($_POST['user_id'])){ $sender_id = $_POST['user_id']; $sql = "SELECT * FROM messages_sys WHERE send_id=$sender_id AND reciever_id=$uid"; $sql_query = mysqli_query($conn, $sql); print_r($sql_query);

  • 写回答

1条回答 默认 最新

  • douboshan1466 2018-06-09 18:21
    关注

    you can use 'if' statement inside for loop :

    foreach($messages as $message){
    
       if($message->reciever == user_id){
         <div class="right">$message</div>
    
       }elseif($meesage->sender == user_id){
        <div class="left">$message</div>
    
       }
    
    }
    

    of course you have to open and close php tag before type html code inside

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?