-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory.php
More file actions
244 lines (222 loc) · 9.94 KB
/
category.php
File metadata and controls
244 lines (222 loc) · 9.94 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
require_once 'includes/dbh.inc.php';
require_once 'includes/cache-manager.inc.php';
require_once 'includes/category-functions.inc.php';
session_set_cookie_params(0, '/');
session_start();
$slug = $_GET['slug'] ?? '';
if (empty($slug)) {
header('Location: /');
exit;
}
// Pagination settings
$postsPerPage = 10;
$currentPage = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($currentPage - 1) * $postsPerPage;
try {
// Get category information
$stmt = $pdo->prepare("SELECT * FROM Categories WHERE Slug = ?");
$stmt->execute([$slug]);
$category = $stmt->fetch();
if (!$category) {
header('HTTP/1.0 404 Not Found');
exit('Category not found');
}
// Check for search query
$searchTerm = $_GET['q'] ?? '';
if (!empty($searchTerm)) {
// Use the search handler with category filter
require_once 'includes/search-handler.inc.php';
$searchResults = getSearchResults($pdo, $searchTerm, $currentPage, $postsPerPage, [
'categoryId' => $category['CategoryID']
]);
$posts = $searchResults['posts'];
$totalPosts = $searchResults['total'];
$totalPages = $searchResults['pages'];
} else {
// Original category posts query code
$stmt = $pdo->prepare("
SELECT COUNT(DISTINCT p.PostID)
FROM Posts p
JOIN PostCategories pc ON p.PostID = pc.PostID
WHERE pc.CategoryID = ? AND p.Status = 'published'
");
$stmt->execute([$category['CategoryID']]);
$totalPosts = $stmt->fetchColumn();
$totalPages = ceil($totalPosts / $postsPerPage);
}
// Get posts for current page
$stmt = $pdo->prepare("
SELECT
p.PostID,
p.Title,
p.CreatedAt,
p.FeaturedImage,
p.FeaturedImageCaption,
LEFT(p.Content, 300) as Excerpt,
u.Username as AuthorName,
GROUP_CONCAT(DISTINCT c.Name) as Categories,
GROUP_CONCAT(DISTINCT t.Name) as Tags
FROM Posts p
LEFT JOIN Users u ON p.UserID = u.UserID
LEFT JOIN PostCategories pc ON p.PostID = pc.PostID
LEFT JOIN Categories c ON pc.CategoryID = c.CategoryID
LEFT JOIN PostTags pt ON p.PostID = pt.PostID
LEFT JOIN Tags t ON pt.TagID = t.TagID
WHERE p.Status = 'published' AND pc.CategoryID = ?
GROUP BY p.PostID
ORDER BY p.CreatedAt DESC
LIMIT ? OFFSET ?
");
$stmt->execute([$category['CategoryID'], $postsPerPage, $offset]);
$posts = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Category: <?php echo htmlspecialchars($category['Name']); ?></title>
<link rel="stylesheet" href="/css/blog-styles.css">
<link rel="stylesheet" href="/style.css">
</head>
<body>
<?php include_once 'header.php'; ?>
<script src="/js/breadcrumbs.js"></script>
<div class="blog-header-container">
<div class="blog-header">
<h1>Category: <?php echo htmlspecialchars($category['Name']); ?></h1>
</div>
</div>
<div class="blog-container">
<!-- Posts Grid -->
<div class="posts-grid">
<?php if (empty($posts)): ?>
<div class="no-posts">
<p>No posts found in this category.</p>
</div>
<?php else: ?>
<?php foreach ($posts as $post):
$slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $post['Title'])));
?>
<article class="post-card">
<?php if ($post['FeaturedImage']): ?>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>" class="post-thumbnail-link">
<img src="<?php echo htmlspecialchars($post['FeaturedImage']); ?>"
alt="<?php echo htmlspecialchars($post['Title']); ?>"
class="post-thumbnail">
</a>
<?php endif; ?>
<div class="post-content">
<h2>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>">
<?php echo htmlspecialchars($post['Title']); ?>
</a>
</h2>
<div class="post-meta">
<span class="post-date">
<?php echo date('F j, Y', strtotime($post['CreatedAt'])); ?>
</span>
<span class="post-author">
by <?php echo htmlspecialchars($post['AuthorName']); ?>
</span>
<?php if ($post['Categories']): ?>
<span class="post-categories">
<?php
$categories = explode(',', $post['Categories']);
foreach ($categories as $index => $cat):
$categorySlug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $cat)));
?>
<a class="article-category-tag" href="/category/<?php echo $categorySlug; ?>">
<?php echo htmlspecialchars(trim($cat)); ?>
</a><?php echo $index < count($categories) - 1 ? ' ' : ''; ?>
<?php endforeach; ?>
</span>
<?php endif; ?>
</div>
<div class="post-excerpt">
<?php
$excerpt = strip_tags($post['Excerpt']);
if (strlen($excerpt) >= 300) {
$excerpt = substr($excerpt, 0, strrpos(substr($excerpt, 0, 300), ' ')) . '...';
}
echo $excerpt;
?>
</div>
<a href="/blog/post/<?php echo $post['PostID']; ?>/<?php echo $slug; ?>" class="read-more">
Read More
</a>
</div>
</article>
<?php endforeach; ?>
<?php endif; ?>
</div>
<!-- Sidebar -->
<div class="blog-sidebar">
<!-- Search -->
<div class="blog-search-container">
<input type="text" id="search-bar" placeholder="Search...">
</div>
<!-- Categories -->
<div class="blog-categories">
<h2>Categories</h2>
<ul class="blog-categories-list">
<?php
$sidebarCategories = getCategoriesWithPostCount($pdo);
foreach ($sidebarCategories as $cat):
$catSlug = htmlspecialchars($cat['Slug']);
$name = htmlspecialchars($cat['Name']);
$count = (int)$cat['post_count'];
$isActive = $cat['CategoryID'] === $category['CategoryID'];
?>
<li class="blog-category">
<a class="category-link <?php echo $isActive ? 'active' : ''; ?>"
href="/category/<?php echo $catSlug; ?>">
<span class="category-name"><?php echo $name; ?></span>
<span class="category-count"><?php echo $count; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<!-- Ad Box -->
<div class="sidebar-sponsor-container">
<img src="/img/sponsor_spot.png" alt="Ads support us so much!">
</div>
</div>
</div>
<?php if ($totalPages > 1): ?>
<nav class="pagination">
<?php if ($currentPage > 1): ?>
<a href="/category/<?php echo $category['Slug']; ?>/page/<?php echo ($currentPage - 1); ?>" class="page-link">Previous</a>
<?php endif; ?>
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<?php if (
$i == 1 ||
$i == $totalPages ||
($i >= $currentPage - 2 && $i <= $currentPage + 2)
): ?>
<a href="/category/<?php echo $category['Slug']; ?>/page/<?php echo $i; ?>"
class="page-link <?php echo $i === $currentPage ? 'active' : ''; ?>"><?php echo $i; ?></a>
<?php elseif (
$i == $currentPage - 3 ||
$i == $currentPage + 3
): ?>
<span class="page-ellipsis">...</span>
<?php endif; ?>
<?php endfor; ?>
<?php if ($currentPage < $totalPages): ?>
<a href="/category/<?php echo $category['Slug']; ?>/page/<?php echo ($currentPage + 1); ?>" class="page-link">Next</a>
<?php endif; ?>
</nav>
<?php endif; ?>
<?php include_once 'footer.php'; ?>
<script src="/js/search.js"></script>
</body>
</html>
<?php
} catch (PDOException $e) {
error_log("Error in category.php: " . $e->getMessage());
echo "An error occurred while loading the category posts.";
}
?>