:::

2. header.php 共同要執行的程式

<?php
session_start();

// 自動載入Composer的物件
require_once 'vendor/autoload.php';
// 載入設定檔
require_once "config.php";
// 載入共同的函式檔
require_once "function.php";

// 連線資料庫
try
{
    $db = new PDO("mysql:host={$dbhost};dbname={$dbname};charset={$dbcharacter}", $dbuser, $dbpasswd);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //禁用prepared statements的模擬效果
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //讓資料庫顯示錯誤原因
    // echo "連線成功";
} catch (PDOException $e) {
    die("無法連上資料庫:" . $e->getMessage());
}

// 佈景引擎
$smarty = new \Smarty;
$smarty->setTemplateDir(_PATH . '/templates/');
$smarty->setCompileDir(_PATH . '/templates_c/');
$smarty->setConfigDir(_PATH . '/configs/');
$smarty->setCacheDir(_PATH . '/cache/');

// 將網址及實體路徑送到樣板
$smarty->assign('path', _PATH);
$smarty->assign('url', _URL);

// 文章分類
$smarty->assign('categories', $categories);

// 顯示年度文章
$smarty->assign('article_count', article_count());

$is_admin = isset($_SESSION['is_admin']) ? $_SESSION['is_admin'] : false;
$smarty->assign('is_admin', $is_admin);

// 年度文章數
function article_count()
{
    global $db;
    $sql = "SELECT COUNT(*) as count, LEFT(`date`,4) as year
    FROM `articles`
    GROUP BY `year`
    ORDER BY `year` DESC";
    $sth = $db->prepare($sql);
    $sth->execute();
    $data = [];
    while ($year_count = $sth->fetch(PDO::FETCH_ASSOC)) {
        $data[] = $year_count;
    }
    return $data;
}

 


:::

書籍目錄

展開 | 闔起

快速登入


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

計數器

今天: 6582658265826582
昨天: 2489248924892489
總計: 8028564802856480285648028564802856480285648028564