I have a table in my database with the name of chat_message
,
i am storing into the chat_message
column, sender id as from_user_id
, receiver id as to_user_id
and the chat_message
,
Here i am using this code to fetch all the chat_message
.
<?php
session_start();
require_once('include/dbcon.php');
$session_to_user_id = $_SESSION['session_to_user_id'];
$session_id = $_SESSION['id'];
// fetching chat message of sender
$query = "SELECT * FROM `chat_message` WHERE `from_user_id`='$session_id' AND `to_user_id` = '$session_to_user_id' order by `chat_message_id` asc ";
// fetching chat message of receiver
$query1 ="SELECT * FROM `chat_message` WHERE `from_user_id`='$session_to_user_id' AND `to_user_id` = '$session_id' order by `chat_message_id` asc ";
$run = mysqli_query ($con,$query);
$run1 = mysqli_query($con,$query1);
while($data = mysqli_fetch_assoc($run) AND $data1 = mysqli_fetch_assoc($run1)){
$chat_message = $data['chat_message'];
$chat_message1 = $data1['chat_message'];
?>
<p>
<div class="chat">
<div class="bubble you black-text">
<?php
echo $chat_message;
?>
</div>
<div class="bubble me black-text">
<?php
echo $chat_message1;
?>
</div>
</div>
</p>
<?php
}
?>
The code is working but.
When one person send a message to another message then the message is displaying into chat box.
But when he send message again to the second user then the message is not displaying.
But when first user and second user sending single message in queue the script is working fine.
But when a user send more than one message then the problem occurred.
here i want only print the chat_message of the sender on right hand side and the receiver message to the left hand side.
At the end i have use division chat
to print the chat_message
of the sender on right hand side and the receiver message to the left hand side.