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. 计算总记录数
$sql = 'SELECT COUNT(*) AS `total` FROM `user`';
$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 `user`
LIMIT $offset, $num;
SQL;
$stmt = $db->prepare($sql);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<hr>';
if (count($users) === 0) {
echo '查询结果为空';
} else {
foreach ($users as $user) {
extract($user); // $id,$name,$age,$email
printf('%d-%s-%s-%s<br>', $id, $name, $age, $email);
}
}
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);
// 去重, 替换
//处理的是当前页码左边显示上一页的页码(替换掉数组中null的元素)
$before = array_unique(array_slice($paginate, 0, $page));
//处理的是当前页码右边显示下一页的页码(替换掉数组中null的元素)
$after = array_unique(array_slice($paginate,$page));
//上一页
if($page>1) $pre = $page-1;
//下一页
if($page!=$pages) $next = $page+1;
// 用解构进行合并
// return [$pre,...$before, ...$after,$next];
//对返回值进行重构
return [$pre,[...$before, ...$after],$next];
}
<?php
require 'page_refer.php';
require 'implement.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>分页展示数据</title>
<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>
</head>
<body>
<table>
<caption>员工信息表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user) :extract($user) ?>
<tr>
<td><?=$id?>
</td>
<td><?=$name?>
</td>
<td><?=$age?>
</td>
<td><?=$email?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<p>
<?php
//利用当前url的页数与总页数对比,来判断
if($page<= $pages){
$url = $_SERVER['PHP_SELF'] . '?p=';
$page_nums =createPages($page,$pages);
foreach($page_nums as $k=>$v){
if($k==0 ){
if($v !=null){
echo "<a href='$url$v' >上一页</a>";
}
}
if($k==1){
foreach($v as $v1){
$active = ($v1 == $page) ? 'active' : '';
if($v1 == null){ echo "<a>....</a>";}else{
echo "<a href='$url$v1' class='$active'>$v1</a>";
}
}
}
if($k==2 ){
if($v !=null){
echo "<a href='$url$v' >下一页</a>";
}
}
}
}
?>
</p>
</body>
</html>