-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_blogs.php
More file actions
41 lines (34 loc) · 1.09 KB
/
fetch_blogs.php
File metadata and controls
41 lines (34 loc) · 1.09 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
<?php
include 'db_connection.php';
// Check if a specific blog_id is requested
if (isset($_GET['blog_id'])) {
$blog_id = intval($_GET['blog_id']);
$sql = "SELECT * FROM BLOGS WHERE blog_id = $blog_id";
$result = mysqli_query($conn, $sql);
if ($result && mysqli_num_rows($result) > 0) {
$blog = mysqli_fetch_assoc($result);
echo json_encode($blog);
} else {
echo json_encode(null);
}
} else {
// Check if sort parameter is provided
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'recent';
// Set SQL query based on sort parameter
if ($sort == 'topRated') {
$sql = "SELECT * FROM BLOGS ORDER BY (upvotes - downvotes) DESC, date DESC";
} else {
$sql = "SELECT * FROM BLOGS ORDER BY date DESC";
}
$result = mysqli_query($conn, $sql);
$blogs = [];
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$blogs[] = $row;
}
}
// Return blogs in JSON format
echo json_encode($blogs);
}
mysqli_close($conn);
?>