批改状态:合格
老师批语:
1 , 分页查询的原理与偏移量的计算方法
根据理解 , offset设置偏移量原理是需要查询的页数减一再乘以自己需要每页做显示的数量 ;
即offset有两个参数 ,第一个是偏移量 $page - 1 , 第二个是需要显示的数量 $num ;
当然分页查询还需要其它的相关条件 , 包括总数量 , 页数 , 中间页码显示值 ;
其中中间页面用 for 语句遍历出来即可 for & endfor , 条件是 $i =1; $i <=总页数$pages ; $i++ ;
2 , 编程 : 实现分页查询,要求有上一下,下一页,直接跳到首页和尾页,中间页的生成,以及快速页码跳转功能
<?php
namespace model;
class Page
{
private $offset;
private $num;
private $pdo;
public function __construct($num=3)
{
$this ->num = $num;
$this ->offset = ($this ->getPage()-1) * $this ->num;
}
public function connect($type,$host,$dbname,$user,$pass)
{
try{
$this ->pdo = new \PDO("$type:host=$host;dbname=$dbname",$user,$pass);
}catch(\PDOException $e){
die($e ->getMessage());
}
}
public function getPage()
{
return isset($_GET['p']) ? $_GET['p'] : 1;
}
public function getPages($table)
{
$stmt = $this ->pdo ->prepare("SELECT COUNT(*) FROM {$table}");
$stmt ->execute();
$total = $stmt ->fetchColumn(0);
return ceil($total / $this ->num);
}
public function getData($table)
{
$sql = "SELECT * FROM {$table} LIMIT {$this ->offset} , {$this ->num}";
$stmt = $this ->pdo ->prepare($sql);
$stmt ->execute();
return $stmt ->fetchALL(\PDO::FETCH_ASSOC);
}
}table,th,td {
border: 1px solid black;
}
table th {
background-color: lightskyblue;
}
table {
border-collapse: collapse;
width: 70%;
margin: 30px auto;
text-align: center;
}
table caption {
font-size: 1.5rem;
margin-bottom: 15px;
}
h3 {
text-align: center;
}
h3 a {
text-decoration: none;
margin-left: 10px;
border: 1px solid black;
display: inline-block;
height: 30px;
min-width: 30px;
padding: 0 10px;
background-color: lightgreen;
}
h3 a:hover{
background-color: red;
color: white;
}
.active {
background-color: red;
}
form {
display: inline;
}<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<title>分页查询</title>
</head>
<body>
<?php
require 'Page.php';
use model\Page;
$page = new Page();
$page ->connect('mysql','127.0.0.1','php','root','root');
$currentPage = $page ->getPage();
$totalPages = $page ->getPages('staff');
$data = $page ->getData('staff');
?>
<table>
<caption>员工信息表</caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>工资</th>
</tr>
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['age']; ?></td>
<td><?php echo $row['sex']?'男':'女'; ?></td>
<td><?php echo $row['salary']; ?></td>
</tr>
<?php endforeach;?>
</table>
<h3>
<!-- 当前是第一页的时候,上一页和首页链接应该不显示-->
<?php if($currentPage != 1): ?>
<a href="task.php?p=1">首页</a>
<a href="task.php?p=<?php echo $currentPage-1; ?>">上一页</a>
<?php endif; ?>
<!--生成中间页码-->
<?php for($i=1; $i<=$totalPages; $i++): ?>
<!------高亮显示当前页码----------->
<a class="<?php if($currentPage==$i){echo 'active';}?>" href="task.php?p=<?php echo $i ?>"><?php echo $i ?></a>
<?php endfor; ?>
<!--当前已经是最后一页的时候,下一页和最后一页也应该不显示-->
<?php if($currentPage != $totalPages) :?>
<a href="task.php?p=<?php echo $currentPage+1; ?>">下一页</a>
<a href="task.php?p=<?php echo $totalPages; ?>">尾页</a>
<?php endif; ?>
<!--实现页面的快速跳转-->
<form action="" method="get">
第
<select name="p" id="">
<?php for($i=1; $i<=$totalPages; $i++): ?>
<!-- 循环输出全部页码,并锁定当前页-->
<option value="<?php echo $i; ?>" <?php if($currentPage==$i){echo 'selected';} ?>><?php echo $i; ?></option>
<?php endfor; ?>
</select>
页
<button>跳转</button>
</form>
</h3>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号