Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
namespace _0819;
use PDO;
// 连接数据库
$db = new PDO('mysql:dbname=phpedu;port=3308', 'user', '123456');
//获取url的页码
$page = $_GET['p'] ?? 1;
//每页显示的记录数
$num = 5;
//当前页的起始记录数
$offset = ($page - 1) * $num;
//计算总记录数
$sql = 'SELECT COUNT(*) AS `total` FROM `staff`';
$stmt = $db->prepare($sql);
$stmt->execute();
// 将总数量绑定到一个变量上
$stmt->bindColumn('total', $total);
$stmt->fetch();
// 向上取整总页数
$pages = ceil($total / $num);
//包含分页接口Page
include 'Page.php';
//调用Page接口的静态方法clickPage();
$arr = \_0819\Page::clickPage('http://www.test.io/作业/PHP编程/0819/index.php',$page,$pages);
$sql = <<< SQL
SELECT *
FROM `staff`
LIMIT $offset, $num;
SQL;
$stmt = $db->prepare($sql);
$stmt->execute();
$staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($staffs) === 0) {
echo '查询结果为空';
}
?>
<!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;
margin: auto;
}
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;
place-content: center;
margin: auto;
}
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 ($staffs as $staff) :extract($staff) ?>
<tr>
<td><?=$id?>
</td>
<td><?=$name?>
</td>
<td><?=$sex?>
</td>
<td><?=$email?>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<p>
<?php
echo $arr;
?>
</p>
</body>
</html>
<?php
namespace _0819;
//分页功能类
class Page
{
/**
* 分页功能:仿PHP中文网分页
* @param string $url 基本链接('http://www.test.io/测试/demo.php')
* @param int $page = 1,当前页码,默认第1页
* @param int $pages 总页数。
* @return string
*/
public static function clickPage(string $url,int $page = 1,int $pages):string
{
//通过总页娄生成数组
$pageArr = range(1,$pages);
//首尾页数必须显示,当前页和前后两页显示
$pageDisplay = array_map(function($p) use($page,$pages){
//把其他不显示的元素置为null
return ($p === 1 || $p === $pages || abs($page-$p) <= 2)? $p : null;
},$pageArr);
//以当前页为分界线,各自只保留一个null,
$before = array_unique(array_slice($pageDisplay,0,$page));
$after = array_unique(array_slice($pageDisplay,$page));
//合并前后两个数组
$pageArr = array_merge($before,$after);
//判断上一页的值
$prePage = ($page - 1) >1 ?($page - 1): 1;
//判断下一页的值
$nextPage = ($page + 1) < $pages ? ($page + 1): $pages;
//并接上一页的html代码
$preUrl = $url.'?p='.$prePage;
$str = <<< Str
<a href="$preUrl" >上一页</a>
Str;
//以数组的值来判断是否显示。
//如果是当前页就给颜色
for($i = 0; $i < count($pageArr);$i++){
$active = "";
if($pageArr[$i]){
if($page === $pageArr[$i]){
$active = "active";
}
$currentUrl = $url.'?p='.$pageArr[$i];
$str .="<a href=$currentUrl class=$active >$pageArr[$i]</a>";
}else{
$str .="<a>...</a>";
}
}
//并接上一页的html代码
$nextUrl = $url.'?p='.$nextPage;
$str .= <<< Str
<a href="$nextUrl" >下一页</a>
Str;
return $str;
}
}