:::

6-5 讀出指定年度的文章

  1. 由於選項上有連結 index.php?year={$year},所以,點了之後,網址會變成如:index.php?year=2023,也就是我們要讓系統只列出2023年文章即可
  2. 由於有新傳入外來變數到 index.php ,所以要新增過濾 $year
    // 變數過濾
    $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
    $op = isset($_REQUEST['op']) ? htmlspecialchars($_REQUEST['op']) : 'index';
    $category = isset($_REQUEST['category']) ? (int) $_REQUEST['category'] : 0;
    $keyword = isset($_REQUEST['keyword']) ? htmlspecialchars($_REQUEST['keyword']) : '';
    $year = isset($_REQUEST['year']) ? (int) $_REQUEST['year'] : 0;

     

  3. index.php 的流程中 index()  函式加入 $year 參數
    // 預設為文章列表
    default:
        list($all_news, $paginator) = index($category, $year, $keyword, $perPage);
        $smarty->assign('all_news', $all_news);
        $smarty->assign('paginator', $paginator);
        break;

     

  4. 修改 index.php index() 函式,年度搜尋和關鍵字搜尋其實很類似,只不過只要搜尋 date 欄位即可,而且,年度一定在左邊,只有右邊日期會不同,所以把 % 萬用字元放到右邊即可。
    // 列出所有文章
    function index($category = 0, $year = 0, $keyword = '', $perPage = 10)
    {
        global $pdo;
        // 計算總資料數
        try {
            if ($year) {
                $stmt = $pdo->prepare('SELECT count(*) FROM `news` WHERE `date` LIKE ?');
                $stmt->execute(["$year%"]);
            } elseif ($keyword) {
                $stmt = $pdo->prepare('SELECT count(*) FROM `news` WHERE `title` LIKE ? OR `content` LIKE ? OR `author` LIKE ?');
                $stmt->execute(["%$keyword%", "%$keyword%", "%$keyword%"]);
            } elseif ($category) {
                $stmt = $pdo->prepare('SELECT count(*) FROM `news` WHERE `category`=?');
                $stmt->execute([$category]);
            } else {
                $stmt = $pdo->prepare('SELECT count(*) FROM `news`');
                $stmt->execute();
            }
            list($totalCount) = $stmt->fetch(PDO::FETCH_NUM);
            // 產生分頁物件
            $pagination = new Pagination(['totalCount' => $totalCount, 'perPage' => $perPage]);
            // 產生分頁工具列
            ob_start();
            Paginator::widget(['pagination' => $pagination]);
            $paginator = ob_get_clean();
        } catch (PDOException $e) {
            die("計算總資料數失敗:" . $e->getMessage());
        }
    
        try {
            if ($year) {
                $stmt = $pdo->prepare('SELECT * FROM `news` WHERE `date` LIKE ? LIMIT ?, ?');
                $stmt->execute(["$year%", $pagination->offset, $pagination->limit]);
            } elseif ($keyword) {
                $stmt = $pdo->prepare('SELECT * FROM `news` WHERE `title` LIKE ? OR `content` LIKE ? OR `author` LIKE ? ORDER BY `date` DESC LIMIT ?, ?');
                $stmt->execute(["%$keyword%", "%$keyword%", "%$keyword%", $pagination->offset, $pagination->limit]);
            } elseif ($category) {
                $stmt = $pdo->prepare('SELECT * FROM `news` WHERE `category`=? ORDER BY `date` DESC LIMIT ?, ?');
                $stmt->execute([$category, $pagination->offset, $pagination->limit]);
            } else {
                $stmt = $pdo->prepare('SELECT * FROM `news` ORDER BY `date` DESC LIMIT ?, ?');
                $stmt->execute([$pagination->offset, $pagination->limit]);
            }
            $all_news = [];
            $news = $stmt->fetchAll();
            foreach ($news as $row) {
                $media = $row['media'];
                unset($row['media']);
                $row = array_map('htmlspecialchars', $row);
                $row['files'] = json_decode($media, true);
                $all_news[] = $row;
            }
    
        } catch (PDOException $e) {
            echo "資料庫連接錯誤:" . $e->getMessage();
        }
        return [$all_news, $paginator];
    }
  5. 如此就大功告成了!
  6. 前台顯示頁面至此算是完成了!

:::

書籍目錄

展開 | 闔起

快速登入


http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1846%26tbsn%3D52

計數器

今天: 1660166016601660
昨天: 4159415941594159
總計: 8069178806917880691788069178806917880691788069178