-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_enrollment.php
62 lines (51 loc) · 1.59 KB
/
get_enrollment.php
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
57
58
59
60
61
62
<?php
include 'dbconnection.php';
// Establish database connection
$con = dbconnection();
// Check if email parameter is set
if (!isset($_GET['email']) || empty($_GET['email'])) {
http_response_code(400); // Bad Request
echo json_encode(['error' => 'Email parameter is required']);
exit();
}
$email = $_GET['email'];
// Prepare the query to fetch course_ids based on user email
$sql = "SELECT course_id FROM enrollment WHERE uemail = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
http_response_code(404); // Not Found
echo json_encode(['error' => 'No enrollment found for the provided email']);
exit();
}
// Fetch all course_ids
$course_ids = [];
while ($row = $result->fetch_assoc()) {
$course_ids[] = $row['course_id'];
}
// Prepare the query to fetch course details based on course_ids
$placeholders = implode(',', array_fill(0, count($course_ids), '?'));
$sql = "SELECT * FROM courses WHERE course_id IN ($placeholders)";
$stmt = $con->prepare($sql);
$stmt->bind_param(str_repeat('i', count($course_ids)), ...$course_ids);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
http_response_code(404); // Not Found
echo json_encode(['error' => 'No courses found']);
exit();
}
// Fetch all course details
$courses = [];
while ($row = $result->fetch_assoc()) {
$courses[] = $row;
}
// Return course details as JSON
header('Content-Type: application/json');
echo json_encode($courses);
// Close the connection
$stmt->close();
$con->close();
?>