Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
namespace _0819;
use PDO;
// 连接
$db = new PDO('mysql:dbname=phpedu', 'root', 'root');
// 1. 页数
$page = $_GET['p'] ?? 1;
echo '当前页数: p = ' . $page . '<br>';
// 2. 数量
$num = 5;
echo '当前数量: num = ' . $num . '<br>';
// 3. 偏移量 = (页数 - 1) * 数量
$offset = ($page - 1) * $num;
echo '当前偏移量: offset = ' . $offset . '<br>';
// 4. 计算总记录数
// SELECT CEIL(COUNT(*)/5) AS `total` FROM `staff`
// SELECT COUNT(*) AS `total` FROM `staff`
$sql = 'SELECT COUNT(*) AS `total` FROM `staff`';
$stmt = $db->prepare($sql);
$stmt->execute();
// 将总数量绑定到一个变量上
$stmt->bindColumn('total', $total);
$stmt->fetch();
echo '当前总记录数量: total = ' . $total . '<br>';
// 5. 计算总页数
// 向上取整
$pages = ceil($total / $num);
echo '当前总页数: pages = ' . $pages . '<br>';
$sql = <<< SQL
SELECT *
FROM `staff`
LIMIT $offset, $num;
SQL;
$stmt = $db->prepare($sql);
$stmt->execute();
$staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<hr>';
/**
* 生成分页码
*
* @param integer $page 当前页
* @param integer $pages 总页数
* @return array
*/
// function createPages(int $page, int $pages): array
// {
// 当前是第8页, 共计20页
// [1, ... 6, 7, 8, 9, 10, .... 20]
// 当前是第10页, 共计20页
// [1, ... 8, 9, 10, 11, 12, .... 20]
// 1. 生成与总页数长度相同的递增的整数数组
$pageArr = range(1, $pages);
// 2. 只需要当前和前后二页, 其它页码用 false/null 来标记
$paginate = array_map(function ($p) use ($page, $pages) {
return ($p == 1 || $p == $pages || abs($page - $p) <= 2) ? $p : null;
}, $pageArr);
// dump($paginate);
// 去重, 替换
$before = array_unique(array_slice($paginate, 0, $page));
$after = array_unique(array_slice($paginate, $page));
// 用解构进行合并
// return [...$before, ...$after];
$page_ret = [...$before, ...$after];
// }
<?php
require 'page.php';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP实现分页</title>
</head>
<style>
table {
width: 400px;
border-collapse: collapse;
text-align: center;
}
table th,
table td {
border: 1px solid;
padding: 5px;
}
table thead {
background-color: lightcyan;
}
table caption {
font-size: larger;
margin-bottom: 8px;
}
body>p {
display: flex;
}
p>a {
text-decoration: none;
color: #555;
border: 1px solid;
padding: 5px 10px;
margin: 10px 2px;
}
.active {
background-color: seagreen;
color: white;
border: 1px solid seagreen;
}
</style>
<body>
<table>
<caption>员工信息表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
<?php foreach ($staffs as $staff) : extract($staff) ?>
<?php $sex = $sex ? '男' : '女';
?>
<tr>
<td><?= $id ?>
</td>
<td><?= $name ?>
</td>
<td><?= $sex ?>
</td>
<td><?= $email ?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<p>
<?php
$a = $page - 1;
if ($page <= 0) {
echo "<script>alert('前面没有了');history.go(-1);</script>";
$a = 1;
}
$dqpage = $_SERVER['PHP_SELF'] . '?p=' . $a;
?>
<a href="<?= $dqpage ?>">上一页</a>
<?php foreach ($page_ret as $k => $v) : ?>
<?php
// 页码跳转的url
if ($v === null) {
$v = '...';
}
if ($v === '...') {
$url = '#';
} else {
$url = $_SERVER['PHP_SELF'] . '?p=' . $v;
}
// 实现页码高亮
$page = $_GET['p'] ?? 1;
$active = ($v == $page) ? 'active' : null;
?>
<a href="<?= $url ?>" class="<?= $active ?>"><?= $v ?></a>
<?php endforeach ?>
<?php
$b = $page + 1;
if ($page > $pages) {
echo "<script>alert('后面没有了');history.go(-1);</script>";
$a = $pages;
}
$dqpage = $_SERVER['PHP_SELF'] . '?p=' . $b;
?>
<a href="<?= $dqpage ?>">下一页</a>
</p>
</body>
</html>