-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_messages.php
More file actions
38 lines (33 loc) · 1.3 KB
/
Copy pathfetch_messages.php
File metadata and controls
38 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
require 'dbconnect.php';
session_start();
// Check if the user is logged in
if (isset($_SESSION['user_id'])) {
$user_id = $_SESSION['user_id']; // Get the logged-in user ID from the session
// Query for messages
if (isset($_GET['user_id'])) {
$query = "SELECT id, user_id, admin_reply, message, timestamp, admin_id FROM chat_messages WHERE user_id = ? ORDER BY timestamp ASC";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id); // Use logged-in user ID to fetch the messages
$stmt->execute();
$result = $stmt->get_result();
$messages = [];
while ($row = $result->fetch_assoc()) {
// If admin_id is 0, it's a user message. Otherwise, it's an admin message.
$is_admin_message = ($row['admin_id'] != 0);
$messages[] = [
'id' => $row['id'],
'user_id' => $row['user_id'],
'message' => $row['message'],
'is_admin' => $is_admin_message, // Mark as admin if admin_id is not 0
'timestamp' => $row['timestamp']
];
}
echo json_encode($messages);
} else {
echo json_encode(['error' => 'Invalid user ID.']);
}
} else {
echo json_encode(['error' => 'User is not logged in.']);
}
exit;