-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopular_courses.php
83 lines (71 loc) · 1.8 KB
/
popular_courses.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
header('Content-Type: application/json');
// Database connection details
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "learners";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to fetch up to 8 popular courses per category based on rating
$sql = "
SELECT
co.category_id,
cat.title AS category_title,
cat.image AS category_image,
co.course_id,
co.title AS title,
co.instructor_name,
co.duration,
co.price,
co.release_date,
co.video_content,
co.description,
co.video_title,
co.prerequisite,
co.rating_count,
co.certificate,
co.intro_video,
co.image AS image,
co.stars,
co.discount
FROM
courses co
JOIN
categories cat ON co.category_id = cat.category_id
JOIN (
SELECT
category_id,
course_id,
stars,
ROW_NUMBER() OVER (PARTITION BY category_id ORDER BY stars DESC) AS rank
FROM
courses
) ranked_courses ON co.course_id = ranked_courses.course_id
WHERE
ranked_courses.rank <= 3
ORDER BY
co.category_id, co.stars DESC
";
// Execute query
$result = $conn->query($sql);
if (!$result) {
echo json_encode(['error' => 'Error executing query: ' . $conn->error]);
$conn->close();
exit();
}
$popular_courses = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$popular_courses[] = $row;
}
}
// Output the data as JSON
echo json_encode($popular_courses);
// Close connection
$conn->close();
?>