main
xgkblog/admin/posts.php
posts.php7 KB
<?php 
require 'config.php';

// ================= 1. 处理删除操作 =================
if (isset($_GET['delete_id'])) {
    $id = $_GET['delete_id'];
    $stmt = $pdo->prepare("DELETE FROM posts WHERE id = :id");
    $stmt->execute([':id' => $id]);
    header('Location: posts.php?msg=deleted'); 
    exit;
}

// ================= 2. 分页参数设置 =================
// 获取当前页码,默认为第 1 页
$current_page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
// 每页显示的条数,默认 10 条
$page_size = 10; 
// 计算偏移量 (OFFSET)
$offset = ($current_page - 1) * $page_size;

// ================= 3. 获取数据 =================
// 3.1 获取文章总数(用于计算总页数)
$total_sql = "SELECT COUNT(*) FROM posts";
$total_posts = $pdo->query($total_sql)->fetchColumn();
// 计算总页数,使用 ceil 向上取整
$total_pages = ceil($total_posts / $page_size);

// 3.2 获取当前页的文章列表(带分类信息)
$sql = "SELECT p.id, p.title, p.created_at, c.name as category_name 
        FROM posts p 
        LEFT JOIN categories c ON p.category_id = c.id 
        ORDER BY p.created_at DESC 
        LIMIT :limit OFFSET :offset";

$stmt = $pdo->prepare($sql);
$stmt->bindValue(':limit', $page_size, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$posts = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章管理</title>
    <style>
        /* 分页样式 */
        .pagination {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            gap: 8px;
            margin-top: 20px;
            font-size: 14px;
        }
        .pagination a, .pagination span {
            padding: 6px 12px;
            border-radius: 6px;
            text-decoration: none;
            color: #4a5568;
            background: #f0f4f8;
            transition: all 0.2s;
        }
        .pagination a:hover {
            background: #4a90e2;
            color: #fff;
        }
        .pagination .current-page {
            background: #4a90e2;
            color: #fff;
            font-weight: bold;
        }
        .pagination .disabled {
            color: #ccc;
            cursor: not-allowed;
            background: #f8f9fa;
        }
    </style>
</head>
<body>

    <div class="overlay" id="overlay"></div>

    <!-- 侧边栏 -->
    <?php require_once __DIR__ . '/views/sidebar.php'; ?>

    <!-- 主内容区 -->
    <div class="main-wrapper">
        <?php require_once __DIR__ . '/views/header.php'; ?>
        <main class="content-area">
            <div class="page-header">
                <h1 class="page-title">数据概览</h1>
                <button class="btn-primary"><a href="post_edit.php">+ 撰写新文章</a></button>
            </div>
            
            <!-- 统计卡片 -->
            <?php require_once __DIR__ . '/views/tj.php'; ?>

            <!-- 提示信息 -->
            <?php if(isset($_GET['msg'])): ?>
                <p style="color:green; font-weight:bold; margin-bottom:15px;">✅ 操作成功!</p>
            <?php endif; ?>

            <!-- 数据表格 -->
            <div class="card" style="padding: 0; overflow: hidden;">
                <div class="table-wrapper">
                    <table>
                        <thead>
                            <tr>
                                <th style="width: 40px;"><input type="checkbox" class="table-checkbox"></th>
                                <th style="width: 60px;">ID</th>
                                <th>标题</th>
                                <th>分类</th>
                                <th>发布时间</th>
                                <th style="width: 120px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($posts)): ?>
                                <?php foreach ($posts as $post): ?>
                                <tr>
                                    <td><input type="checkbox" class="table-checkbox"></td>
                                    <td><?php echo $post['id']; ?></td>
                                    <td>
                                        <div class="article-title"><?php echo htmlspecialchars($post['title']); ?></div>
                                    </td>
                                    <td>
                                        <div class="article-cat"><?php echo htmlspecialchars($post['category_name'] ?? '未分类'); ?></div>
                                    </td>
                                    <td><?php echo date('Y-m-d H:i', strtotime($post['created_at'])); ?></td>
                                    <td>
                                        <a href="post_edit.php?id=<?php echo $post['id']; ?>" class="action-btn" title="编辑">✏️</a>
                                        <a href="?delete_id=<?php echo $post['id']; ?>" class="action-btn" title="删除" onclick="return confirm('确定要删除吗?')">🗑️</a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="6" style="text-align:center; padding:30px; color:#999;">暂无文章数据</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- ================= 4. 分页导航 ================= -->
            <?php if ($total_pages > 1): ?>
            <div class="pagination">
                <!-- 上一页 -->
                <?php if ($current_page > 1): ?>
                    <a href="?page=<?php echo $current_page - 1; ?>">上一页</a>
                <?php else: ?>
                    <span class="disabled">上一页</span>
                <?php endif; ?>

                <!-- 页码显示 (这里简单展示,如果页数过多可以加上省略号逻辑) -->
                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <?php if ($i == $current_page): ?>
                        <span class="current-page"><?php echo $i; ?></span>
                    <?php else: ?>
                        <a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                    <?php endif; ?>
                <?php endfor; ?>

                <!-- 下一页 -->
                <?php if ($current_page < $total_pages): ?>
                    <a href="?page=<?php echo $current_page + 1; ?>">下一页</a>
                <?php else: ?>
                    <span class="disabled">下一页</span>
                <?php endif; ?>
            </div>
            <?php endif; ?>
          
        </main>
    </div>

    <?php require_once __DIR__ . '/views/footer.php'; ?>
</body>
</html>