33 lines
2.1 KiB
PHP
33 lines
2.1 KiB
PHP
<?php
|
|
require_once __DIR__ . "/includes/config.php";
|
|
require_once __DIR__ . "/includes/functions.php";
|
|
$id = isset($_GET["id"]) ? (int)$_GET["id"] : 0;
|
|
$c = db()->prepare("SELECT * FROM categories WHERE id=?"); $c->execute([$id]); $cat = $c->fetch();
|
|
if (!$cat) { header("Location: " . SITE_URL . "index.php"); exit; }
|
|
$pageName = "category";
|
|
$curCategory = $id;
|
|
$pageTitle = $cat["name"];
|
|
record_visit("category/" . $id);
|
|
$perPage = 5;
|
|
$st = db()->prepare("SELECT COUNT(*) c FROM articles WHERE category_id=? AND status=?"); $st->execute([$id,"published"]);
|
|
$pg = paginate((int)$st->fetch()["c"], isset($_GET["page"])?(int)$_GET["page"]:1, $perPage);
|
|
$stmt = db()->prepare("SELECT a.* FROM articles a WHERE a.category_id=? AND a.status=? ORDER BY a.created_at DESC LIMIT ? OFFSET ?");
|
|
$stmt->bindValue(1,$id,PDO::PARAM_INT); $stmt->bindValue(2,"published",PDO::PARAM_STR);
|
|
$stmt->bindValue(3,$pg["perPage"],PDO::PARAM_INT); $stmt->bindValue(4,$pg["offset"],PDO::PARAM_INT);
|
|
$stmt->execute(); $articles = $stmt->fetchAll();
|
|
require_once __DIR__ . "/includes/header.php";
|
|
?>
|
|
<div class="page-head">
|
|
<h1><?php echo e($cat["name"]); ?></h1>
|
|
<div class="desc">分类下的文章</div>
|
|
</div>
|
|
<?php if (!$articles) { ?><div class="card glass" style="text-align:center;color:var(--text-muted);padding:60px;">该分类暂无文章</div><?php } ?>
|
|
<?php foreach ($articles as $a) { ?>
|
|
<article class="card glass clickable" onclick="location.href='<?php echo e(SITE_URL); ?>article.php?id=<?php echo (int)$a["id"]; ?>'">
|
|
<div class="card-meta"><span><?php echo e(substr($a["created_at"],0,10)); ?></span><span><?php echo (int)$a["views"]; ?> 次阅读</span></div>
|
|
<h2><?php echo e($a["title"]); ?></h2>
|
|
<p><?php echo e(mb_strimwidth(strip_tags($a["excerpt"] ?: $a["content"]),0,120,"...")); ?></p>
|
|
</article>
|
|
<?php } ?>
|
|
<?php if ($pg["pages"]>1) { ?><div class="pagination"><?php for($i=1;$i<=$pg["pages"];$i++){ ?><a class="page-btn<?php echo $i===$pg["page"]?" active":""; ?>" href="?id=<?php echo $id; ?>&page=<?php echo $i; ?>"><?php echo $i; ?></a><?php } ?></div><?php } ?>
|
|
<?php require_once __DIR__ . "/includes/footer.php"; ?>
|