Bläddra i källkod

📦 搜索页面

快乐的梦鱼 3 veckor sedan
förälder
incheckning
f6831cf1cd
3 ändrade filer med 224 tillägg och 5 borttagningar
  1. 50 2
      CommonQuery.php
  2. 6 3
      Db.class.php
  3. 168 0
      search.php

+ 50 - 2
CommonQuery.php

@@ -7,6 +7,7 @@ function loadListByChannelName($maxCount, $name) {
         // 1. 从pr_cms_channel表中通过name查询channel_id
         $channel = Db::table('pr_cms_channel')
             ->where('name', $name)
+            ->where('status', 'normal')
             ->first(['id']);
         
         // 如果没有找到对应的频道,返回空数组
@@ -19,6 +20,7 @@ function loadListByChannelName($maxCount, $name) {
         // 2. 在pr_cms_archives中通过channel_id按createtime倒序查询指定数量的最新文章
         $articles = Db::table('pr_cms_archives')
             ->where('channel_id', $channelId)
+            ->where('status', 'normal')
             ->order('createtime DESC')
             ->limit(0, $maxCount)
             ->get();
@@ -34,6 +36,7 @@ function loadListByChannelNameAndPage($maxCount, $name, $page, $searchKeyword =
         // 1. 从pr_cms_channel表中通过name查询channel_id
         $channel = Db::table('pr_cms_channel')
             ->where('name', $name)
+            ->where('status', 'normal')
             ->first(['id']);
         
         // 如果没有找到对应的频道,返回包含空列表和0总页数的数组
@@ -45,7 +48,49 @@ function loadListByChannelNameAndPage($maxCount, $name, $page, $searchKeyword =
         
         // 2. 查询符合条件的文章总数量
         $totalCountQuery = Db::table('pr_cms_archives')
-            ->where('channel_id', $channelId);
+            ->where('channel_id', $channelId)
+            ->where('status', 'normal');
+        
+        // 如果提供了搜索关键字,添加对title字段的模糊搜索
+        if (!empty($searchKeyword)) {
+            $totalCountQuery->where('title', 'like', '%' . $searchKeyword . '%');
+        }
+        
+        $totalCount = $totalCountQuery->count();
+        
+        // 计算总页数
+        $totalPages = $totalCount > 0 ? ceil($totalCount / $maxCount) : 0;
+        
+        // 3. 查询当前页的文章列表
+        $articlesQuery = Db::table('pr_cms_archives')
+            ->where('channel_id', $channelId)
+            ->where('status', 'normal');
+        
+        // 如果提供了搜索关键字,添加对title字段的模糊搜索
+        if (!empty($searchKeyword)) {
+            $articlesQuery->where('title', 'like', '%' . $searchKeyword . '%');
+        }
+        
+        $articles = $articlesQuery
+            ->order('createtime DESC')
+            ->limit(($page - 1) * $maxCount, $maxCount)
+            ->get();
+        
+        // 返回包含文章列表和总页数的数组
+        return [
+            'list' => $articles ?? [],
+            'totalPages' => $totalPages
+        ];
+    } catch (Exception $e) {
+        // 发生异常时返回包含空列表和0总页数的数组
+        return ['list' => [], 'totalPages' => 0];
+    }
+}
+function loadListPage($maxCount, $page, $searchKeyword = '') {
+    try {
+        // 2. 查询符合条件的文章总数量
+        $totalCountQuery = Db::table('pr_cms_archives')
+            ->where('status', 'normal');
         
         // 如果提供了搜索关键字,添加对title字段的模糊搜索
         if (!empty($searchKeyword)) {
@@ -59,7 +104,7 @@ function loadListByChannelNameAndPage($maxCount, $name, $page, $searchKeyword =
         
         // 3. 查询当前页的文章列表
         $articlesQuery = Db::table('pr_cms_archives')
-            ->where('channel_id', $channelId);
+            ->where('status', 'normal');
         
         // 如果提供了搜索关键字,添加对title字段的模糊搜索
         if (!empty($searchKeyword)) {
@@ -86,6 +131,7 @@ function loadChildChannelByChannelName($name) {
         // 1. 从pr_cms_channel表中通过name查询channel_id
         $channel = Db::table('pr_cms_channel')
             ->where('name', $name)
+            ->where('status', 'normal')
             ->first(['id']);
         
         // 如果没有找到对应的频道,返回空数组
@@ -98,6 +144,7 @@ function loadChildChannelByChannelName($name) {
         // 2. 在pr_cms_channel中通过parent_id查询子频道
         $childChannels = Db::table('pr_cms_channel')
             ->where('parent_id', $channelId)
+            ->where('status', 'normal')
             ->get();
         
         return $childChannels ?? [];
@@ -111,6 +158,7 @@ function getContentById($id) {
         // 1. 从pr_cms_archives表中获取基础信息
         $archive = Db::table('pr_cms_archives')
             ->where('id', $id)
+            ->where('status', 'normal')
             ->first();
         
         // 如果没有找到对应的归档信息,返回null

+ 6 - 3
Db.class.php

@@ -6,7 +6,7 @@ class Db
     protected static $cfg = [
         'dsn'      => 'mysql:host=127.0.0.1;dbname=protection_center;charset=utf8mb4',
         'user'     => 'root',
-        'pass'     => 'root',
+        'pass'     => '7ced1f1f69289c72',
         'options'  => [
             PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
             PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
@@ -105,8 +105,11 @@ class Db
     /** 查多条 */
     public function get(array $columns = ['*'])
     {
-        $col = $columns === ['*'] ? '*' : implode('`,`', $columns);
-        $sql = "SELECT `{$col}` FROM `{$this->table}`";
+        $col = $columns === ['*'] ? '*' : (
+            count($columns) === 1 ? "`{$columns[0]}`" :
+            implode('`,`', $columns)
+        );
+        $sql = "SELECT {$col} FROM `{$this->table}`";
         $this->buildWhere($sql);
         $this->buildOrder($sql);
         $this->buildLimit($sql);

+ 168 - 0
search.php

@@ -0,0 +1,168 @@
+<?php
+// 引入公共查询类
+require_once 'CommonQuery.php';
+
+// 获取URL参数
+$currentPage = isset($_GET['page']) ? intval($_GET['page']) : 1;
+$searchKeyword = isset($_GET['keyword']) ? $_GET['keyword'] : '';
+
+// 验证当前页码
+if ($currentPage < 1) {
+    $currentPage = 1;
+}
+
+// 每页显示的文章数量
+$pageSize = 12;
+
+// 使用loadListPage函数获取全部文章搜索结果
+$results = loadListPage($pageSize, $currentPage, $searchKeyword);
+$workUpdates = $results['list'];
+$totalPages = $results['totalPages'];
+
+// 轮播图数据
+$carouselItems = [
+  [
+    "image" => "/images/test-header-4.png",
+    "alt" => "搜索结果"
+  ]
+];
+?>
+<!DOCTYPE html>
+<html lang="zh-CN">
+
+<head>
+  <meta charset="UTF-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>厦门市文化遗产保护中心 - 搜索结果</title>
+  <link rel="stylesheet" href="/css/fonts.css">
+  <link rel="stylesheet" href="/css/fontawesome.min.css">
+  <link rel="stylesheet" href="/css/bootstrap.min.css">
+  <link rel="stylesheet" href="/css/swiper-bundle.min.css">
+  <link rel="stylesheet" href="/css/style.css">
+  <script src="/js/jquery-3.7.1.js"></script>
+  <script src="/js/bootstrap.bundle.js"></script>
+  <script src="/js/swiper-bundle.min.js"></script>
+</head>
+
+<body>
+  <?php include __DIR__ . '/components/navbar.php'; ?>
+
+  <!-- 轮播图 -->
+  <div class="swiper mySwiper" style="width: 100%; height: 400px;">
+    <div class="swiper-wrapper">
+      <?php foreach ($carouselItems as $item): ?>
+        <div class="swiper-slide">
+          <img src="<?php echo $item['image']; ?>" alt="<?php echo $item['alt']; ?>" style="width: 100%; height: 100%; object-fit: cover;">
+        </div>
+      <?php endforeach; ?>
+    </div>
+    <div class="swiper-pagination"></div>
+    <div class="swiper-button-prev"></div>
+    <div class="swiper-button-next"></div>
+  </div>
+
+  <!-- 主要内容 -->
+  <div class="main-content">
+    <div class="container">
+        <div class="row">
+          <!-- 左侧导航 -->
+          <div class="col col-sm-12 col-md-3 col-lg-3">
+            <div class="sidebar">
+              <div class="title">
+                <h2>搜索结果</h2>
+              </div>
+            </div>
+          </div>
+          
+          <!-- 右侧内容 -->
+          <div class="col col-sm-12 col-md-9 col-lg-9">
+            <div class="content">
+      <div class="section-title">
+        <h2 class="icon">搜索结果</h2>
+        
+        <nav aria-label="breadcrumb">
+          <ol class="breadcrumb">
+            <li class="breadcrumb-item"><a href="/">首页</a></li>
+            <li class="breadcrumb-item active" aria-current="page">搜索结果</li>
+          </ol>
+        </nav>
+      </div>
+              
+      <!-- 文章列表 -->
+      <div class="news-list">
+        <?php if (!empty($workUpdates)): ?>
+          <?php foreach ($workUpdates as $item): ?>
+            <?php
+            // 确保所有必要字段存在
+            $title = isset($item['title']) ? $item['title'] : '暂无标题';
+            $articleId = isset($item['id']) ? $item['id'] : '';
+            $date = isset($item['createtime']) ? date('Y-m-d', strtotime($item['createtime'])) : '未知日期';
+            ?>
+            <div class="news-item">
+              <a href="xinWenXiangQing.php?id=<?php echo $articleId; ?>&keyword=<?php echo urlencode($searchKeyword); ?>" class="title"><?php echo $title; ?></a>
+              <span class="date"><?php echo $date; ?></span>
+            </div>
+          <?php endforeach; ?>
+        <?php else: ?>
+          <div class="no-news">
+            <?php echo !empty($searchKeyword) ? '没有找到与"' . htmlspecialchars($searchKeyword) . '"相关的文章' : '暂无文章'; ?>
+          </div>
+        <?php endif; ?>
+      </div>
+              
+              <!-- 分页 -->
+      <nav aria-label="List Page navigation">
+        <ul class="pagination mt-4">
+          <li class="prev"><a href="search.php?page=<?php echo $currentPage > 1 ? $currentPage - 1 : 1; ?><?php echo !empty($searchKeyword) ? '&keyword=' . urlencode($searchKeyword) : ''; ?>">&lt;</a></li>
+          <?php for ($i = 1; $i <= $totalPages; $i++): ?>
+            <li class="<?php echo $i == $currentPage ? 'active' : ''; ?>"><a href="search.php?page=<?php echo $i; ?><?php echo !empty($searchKeyword) ? '&keyword=' . urlencode($searchKeyword) : ''; ?>"><?php echo $i; ?></a></li>
+          <?php endfor; ?>
+          <li class="next"><a href="search.php?page=<?php echo $currentPage < $totalPages ? $currentPage + 1 : $totalPages; ?><?php echo !empty($searchKeyword) ? '&keyword=' . urlencode($searchKeyword) : ''; ?>">&gt;</a></li>
+        </ul>
+      </nav>
+            </div>
+          </div>
+        </div>
+    </div>
+  </div>
+
+  <?php include __DIR__ . '/components/footer.php'; ?>
+
+  <script>
+    $(document).ready(function() {
+      // 初始化 Swiper
+      const swiper = new Swiper(".mySwiper", {
+        slidesPerView: 1,
+        spaceBetween: 30,
+        loop: true,
+        pagination: {
+          el: ".swiper-pagination",
+          clickable: true,
+        },
+        navigation: {
+          nextEl: ".swiper-button-next",
+          prevEl: ".swiper-button-prev",
+        },
+        autoplay: {
+          delay: 5000,
+          disableOnInteraction: false,
+        },
+      });
+      
+      // 为搜索输入框添加回车事件
+      $('#search-input').on('keypress', function(e) {
+        if (e.key === 'Enter') {
+          performSearch();
+        }
+      });
+    });
+    
+    // 执行搜索函数
+    function performSearch() {
+      const keyword = $('#search-input').val().trim();
+      location.href = 'search.php?page=1&keyword=' + encodeURIComponent(keyword);
+    }
+  </script>
+</body>
+
+</html>