Files
myblog/admin/article_list.php
2026-09-09 19:30:18 +08:00

48 lines
2.3 KiB
PHP

<?php
require_once __DIR__ . "/../includes/functions.php";
require_once __DIR__ . "/../includes/auth.php";
$adminPage = "articles";
$pageTitle = "文章管理";
require __DIR__ . "/_layout.php";
$page = isset($_GET["page"]) ? max(1,(int)$_GET["page"]) : 1;
$per = 15;
$total = (int)db()->query("SELECT COUNT(*) FROM articles")->fetchColumn();
$pages = max(1,(int)ceil($total/$per));
$page = min($page,$pages);
$off = ($page-1)*$per;
$stmt = db()->prepare("SELECT a.id,a.title,a.views,a.created_at,c.name AS cat, COUNT(t.id) AS tagcount FROM articles a LEFT JOIN categories c ON a.category_id=c.id LEFT JOIN article_tags at ON at.article_id=a.id LEFT JOIN tags t ON t.id=at.tag_id GROUP BY a.id ORDER BY a.created_at DESC LIMIT ? OFFSET ?");
$stmt->bindValue(1,$per,PDO::PARAM_INT);
$stmt->bindValue(2,$off,PDO::PARAM_INT);
$stmt->execute();
$list = $stmt->fetchAll();
?>
<div class="admin-head">
<h1>文章管理</h1>
<a class="glass-btn primary" href="article_edit.php">写文章</a>
</div>
<?php if (isset($_GET["deleted"])) { ?><div class="msg ok">删除成功</div><?php } ?>
<div class="card glass">
<table class="admin-table">
<thead><tr><th style="width:40px">ID</th><th class="title">标题</th><th>分类</th><th>标签</th><th>阅读</th><th>日期</th><th>操作</th></tr></thead>
<tbody>
<?php if (!$list): ?><tr><td colspan="7" class="text-muted">暂无文章</td></tr><?php endif; ?>
<?php foreach ($list as $a): ?>
<tr>
<td><?php echo $a["id"]; ?></td>
<td class="title"><?php echo e($a["title"]); ?></td>
<td class="text-muted"><?php echo e($a["cat"] ?: "未分类"); ?></td>
<td><span class="badge-count"><?php echo $a["tagcount"]; ?></span></td>
<td><?php echo $a["views"]; ?></td>
<td class="text-muted"><?php echo e(substr($a["created_at"],0,10)); ?></td>
<td><div class="actions"><a href="article_edit.php?id=<?php echo $a["id"]; ?>">编辑</a><a href="article_delete.php?id=<?php echo $a["id"]; ?>" data-confirm="确定删除该文章?">删除</a></div></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="pagination">
<?php for($i=1;$i<=$pages;$i++): ?><a class="<?php echo $i===$page?"current":""; ?>" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a><?php endfor; ?>
</div>
</div>
<?php require __DIR__ . "/_tail.php"; ?>