:::

1. index.php 首頁檔

<?php
use JasonGrimes\Paginator;

require_once "header.php";

// 過濾外來變數
$op      = isset($_REQUEST['op']) ? filter_var($_REQUEST['op'], FILTER_SANITIZE_STRING) : 'index';
$p       = isset($_REQUEST['p']) ? (int) $_REQUEST['p'] : 1;
$id      = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
$cate_id = isset($_REQUEST['cate_id']) ? (int) $_REQUEST['cate_id'] : 0;
$keyword = isset($_REQUEST['keyword']) ? filter_var($_REQUEST['keyword'], FILTER_SANITIZE_STRING) : '';
$year    = isset($_REQUEST['year']) ? (int) $_REQUEST['year'] : 0;

// 流程控制
switch ($op) {
    case "show":
        add_count($id);
        $news = show($id);
        $smarty->assign('news', $news);
        break;

    default:
        $all_news = index($p, $cate_id, $keyword, $year);
        $smarty->assign('all_news', $all_news, );
        break;
}

$smarty->assign('year', $year);
$smarty->assign('cate_id', $cate_id);
$smarty->assign('op', $op);
$smarty->display('index.tpl');

// 點閱數+1
function add_count($id)
{
    global $db;
    $sql = "UPDATE `articles` SET `counter` = `counter` + 1
    WHERE `id` = ?";
    $sth    = $db->prepare($sql);
    $values = [$id];
    $sth->execute($values);
}

// 列出所有文章
function index($p = 1, $cate_id = 0, $keyword = '', $year = 0)
{
    global $db;
    // 將抓出的資料陣列放到 $news 陣列中
    $all_news = $value = [];
    $where    = '';

    // 是否要分類
    if ($cate_id) {
        $where = "WHERE `cate_id`= ?";
        $value = [$cate_id];
    } elseif ($keyword) {
        $where = "WHERE `title` like ? or `content` like ? or `info` like ? or `date` like ?";
        $value = ["%{$keyword}%", "%{$keyword}%", "%{$keyword}%", "%{$keyword}%"];
    } elseif ($year) {
        $where = "WHERE `date` like ? ";
        $value = ["{$year}%"];
    }

    // 讀取資料庫(抓出全部資料數)
    $sql = "SELECT count(*) FROM `articles` $where ";
    $sth = $db->prepare($sql);
    $sth->execute($value);
    list($total) = $sth->fetch(PDO::FETCH_NUM);

    // 分頁資料
    $paginator              = new Paginator($total, _NEWS_PER_PAGE, $p, "index.php?p=(:num)&cate_id=$cate_id&keyword=$keyword&year=$year");
    $all_news['next_pages'] = $paginator->getNextUrl(); // 下一頁網址
    $all_news['prev_pages'] = $paginator->getPrevUrl(); // 上一頁網址
    $all_news['pages']      = $paginator->getPages(); // 分頁工具所需資料

    // 計算從哪筆資料開始讀取
    $start = ($p - 1) * _NEWS_PER_PAGE;

    // 讀取資料庫(抓出全部資料)
    $sql = "SELECT * FROM `articles` $where ORDER BY `date` DESC LIMIT ?, ?";
    $sth = $db->prepare($sql);

    $value[] = $start;
    $value[] = _NEWS_PER_PAGE;
    $sth->execute($value);

    $all_news['data'] = [];
    while ($news = $sth->fetch(PDO::FETCH_ASSOC)) {

        $news['summary'] = get_summary($news['content']);
        $news['files']   = get_files($news['id']);

        // 過濾整個陣列
        $all_news['data'][] = filter_var_array($news, FILTER_SANITIZE_STRING);
    }

    return $all_news;
}

// 取得摘要
function get_summary($content)
{
    // 若是本文超過130個字就加上...
    $more = mb_strlen($content) > _SUMMARY_COUNT ? '...' : '';

    // 產生摘要
    $summary = mb_substr($content, 0, _SUMMARY_COUNT) . $more;
    return $summary;
}

 


:::

書籍目錄

展開 | 闔起

快速登入


https%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1864%26tbsn%3D53

計數器

今天: 5771577157715771
昨天: 2489248924892489
總計: 8027753802775380277538027753802775380277538027753