Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:现在体会到了$_SERVER变量的强大作用吧, 里面的每一个键都值得反复研究, 许多功能 都要靠它来实现
1、代码(数据连接查询略)
<?php
$staffs=require 'contect.php';
// echo $pages;
// print_r($staffs);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>员工信息表</title>
<style>
body>div {
width: 700px;
margin: 0 auto;
outline: 1px solid red;
display: flex;
flex-flow: column nowrap;
align-items: center;
}
p {
height: 25px;
display: flex;
justify-content: flex-start;
align-items: center;
}
p a {
text-decoration: none;
width: 38px;
text-align: center;
margin: 0 3px;
}
.active {
background-color: tomato;
color: white;
}
</style>
</head>
<body>
<div>
<table border=1 cellspacing=0 cellpadding=10>
<caption style="font-size:28px">员工信息表</caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>职位</th>
<th>电话</th>
<th>操作</th>
</tr>
<?php foreach($staffs as $staff):?>
<tr>
<td><?php echo $staff['id'];?></td>
<td><?php echo $staff['name'];?></td>
<td><?php echo $staff['age'];?></td>
<td><?php echo $staff['sex'] ? '男' : '女';?></td>
<td><?php echo $staff['position'];?></td>
<td><?php echo $staff['mobile'];?></td>
<td><button>编辑</button><button>删除</button></td>
</tr>
<?php endforeach; ?>
</table>
<p>
<a href="demo.php?p=1">首页</a>
<?php
$current=filter_input(INPUT_GET,'p',FILTER_VALIDATE_INT);
// echo $current;
if($current<=1){
$prev=1;
}elseif($current>$pages){
$prev=$pages;
}
else{
$prev=$current-1;
}
if($current>=$pages){
$next=$pages;
}elseif($current<1){
$next=1;
}
else{
$next=$current+1;
}
?>
<a href="demo.php?p=<?php echo $prev ?>">前一页</a>
<?php for($i=1;$i<=$pages;$i++): ?>
<a href="demo.php?p=<?php echo $i;?>"
class='page <?php echo ($i===$current) ? 'active':null ?>'><?php echo $i;?></a>
<?php endfor; ?>
<a href="demo.php?p=<?php echo $next?>">下一页</a>
<a href="demo.php?p=<?php echo $pages?>">尾页</a>
</p>
<form action="demo.php" method='GET'>
<input type="number" name="p" min='1' max='<?php echo $pages ?>'>
<button>跳转</button>
</form>
<p>
<a href="demo.php?p=1">首页</a>
<?php
$current=filter_input(INPUT_GET,'p',FILTER_VALIDATE_INT);
// echo $current;
if($current<=1){
$prev=1;
}elseif($current>$pages){
$prev=$pages;
}
else{
$prev=$current-1;
}
if($current>=$pages){
$next=$pages;
}elseif($current<1){
$next=1;
}
else{
$next=$current+1;
}
?>
<a href="demo.php?p=<?php echo $prev ?>">前一页</a>
<?php $showpages=5; $offset=($showpages-1)/2;
if($current>$offset+1){
echo "<a href=''>……</a>";
$showstart=$current-$offset;
$showend=$current+$offset;
}
else{
$showstart=1;
$showend=$current+($offset+($offset-$current)+$showstart);
}
if($showend>$pages){
$showstart=$current-($offset+($showend-$pages));
$showend=$pages;
}
?>
<?php for($i=$showstart;$i<=$showend;$i++): ?>
<a href="demo.php?p=<?php echo $i;?>"
class='page <?php echo ($i===$current) ? 'active':null ?>'><?php echo $i;?></a>
<?php endfor; ?>
<?php if($showend<$pages){ echo "<a href=''>……</a>" ; } ?>
<a href="demo.php?p=<?php echo $next?>">下一页</a>
<a href="demo.php?p=<?php echo $pages?>">尾页</a>
</p>
</div>
</body>
</html>
2、演示结果:
1、数据库分页查询:SELECT * FROM
staffsLIMIT 5 OFFSET 0
;
(LIMIT 每页显示的记录数,OFFSET 偏移量=每页显示的数量 * (当前页数 - 1))
2、数据统计总数:SELECT COUNT('id') AS sum FROM `staffs
;
3、跳转页码:利用form中method=”GET”;设置input的name=”p”;即可实现跳转至输入页码;利用input中(min|max)来限制页码超出范围
4、利用showpages页码、开始页码、结束页码、和页码偏移量来结算显示省率号;
5、获取当前脚本地址:$_SERVER['PHP_SELF']