search.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. require_once '../includes/config.php';
  3. require_once '../includes/database.php';
  4. require_once '../includes/publication.php';
  5. $publication = new Publication();
  6. $query = $_GET['q'] ?? '';
  7. $page = max(1, (int)($_GET['page'] ?? 1));
  8. $limit = 10;
  9. $offset = ($page - 1) * $limit;
  10. $results = [];
  11. $totalResults = 0;
  12. if ($query) {
  13. $results = $publication->search($query, 'published');
  14. $totalResults = count($results);
  15. // Apply pagination
  16. $results = array_slice($results, $offset, $limit);
  17. }
  18. ?>
  19. <!DOCTYPE html>
  20. <html lang="en">
  21. <head>
  22. <meta charset="UTF-8">
  23. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  24. <title>Search - <?php echo SITE_TITLE; ?></title>
  25. <link rel="stylesheet" href="../css/style.css">
  26. </head>
  27. <body>
  28. <header class="site-header">
  29. <div class="container">
  30. <h1><a href="index.php"><?php echo SITE_TITLE; ?></a></h1>
  31. <nav class="main-nav">
  32. <a href="index.php">Home</a>
  33. <a href="categories.php">Categories</a>
  34. <a href="search.php">Search</a>
  35. </nav>
  36. </div>
  37. </header>
  38. <div class="container">
  39. <main class="main-content">
  40. <h2>Search Publications</h2>
  41. <form method="get" class="search-form-large">
  42. <input type="text" name="q" placeholder="Search for publications..."
  43. value="<?php echo htmlspecialchars($query); ?>" required>
  44. <button type="submit" class="btn btn-primary">Search</button>
  45. </form>
  46. <?php if ($query): ?>
  47. <div class="search-results">
  48. <h3>Search Results for "<?php echo htmlspecialchars($query); ?>"</h3>
  49. <?php if (empty($results)): ?>
  50. <p>No publications found matching your search.</p>
  51. <?php else: ?>
  52. <p class="results-count">Found <?php echo $totalResults; ?> publications</p>
  53. <div class="publication-list">
  54. <?php foreach ($results as $pub): ?>
  55. <article class="publication-summary">
  56. <h3>
  57. <a href="publication.php?id=<?php echo $pub['id']; ?>">
  58. <?php echo htmlspecialchars($pub['title']); ?>
  59. </a>
  60. </h3>
  61. <?php if ($pub['summary']): ?>
  62. <p class="summary"><?php echo htmlspecialchars($pub['summary']); ?></p>
  63. <?php endif; ?>
  64. <div class="meta">
  65. <span class="author">By <?php echo htmlspecialchars($pub['author']); ?></span>
  66. <span class="date"><?php echo date('F j, Y', strtotime($pub['created_at'])); ?></span>
  67. <?php if ($pub['categories']): ?>
  68. <span class="categories">
  69. <?php foreach (explode(',', $pub['categories']) as $cat): ?>
  70. <a href="index.php?category=<?php echo urlencode(trim($cat)); ?>" class="category-tag">
  71. <?php echo htmlspecialchars(trim($cat)); ?>
  72. </a>
  73. <?php endforeach; ?>
  74. </span>
  75. <?php endif; ?>
  76. </div>
  77. </article>
  78. <?php endforeach; ?>
  79. </div>
  80. <?php $totalPages = ceil($totalResults / $limit); ?>
  81. <?php if ($totalPages > 1): ?>
  82. <div class="pagination">
  83. <?php if ($page > 1): ?>
  84. <a href="?q=<?php echo urlencode($query); ?>&page=<?php echo $page - 1; ?>" class="pagination-link">« Previous</a>
  85. <?php endif; ?>
  86. <?php for ($i = 1; $i <= $totalPages; $i++): ?>
  87. <?php if ($i == $page): ?>
  88. <span class="pagination-current"><?php echo $i; ?></span>
  89. <?php else: ?>
  90. <a href="?q=<?php echo urlencode($query); ?>&page=<?php echo $i; ?>" class="pagination-link"><?php echo $i; ?></a>
  91. <?php endif; ?>
  92. <?php endfor; ?>
  93. <?php if ($page < $totalPages): ?>
  94. <a href="?q=<?php echo urlencode($query); ?>&page=<?php echo $page + 1; ?>" class="pagination-link">Next »</a>
  95. <?php endif; ?>
  96. </div>
  97. <?php endif; ?>
  98. <?php endif; ?>
  99. </div>
  100. <?php endif; ?>
  101. </main>
  102. </div>
  103. <footer class="site-footer">
  104. <div class="container">
  105. <p>&copy; <?php echo date('Y'); ?> <?php echo SITE_TITLE; ?>. All rights reserved.</p>
  106. </div>
  107. </footer>
  108. </body>
  109. </html>