| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : D:/bpkd/ |
Upload File : |
<?php
/**
* Halaman Kategori Berita
* Website Instansi - CMS
*/
// Load inisialisasi
require_once 'includes/init.php';
// Set page variables
$page_title = 'Kategori Berita - ' . ($settings['site_title'] ?? 'Website Instansi');
$meta_description = 'Daftar berita berdasarkan kategori di website ' . ($settings['site_title'] ?? 'Website Instansi');
$meta_keywords = 'kategori, berita, artikel, informasi';
// Gunakan global $conn
global $conn;
// =====================================================
// HANDLE URL PARAMETERS
// =====================================================
$slug = isset($_GET['slug']) ? sanitize($_GET['slug']) : '';
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
// Jika tidak ada parameter, redirect ke berita
if (empty($slug) && $id <= 0) {
header("Location: berita.php");
exit;
}
// =====================================================
// AMBIL DATA KATEGORI
// =====================================================
$category = null;
// Cari berdasarkan slug
if (!empty($slug)) {
$slug = mysqli_real_escape_string($conn, $slug);
$query = "SELECT * FROM categories WHERE slug = '$slug' AND status = 'published'";
$result = $conn->query($query);
if ($result && $result->num_rows > 0) {
$category = $result->fetch_assoc();
}
}
// Cari berdasarkan ID
if (!$category && $id > 0) {
$query = "SELECT * FROM categories WHERE id = $id AND status = 'published'";
$result = $conn->query($query);
if ($result && $result->num_rows > 0) {
$category = $result->fetch_assoc();
}
}
// Jika kategori tidak ditemukan
if (!$category) {
header("HTTP/1.0 404 Not Found");
include '404.php';
exit;
}
// =====================================================
// PAGINATION SETUP
// =====================================================
$limit = 9; // Jumlah artikel per halaman
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$offset = ($page - 1) * $limit;
// =====================================================
// HITUNG TOTAL ARTIKEL DI KATEGORI INI
// =====================================================
$count_query = "SELECT COUNT(*) as total FROM articles WHERE category_id = {$category['id']} AND status = 'published'";
$count_result = $conn->query($count_query);
$total_articles = $count_result->fetch_assoc()['total'];
$total_pages = ceil($total_articles / $limit);
// =====================================================
// AMBIL ARTIKEL BERDASARKAN KATEGORI
// =====================================================
$articles = $conn->query("
SELECT a.*, u.full_name as author_name
FROM articles a
LEFT JOIN users u ON a.author_id = u.id
WHERE a.category_id = {$category['id']} AND a.status = 'published'
ORDER BY a.published_at DESC
LIMIT $offset, $limit
");
// =====================================================
// AMBIL KATEGORI LAINNYA (UNTUK SIDEBAR)
// =====================================================
$other_categories = $conn->query("
SELECT c.*, COUNT(a.id) as article_count
FROM categories c
LEFT JOIN articles a ON c.id = a.category_id AND a.status = 'published'
WHERE c.id != {$category['id']} AND c.status = 'published'
GROUP BY c.id
ORDER BY c.name
LIMIT 5
");
// Set page title
$page_title = 'Kategori: ' . $category['name'] . ' - ' . ($settings['site_title'] ?? 'Website Instansi');
$meta_description = 'Daftar berita dan artikel dalam kategori ' . $category['name'];
// Include header
include 'includes/header.php';
?>
<!-- Page Header -->
<section class="page-header bg-primary text-white py-5">
<div class="container">
<div class="row">
<div class="col-12 text-center">
<h1 class="display-4 fw-bold">Kategori: <?php echo htmlspecialchars($category['name']); ?></h1>
<?php if (!empty($category['description'])): ?>
<p class="lead"><?php echo htmlspecialchars($category['description']); ?></p>
<?php endif; ?>
</div>
</div>
</div>
</section>
<!-- Breadcrumb -->
<div class="breadcrumb-wrapper bg-light py-2">
<div class="container">
<nav aria-label="breadcrumb">
<ol class="breadcrumb mb-0">
<li class="breadcrumb-item"><a href="index.php"><i class="fas fa-home me-1"></i>Home</a></li>
<li class="breadcrumb-item"><a href="berita.php">Berita</a></li>
<li class="breadcrumb-item active"><?php echo htmlspecialchars($category['name']); ?></li>
</ol>
</nav>
</div>
</div>
<!-- Category Info -->
<section class="category-info py-3">
<div class="container">
<div class="row">
<div class="col-12">
<div class="alert alert-info mb-0">
<i class="fas fa-info-circle me-2"></i>
Menampilkan <strong><?php echo $total_articles; ?></strong> artikel dalam kategori <strong><?php echo htmlspecialchars($category['name']); ?></strong>
</div>
</div>
</div>
</div>
</section>
<!-- Articles List -->
<section class="category-articles py-5">
<div class="container">
<?php if ($articles && $articles->num_rows > 0): ?>
<div class="row">
<?php while($article = $articles->fetch_assoc()):
/*$article_url = 'artikel/' . ($article['slug'] ?: createSlug($article['title']));*/
$article_url = article_url($article);
$image_url = !empty($article['featured_image']) ? $article['featured_image'] : getDefaultImage('article', $article['title'], 400, 250);
?>
<div class="col-lg-4 col-md-6 mb-4" data-aos="fade-up" data-aos-delay="<?php echo $article['id'] * 50; ?>">
<div class="news-card h-100">
<div class="news-image">
<img src="<?php echo $image_url; ?>" alt="<?php echo htmlspecialchars($article['title']); ?>" class="img-fluid">
<div class="news-category"><?php echo htmlspecialchars($category['name']); ?></div>
</div>
<div class="news-content p-4">
<div class="news-meta mb-3">
<span class="me-3"><i class="far fa-calendar me-1"></i> <?php echo tgl_indonesia($article['published_at'] ?: $article['created_at']); ?></span>
<span><i class="far fa-user me-1"></i> <?php echo htmlspecialchars($article['author_name']); ?></span>
</div>
<h3 class="news-title h5 mb-3">
<a href="<?php echo $article_url; ?>" class="text-decoration-none"><?php echo htmlspecialchars($article['title']); ?></a>
</h3>
<p class="news-excerpt text-muted mb-3"><?php echo substr($article['excerpt'] ?? strip_tags($article['content']), 0, 100) . '...'; ?></p>
<a href="<?php echo $article_url; ?>" class="btn btn-sm btn-outline-primary">
Baca Selengkapnya <i class="fas fa-arrow-right ms-1"></i>
</a>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
<!-- Pagination -->
<?php if ($total_pages > 1): ?>
<nav aria-label="Page navigation" class="mt-5">
<ul class="pagination justify-content-center">
<!-- First Page -->
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="?slug=<?php echo urlencode($category['slug']); ?>&page=1">
<i class="fas fa-angle-double-left"></i>
</a>
</li>
<!-- Previous Page -->
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="?slug=<?php echo urlencode($category['slug']); ?>&page=<?php echo $page-1; ?>">
<i class="fas fa-angle-left"></i>
</a>
</li>
<!-- Page Numbers -->
<?php
$start = max(1, $page - 2);
$end = min($total_pages, $page + 2);
if ($start > 1) {
echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
}
for ($i = $start; $i <= $end; $i++):
?>
<li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
<a class="page-link" href="?slug=<?php echo urlencode($category['slug']); ?>&page=<?php echo $i; ?>">
<?php echo $i; ?>
</a>
</li>
<?php endfor;
if ($end < $total_pages) {
echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
}
?>
<!-- Next Page -->
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="?slug=<?php echo urlencode($category['slug']); ?>&page=<?php echo $page+1; ?>">
<i class="fas fa-angle-right"></i>
</a>
</li>
<!-- Last Page -->
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="?slug=<?php echo urlencode($category['slug']); ?>&page=<?php echo $total_pages; ?>">
<i class="fas fa-angle-double-right"></i>
</a>
</li>
</ul>
</nav>
<!-- Page Info -->
<div class="text-center text-muted mt-3">
Menampilkan <?php echo $offset + 1; ?> - <?php echo min($offset + $limit, $total_articles); ?> dari <?php echo $total_articles; ?> artikel
</div>
<?php endif; ?>
<?php else: ?>
<div class="text-center py-5">
<i class="fas fa-newspaper fa-4x text-muted mb-3"></i>
<h3>Belum Ada Artikel</h3>
<p class="text-muted mb-4">Belum ada artikel dalam kategori <?php echo htmlspecialchars($category['name']); ?></p>
<a href="berita.php" class="btn btn-primary">
<i class="fas fa-arrow-left me-2"></i>Kembali ke Berita
</a>
</div>
<?php endif; ?>
</div>
</section>
<!-- Other Categories Section -->
<?php if ($other_categories && $other_categories->num_rows > 0): ?>
<section class="other-categories py-5 bg-light">
<div class="container">
<div class="section-header text-center mb-5" data-aos="fade-up">
<h2 class="section-title">Kategori Lainnya</h2>
<p class="section-subtitle">Temukan artikel di kategori lain</p>
</div>
<div class="row">
<?php while($other = $other_categories->fetch_assoc()):
$other_url = 'kategori/' . ($other['slug'] ?: createSlug($other['name']));
?>
<div class="col-md-4 mb-3" data-aos="fade-up">
<a href="<?php echo $other_url; ?>" class="text-decoration-none">
<div class="other-category-card p-4 bg-white rounded shadow-sm text-center">
<i class="fas fa-folder fa-3x text-primary mb-3"></i>
<h4 class="h5"><?php echo htmlspecialchars($other['name']); ?></h4>
<p class="text-muted small mb-0"><?php echo $other['article_count']; ?> Artikel</p>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
</div>
</section>
<?php endif; ?>
<style>
/* Category Page Styles */
.other-category-card {
transition: all 0.3s;
border: 1px solid #e9ecef;
}
.other-category-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 25px rgba(0,0,0,0.1) !important;
background: var(--gradient-primary) !important;
color: white;
}
.other-category-card:hover i,
.other-category-card:hover h4,
.other-category-card:hover p {
color: white !important;
}
/* News Card */
.news-card {
border: none;
border-radius: 10px;
overflow: hidden;
box-shadow: 0 5px 15px rgba(0,0,0,0.05);
transition: all 0.3s;
}
.news-card:hover {
transform: translateY(-5px);
box-shadow: 0 15px 30px rgba(0,0,0,0.1);
}
.news-image {
position: relative;
height: 200px;
overflow: hidden;
}
.news-image img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.5s;
}
.news-card:hover .news-image img {
transform: scale(1.1);
}
.news-category {
position: absolute;
top: 15px;
left: 15px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 5px 15px;
border-radius: 50px;
font-size: 0.8rem;
font-weight: 600;
z-index: 1;
}
.news-content {
background: white;
}
.news-meta {
color: #999;
font-size: 0.85rem;
}
.news-meta i {
color: #667eea;
}
.news-title a {
color: #333;
transition: color 0.3s;
}
.news-title a:hover {
color: #667eea;
}
.news-excerpt {
font-size: 0.9rem;
line-height: 1.6;
}
.btn-link {
color: #667eea;
text-decoration: none;
font-weight: 600;
transition: all 0.3s;
}
.btn-link:hover {
color: #764ba2;
transform: translateX(5px);
}
/* Pagination */
.pagination .page-link {
border: none;
border-radius: 8px;
margin: 0 3px;
color: #667eea;
font-weight: 500;
transition: all 0.3s;
}
.pagination .page-link:hover {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
transform: translateY(-2px);
}
.pagination .page-item.active .page-link {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
/* Responsive */
@media (max-width: 768px) {
.page-header h1 {
font-size: 2rem;
}
.news-card {
margin-bottom: 20px;
}
}
@media (max-width: 576px) {
.page-header {
padding: 30px 0;
}
.page-header h1 {
font-size: 1.5rem;
}
}
</style>
<?php
// Include footer
include 'includes/footer.php';
?>