-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_posts.php
More file actions
56 lines (49 loc) · 2.14 KB
/
fetch_posts.php
File metadata and controls
56 lines (49 loc) · 2.14 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
include 'db.php';
$categoryFilter = isset($_GET['category']) ? $_GET['category'] : '';
$animalFilter = isset($_GET['animal']) ? $_GET['animal'] : '';
$searchTerm = isset($_GET['search']) ? $_GET['search'] : '';
$sql = "SELECT posts.*,
COALESCE(users.full_name, users.first_name) AS username
FROM posts
JOIN users ON posts.user_id = users.id";
$filters = [];
if (!empty($categoryFilter)) {
$filters[] = "posts.category = '" . $conn->real_escape_string($categoryFilter) . "'";
}
if (!empty($animalFilter)) {
$filters[] = "posts.pet_category = '" . $conn->real_escape_string($animalFilter) . "'";
}
if (!empty($searchTerm)) {
$searchTermEscaped = $conn->real_escape_string($searchTerm);
$filters[] = "(posts.title LIKE '%$searchTermEscaped%' OR posts.content LIKE '%$searchTermEscaped%')";
}
// If there are any filters, add them to the SQL query
if (!empty($filters)) {
$sql .= " WHERE " . implode(" AND ", $filters);
}
// Sort by created date in descending order
$sql .= " ORDER BY posts.created_at DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo '<a href="post_detail.php?post_id=' . $row['id'] . '" class="post">';
if (!empty($row['image'])) {
echo '<img src="' . htmlspecialchars($row['image']) . '" alt="Post Image">';
}
echo '<div class="post-content">';
echo '<h4 class="post-username">' . htmlspecialchars($row['username']) . '</h4>';
echo '<h3>' . htmlspecialchars($row['title']) . '</h3>';
echo '<div style="display: flex; align-items: center;">';
echo '<span class="badge ' . strtolower($row['category']) . '">' . htmlspecialchars($row['category']) . '</span>';
echo '<span class="pet-category-badge">Category: ' . htmlspecialchars($row['pet_category']) . '</span>';
echo '</div>';
echo '<p>' . htmlspecialchars($row['content']) . '</p>';
echo '<span class="post-time">' . date("Y-m-d | h:i A", strtotime($row['created_at'])) . '</span>';
echo '</div>';
echo '</a>';
}
} else {
echo "<p>No posts available.</p>";
}
?>