<?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;
}