批改状态:合格
老师批语:
分页查询的原理与偏移量的计算方法
分析分页的原理:
1. LIMIT 参数的作用: 偏移量与显示数量
2. 如果控制每页显示的数量
3. 接收GET参数,用p表示当前页数,每页显示3条
4. 需要的参数:
(1).totalPage 总页数
(2).totalNumber 一共有多少条数据
(3).pageSize 每页显示多少条数据
(4)currentPage 当前第几页
(5)*.rangeStart 起始页
(6)*.rangeEnd 末页
5. 当前偏移量的计算公式: (页数-1)*每页显示的数量
offset = (page-1)*num
实现分页查询,要求有上一下,下一页,直接跳到首页和尾页,中间页的生成,以及快速页码跳转功能
<?php
namespace model;
class Page
{
//查询起始偏移量
private $offset;
//每页记录数量
private $num;
//数据库连接对象
private $pdo;
//构造方法
public function __construct($num=5)
{
//初始化每页记录数量
$this->num = $num;
//查询起始偏移量: (页码-1)*数量
$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)
{
//因为是读操作,所有必须使用count()获取
$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM `{$table}`");
//执行
$stmt->execute();
//获取结果集中的一行一列,即记录总数
$total = $stmt->fetchColumn(0);
// ceil()是向上取整函数,即当前页哪怕只有一条记录,也必须输出一个页面
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);
}
}点击 "运行实例" 按钮查看在线实例
<?php
//导入分页类
require 'inc/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');
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>封装分页,实现代码复用</title>
<link rel="stylesheet" href="inc/bootstrap/css/bootstrap.min.css">
<script src="inc/jquery/jquery.js"></script>
<script src="inc/bootstrap/js/bootstrap.min.js"></script>
<style>
table tr th{text-align: center;}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<table class="table table-bordered table-hover text-center">
<caption class="h2 text-center">员工信息</caption>
<tr class="info">
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>工资</th>
</tr>
<?php foreach ($data as $row):?>
<tr>
<td><?php echo $row['staff_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>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2 text-center">
<nav>
<ul class="pagination">
<!-- 当前是第一页的时候,上一页和首页链接应该不显示-->
<?php if($currentPage !=1): ?>
<li><a href="http://zaza.com/0910/作业/作业.php?p=1">首页</a></li>
<li><a href="http://zaza.com/0910/作业/作业.php?p=<?php echo $currentPage-1;?>">上一页</a></li>
<?php endif; ?>
<!-- 生成中间页码-->
<?php for ($i=1; $i<=$totalPages;$i++): ?>
<li><a class="<?php if ($currentPage == $i){echo 'active';}?>" href="http://zaza.com/0910/作业/作业.php?p=<?php echo $i ?>"><?php echo $i ?></a></li>
<?php endfor; ?>
<!-- 当前已经是最后一页的时候,下一页和最后一页也应该不显示-->
<?php if ($currentPage != $totalPages): ?>
<li><a href="http://zaza.com/0910/作业/作业.php?p=<?php echo $currentPage+1; ?>">下一页</a>
</li>
<li><a href="http://zaza.com/0910/作业/作业.php?p=<?php echo $totalPages; ?>">尾页</a></li>
<?php endif; ?>
</ul>
<h4 style="display: inline">
<!-- 实现页面的快速跳转-->
<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>
</h4>
</nav>
</div>
</div>
</body>
</html>点击 "运行实例" 按钮查看在线实例

总结:好久没写总结,PHP要逻辑性好强,要基础很好才能玩好
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号