分页查询的原理,使用select查询语句后面加上LIMIT参数,有两个值,第一个偏移量第二个是查询显示的数量,
如果能控制每页显示的数量就可以实现分页查询,使用GET来进行传递,用字符串'p'表示当前的页数.
偏移量的计算方法为:起始的偏移量=(当前的页数-1)*要显示的数量
以下是我的代码
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()
{
//如果url中存在页码变量p则取之,否则默认为1,即第一页
return isset($_GET['p']) ? $_GET['p'] : 1;
}
//获取总页数
public function getPages($table)
{
//因为是读操作,所有必须使用count()获取,不允许通过rowCount()进行判断
$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);
}
}
//实例化分页类
$page = new Page();
//连接数据库
$page->connect('mysql','localhost','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">
<style>
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;
}
</style>
<title>MVC简介</title>
</head>
<body>
<table>
<caption>员工信息表</caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>工资</th>
</tr>
<?php foreach ($data 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['salary'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<h3>
<!-- 当前是第一页的时候,上一页和首页链接应该不显示-->
<?php if($currentPage != 1): ?>
<a href="http://www.123.com/2018-9-7/2018-9-7.php?p=1">首页</a>
<a href="http://www.123.com/2018-9-7/2018-9-7.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="http://www.123.com/2018-9-7/2018-9-7.php?p=<?php echo $i ?>"><?php echo $i ?></a>
<?php endfor; ?>
<!--当前已经是最后一页的时候,下一页和最后一页也应该不显示-->
<?php if($currentPage != $totalPages) :?>
<a href="http://www.123.com/2018-9-7/2018-9-7.php?p=<?php echo $currentPage+1; ?>">下一页</a>
<a href="http://www.123.com/2018-9-7/2018-9-7.php?p=<?php echo $totalPages; ?>">尾页</a>
<?php endif; ?>
<!--实现页面的快速跳转-->
<form action="" method="get">
<input type="text" style="width: 45px;" name="p" id="p" pattern="^[0-9]*$" value="">
<button>跳转</button>
</form>
</h3>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号