I am a total beginner in PHP and MySQL. I am trying to make a chat application where users can open their chat thread and get the messages displayed. I am trying to query messages
for the chat message
and echo the message in the proper div
block. Is it possible to make an If statement
that checks if the $_SESSION['uid']
was true for a column, then output the message in it? Then echo it for each row? Sorry in advance, I am really new so I dont know all of the proper procedures and SQL commands. Thanks!
session_start();
$stmt = $conn->prepare("
SELECT message from messages WHERE conv_id=?
AND (user_send=? OR user_receive=?)
ORDER BY timestamp ASC
");
$stmt->bind_param('iii', $_POST['conv_id'], $_SESSION['uid'],$_SESSION['uid']);
$stmt->execute();
$stmt->bind_result($message);
$message_right = '';
$message_left = '';
while($stmt->fetch()){
if (user_send = $_SESSION['uid']){
$message_right .= '<div class="display-on-right">'.$message.'</div>';
echo $message_right;
}elseif(user_receive = $_SESSION['uid']){
$message_left .= '<div class="display-on-left">.$message.</div>';
echo $message_left;
}
}