-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_subjects.php
More file actions
31 lines (27 loc) · 1014 Bytes
/
fetch_subjects.php
File metadata and controls
31 lines (27 loc) · 1014 Bytes
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
<?php
session_start();
include 'db.php'; // Include your database connection
// Function to fetch subjects with their alert dates
function fetchSubjects() {
global $pdo;
$subjects = [];
// Fetch subjects belonging to the logged-in user
$userId = $_SESSION['id'];
$stmt = $pdo->prepare("SELECT s.id, s.name, GROUP_CONCAT(sa.alert_date ORDER BY sa.alert_date ASC) AS alert_dates
FROM subjects s
LEFT JOIN subject_alerts sa ON s.id = sa.subject_id
WHERE s.user_id = :userId
GROUP BY s.id, s.name");
$stmt->bindParam(':userId', $userId, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$subject = $row;
$subject['alerts'] = explode(',', $row['alert_dates']);
unset($subject['alert_dates']);
$subjects[] = $subject;
}
echo json_encode($subjects);
}
fetchSubjects();
?>