-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_status_notifications.php
More file actions
56 lines (45 loc) · 1.73 KB
/
fetch_status_notifications.php
File metadata and controls
56 lines (45 loc) · 1.73 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
session_start();
require_once 'config.php';
$user_id = $_SESSION['user_id'] ?? 0;
if ($user_id == 0) {
echo json_encode(["status" => "error", "message" => "User not logged in."]);
exit();
}
// Fetch notifications
$query = "SELECT booking_id, service_name, status, preferred_date, created_at, is_read
FROM bookings
WHERE user_id = ?
ORDER BY created_at DESC";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$notifications = [];
$unread_count = 0;
while ($row = $result->fetch_assoc()) {
$status_message = "";
if ($row['status'] == "Pendings") {
$status_message = "Your booking request for '{$row['service_name']}' (Booking ID: #{$row['booking_id']}) has been accepted and is pending.";
} elseif ($row['status'] == "Rejected") {
$status_message = "Your booking request for '{$row['service_name']}' (Booking ID: #{$row['booking_id']}) was rejected.";
} elseif ($row['status'] == "In Progress") {
$status_message = "Your booking #{$row['booking_id']} for '{$row['service_name']}' is now **In Progress**.";
} elseif ($row['status'] == "Completed") {
$status_message = "Your booking #{$row['booking_id']} for '{$row['service_name']}' has been **Completed**!";
}
// ✅ Count unread messages
if ($row['is_read'] == 0) {
$unread_count++;
}
$notifications[] = [
"message" => $status_message,
"created_at" => $row['created_at'],
"is_read" => $row['is_read']
];
}
// ✅ Ensure the unread count is returned
echo json_encode(["status" => "success", "notifications" => $notifications, "unread_count" => $unread_count]);
$stmt->close();
$conn->close();
?>