| 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 : /bpkd/ |
Upload File : |
<?php
/**
* Halaman Pencarian
* Website Instansi - CMS
*/
// Load inisialisasi
require_once 'includes/init.php';
// Set page variables
$page_title = 'Hasil Pencarian - ' . ($settings['site_title'] ?? 'Website Instansi');
$meta_description = 'Hasil pencarian konten di website ' . ($settings['site_title'] ?? 'Website Instansi');
// Additional CSS
$additional_css = ['assets/css/search.css'];
// Gunakan global $conn
global $conn;
// ===== AMBIL KATA KUNCI PENCARIAN =====
$keyword = isset($_GET['q']) ? trim($_GET['q']) : '';
$keyword_raw = $keyword;
// ===== VALIDASI KEYWORD =====
if (!empty($keyword)) {
// Sanitasi untuk keamanan
$keyword = sanitize($keyword);
}
// ===== PAGINATION SETUP =====
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
if ($page < 1) $page = 1;
$offset = ($page - 1) * $limit;
// ===== INISIALISASI VARIABEL =====
$total_results = 0;
$total_pages = 0;
$results = [];
$error_message = '';
// ===== LAKUKAN PENCARIAN JIKA ADA KEYWORD =====
if (!empty($keyword)) {
// Escape untuk query
$search_term = "%$keyword%";
// ===== 1. HITUNG TOTAL HASIL =====
$sql_count = "
SELECT
(SELECT COUNT(*) FROM articles WHERE status='published' AND (title LIKE '$search_term' OR content LIKE '$search_term' OR excerpt LIKE '$search_term')) as total_articles,
(SELECT COUNT(*) FROM pages WHERE status='published' AND (title LIKE '$search_term' OR content LIKE '$search_term' OR excerpt LIKE '$search_term')) as total_pages,
(SELECT COUNT(*) FROM gallery WHERE status='active' AND (title LIKE '$search_term' OR description LIKE '$search_term')) as total_gallery
";
$result_count = $conn->query($sql_count);
if ($result_count) {
$counts = $result_count->fetch_assoc();
$total_results = $counts['total_articles'] + $counts['total_pages'] + $counts['total_gallery'];
$total_pages = ceil($total_results / $limit);
}
// ===== 2. AMBIL DATA ARTIKEL =====
if ($counts['total_articles'] > 0) {
$sql_articles = "
SELECT
'article' as type,
id,
title,
content,
excerpt,
featured_image as image,
created_at,
slug
FROM articles
WHERE status='published'
AND (title LIKE '$search_term' OR content LIKE '$search_term' OR excerpt LIKE '$search_term')
ORDER BY created_at DESC
";
$result_articles = $conn->query($sql_articles);
if ($result_articles) {
while($row = $result_articles->fetch_assoc()) {
$results[] = $row;
}
}
}
// ===== 3. AMBIL DATA HALAMAN =====
if ($counts['total_pages'] > 0) {
$sql_pages = "
SELECT
'page' as type,
id,
title,
content,
excerpt,
featured_image as image,
created_at,
slug
FROM pages
WHERE status='published'
AND (title LIKE '$search_term' OR content LIKE '$search_term' OR excerpt LIKE '$search_term')
ORDER BY created_at DESC
";
$result_pages = $conn->query($sql_pages);
if ($result_pages) {
while($row = $result_pages->fetch_assoc()) {
$results[] = $row;
}
}
}
// ===== 4. AMBIL DATA GALLERY =====
if ($counts['total_gallery'] > 0) {
$sql_gallery = "
SELECT
'gallery' as type,
id,
title,
description as content,
description as excerpt,
image,
created_at,
NULL as slug
FROM gallery
WHERE status='active'
AND (title LIKE '$search_term' OR description LIKE '$search_term')
ORDER BY created_at DESC
";
$result_gallery = $conn->query($sql_gallery);
if ($result_gallery) {
while($row = $result_gallery->fetch_assoc()) {
$results[] = $row;
}
}
}
// ===== 5. URUTKAN BERDASARKAN TANGGAL =====
if (!empty($results)) {
usort($results, function($a, $b) {
return strtotime($b['created_at']) - strtotime($a['created_at']);
});
}
// ===== 6. DEBUG: Catat jumlah hasil =====
error_log("Pencarian: keyword='$keyword', total=$total_results, results_count=" . count($results));
}
// Include header
include 'includes/header.php';
?>
<!-- Page Header -->
<section class="page-header bg-primary text-white py-4">
<div class="container">
<div class="row">
<div class="col-12">
<h1 class="h3 mb-0"><i class="fas fa-search me-2"></i>Hasil Pencarian</h1>
</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 active" aria-current="page">Pencarian</li>
</ol>
</nav>
</div>
</div>
<!-- Search Form Section -->
<section class="search-form-section py-4">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-8">
<form action="search.php" method="GET" class="search-form" id="searchForm">
<div class="input-group">
<input type="text"
class="form-control form-control-lg"
name="q"
id="searchInput"
value="<?php echo htmlspecialchars($keyword_raw); ?>"
placeholder="Ketik kata kunci pencarian..."
autofocus>
<button class="btn btn-primary" type="submit" id="searchButton">
<i class="fas fa-search me-2"></i>Cari
</button>
</div>
</form>
</div>
</div>
</div>
</section>
<!-- Search Results Section -->
<section class="search-results-section py-4 <?php echo !empty($keyword) ? 'bg-light' : ''; ?>">
<div class="container">
<?php if (!empty($keyword)): ?>
<!-- Search Info -->
<div class="search-info mb-4" id="searchInfo">
<p class="lead">
<i class="fas fa-info-circle me-2 text-primary"></i>
Menampilkan hasil untuk "<strong><?php echo htmlspecialchars($keyword); ?></strong>"
<span class="badge bg-primary ms-2" id="totalResults"><?php echo $total_results; ?> ditemukan</span>
</p>
</div>
<?php if ($total_results > 0): ?>
<!-- Results List -->
<div class="results-list" id="resultsList">
<?php
// Potong hasil untuk halaman ini
$page_results = array_slice($results, $offset, $limit);
if (!empty($page_results)):
$no = $offset + 1;
foreach ($page_results as $item):
// Buat URL berdasarkan tipe
if ($item['type'] == 'article') {
$url = article_url($item);
} elseif ($item['type'] == 'page') {
$url = '/bpkd/halaman/' . $item['slug'];
} else {
$url = '/bpkd/galeri/' . $item['id'];
}
// Ambil excerpt
$excerpt = !empty($item['excerpt']) ? $item['excerpt'] : strip_tags($item['content']);
$excerpt = substr($excerpt, 0, 150) . (strlen($excerpt) > 150 ? '...' : '');
// Icon berdasarkan tipe
$icon = '';
$type_label = '';
$badge_color = '';
switch($item['type']) {
case 'article':
$icon = 'newspaper';
$type_label = 'Artikel';
$badge_color = 'primary';
break;
case 'page':
$icon = 'file-alt';
$type_label = 'Halaman';
$badge_color = 'success';
break;
case 'gallery':
$icon = 'images';
$type_label = 'Galeri';
$badge_color = 'warning';
break;
}
?>
<div class="search-item card mb-3" data-aos="fade-up">
<div class="card-body">
<div class="row">
<?php if (!empty($item['image'])): ?>
<div class="col-md-2 mb-3 mb-md-0">
<img src="<?php echo getImageOrDefault($item['image'], $item['type'], $item['title'], 150, 150); ?>"
alt="<?php echo htmlspecialchars($item['title']); ?>"
class="img-fluid rounded"
style="width: 100%; height: 100px; object-fit: cover;">
</div>
<div class="col-md-10">
<?php else: ?>
<div class="col-12">
<?php endif; ?>
<div class="d-flex justify-content-between align-items-start mb-2">
<h5 class="card-title mb-0">
<a href="<?php echo $url; ?>" class="text-decoration-none text-dark fw-bold">
<?php echo htmlspecialchars($item['title']); ?>
</a>
</h5>
<span class="badge bg-<?php echo $badge_color; ?>">
<i class="fas fa-<?php echo $icon; ?> me-1"></i>
<?php echo $type_label; ?>
</span>
</div>
<p class="card-text text-muted"><?php echo htmlspecialchars($excerpt); ?></p>
<div class="d-flex justify-content-between align-items-center mt-2">
<small class="text-muted">
<i class="far fa-calendar me-1"></i> <?php echo tgl_indonesia($item['created_at']); ?>
</small>
<a href="<?php echo $url; ?>" class="btn btn-sm btn-outline-primary">
Baca Selengkapnya <i class="fas fa-arrow-right ms-1"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<?php
$no++;
endforeach;
else:
?>
<div class="alert alert-warning">
<i class="fas fa-exclamation-circle me-2"></i>
Hasil ditemukan tetapi tidak ada di halaman ini.
<a href="?q=<?php echo urlencode($keyword); ?>&page=1" class="alert-link">Klik di sini</a> untuk ke halaman pertama.
</div>
<?php endif; ?>
</div>
<!-- Pagination -->
<?php if ($total_pages > 1): ?>
<nav aria-label="Page navigation" class="mt-4">
<ul class="pagination justify-content-center">
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="?q=<?php echo urlencode($keyword); ?>&page=1">
<i class="fas fa-angle-double-left"></i>
</a>
</li>
<li class="page-item <?php echo $page <= 1 ? 'disabled' : ''; ?>">
<a class="page-link" href="?q=<?php echo urlencode($keyword); ?>&page=<?php echo $page-1; ?>">
<i class="fas fa-angle-left"></i>
</a>
</li>
<?php
$start = max(1, $page - 2);
$end = min($total_pages, $page + 2);
for ($i = $start; $i <= $end; $i++):
?>
<li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
<a class="page-link" href="?q=<?php echo urlencode($keyword); ?>&page=<?php echo $i; ?>">
<?php echo $i; ?>
</a>
</li>
<?php endfor; ?>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="?q=<?php echo urlencode($keyword); ?>&page=<?php echo $page+1; ?>">
<i class="fas fa-angle-right"></i>
</a>
</li>
<li class="page-item <?php echo $page >= $total_pages ? 'disabled' : ''; ?>">
<a class="page-link" href="?q=<?php echo urlencode($keyword); ?>&page=<?php echo $total_pages; ?>">
<i class="fas fa-angle-double-right"></i>
</a>
</li>
</ul>
</nav>
<div class="text-center text-muted mt-2">
Halaman <?php echo $page; ?> dari <?php echo $total_pages; ?>
(Menampilkan <?php echo count($page_results); ?> dari <?php echo $total_results; ?> hasil)
</div>
<?php endif; ?>
<?php else: ?>
<!-- No Results -->
<div class="text-center py-5" id="noResults">
<i class="fas fa-search fa-4x text-muted mb-3"></i>
<h3>Tidak Ditemukan</h3>
<p class="text-muted mb-4">
Maaf, tidak ada hasil yang cocok dengan kata kunci "<strong><?php echo htmlspecialchars($keyword); ?></strong>"
</p>
<div class="suggestions">
<h6 class="mb-3">Saran:</h6>
<ul class="list-unstyled">
<li><i class="fas fa-check-circle text-success me-2"></i>Periksa ejaan kata kunci</li>
<li><i class="fas fa-check-circle text-success me-2"></i>Gunakan kata kunci yang lebih umum</li>
<li><i class="fas fa-check-circle text-success me-2"></i>Coba dengan kata kunci lain</li>
</ul>
</div>
</div>
<?php endif; ?>
<?php else: ?>
<!-- Initial State -->
<div class="text-center py-5" id="initialState">
<i class="fas fa-search fa-4x text-muted mb-3"></i>
<h3>Silakan Masukkan Kata Kunci</h3>
<p class="text-muted">Ketik kata kunci di atas untuk mencari konten di website ini</p>
<div class="popular-searches mt-4">
<h6 class="mb-3">Pencarian Populer:</h6>
<div>
<a href="?q=layanan" class="btn btn-outline-primary btn-sm me-2 mb-2">layanan</a>
<a href="?q=informasi" class="btn btn-outline-primary btn-sm me-2 mb-2">informasi</a>
<a href="?q=kegiatan" class="btn btn-outline-primary btn-sm me-2 mb-2">kegiatan</a>
<a href="?q=profil" class="btn btn-outline-primary btn-sm me-2 mb-2">profil</a>
<a href="?q=pengumuman" class="btn btn-outline-primary btn-sm me-2 mb-2">pengumuman</a>
</div>
</div>
</div>
<?php endif; ?>
</div>
</section>
<!-- Hidden Debug Info -->
<?php if (isset($_GET['debug']) && $_GET['debug'] == 1): ?>
<div class="container mt-3">
<div class="alert alert-secondary">
<h6>Debug Info:</h6>
<pre>
Keyword: <?php echo $keyword; ?>
Total Results: <?php echo $total_results; ?>
Total Pages: <?php echo $total_pages; ?>
Current Page: <?php echo $page; ?>
Offset: <?php echo $offset; ?>
Limit: <?php echo $limit; ?>
Total Results Array: <?php echo count($results); ?>
Page Results Array: <?php echo count($page_results ?? []); ?>
</pre>
</div>
</div>
<?php endif; ?>
<script>
// Stabilkan form pencarian
document.addEventListener('DOMContentLoaded', function() {
const searchForm = document.getElementById('searchForm');
const searchInput = document.getElementById('searchInput');
const searchButton = document.getElementById('searchButton');
if (searchForm) {
// Cegah double submit
let isSubmitting = false;
searchForm.addEventListener('submit', function(e) {
if (isSubmitting) {
e.preventDefault();
return;
}
// Validasi input tidak kosong
if (!searchInput.value.trim()) {
e.preventDefault();
alert('Masukkan kata kunci pencarian!');
searchInput.focus();
return;
}
isSubmitting = true;
searchButton.disabled = true;
searchButton.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span>Mencari...';
});
}
// Stabilkan link pagination
document.querySelectorAll('.pagination a').forEach(link => {
link.addEventListener('click', function(e) {
// Biarkan link bekerja normal
// Tidak perlu preventDefault
});
});
});
// Cegah refresh otomatis
if (window.performance) {
if (performance.navigation.type === 1) {
console.log('Halaman di-refresh');
// Tidak melakukan apa-apa, biarkan halaman tetap
}
}
</script>
<style>
/* Stabilkan tampilan */
.search-item {
transition: all 0.3s;
border-left: 3px solid transparent;
}
.search-item:hover {
border-left-color: var(--primary-color);
box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}
.pagination .page-link {
color: var(--primary-color);
border: none;
margin: 0 2px;
border-radius: 5px;
}
.pagination .page-item.active .page-link {
background: var(--primary-color);
color: white;
}
.pagination .page-item.disabled .page-link {
color: #ccc;
}
/* Loading spinner */
.spinner-border {
width: 1rem;
height: 1rem;
}
</style>
<?php
// Include footer
include 'includes/footer.php';
?>