if (isset($_GET['fetchMessages']) && isset($_GET['consultation_id'])) {
$consultation_id = intval($_GET['consultation_id']);
$user_id = $_SESSION['id'];
// Fetch messages with sender profile pic
$query = "SELECT cc.*, u.profile_pic
FROM consultation_chat cc
JOIN users u ON cc.sender_id = u.id
WHERE cc.consultation_id = ?
ORDER BY cc.sent_at ASC";
$stmt = $connection->prepare($query);
$stmt->bind_param("i", $consultation_id);
$stmt->execute();
$result = $stmt->get_result();
while ($msg = $result->fetch_assoc()) {
$isSender = $msg['sender_id'] === $user_id;
$alignment = $isSender ? 'flex-end' : 'flex-start';
$bubble = $isSender ? 'sent' : 'received';
$pic = !empty($msg['profile_pic']) ? $msg['profile_pic'] : 'medconnect_images/blank_profile_pic.png';
echo "
" . (!$isSender ? "
" : "") . "
" . htmlspecialchars($msg['message'], ENT_QUOTES) . "
" . date("Y-m-d H:i", strtotime($msg['sent_at'])) . "
" . ($isSender ? "
" : "") . "
";
}
exit;
}
// Handle new message POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['message'])) {
$msg = $_POST['message'];
$sender_id = intval($_POST['sender_id']);
$receiver_id = intval($_POST['receiver_id']);
$consult_id = intval($_POST['consultation_id']);
$stmt = $connection->prepare("INSERT INTO consultation_chat (consultation_id, sender_id, receiver_id, message) VALUES (?, ?, ?, ?)");
$stmt->bind_param("iiis", $consult_id, $sender_id, $receiver_id, $msg);
$stmt->execute();
// header("Location: user_window.php?consultation_id=$consult_id§ion=chat");
exit;
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... ger-system