1问答:分页查询的原理与偏移量的计算方法
(1)LIMIT 参数的作用: 偏移量offset与显示数量pageSize
(2) 控制每页显示的数量pageSize
(3) 接收GET参数,用p表示当前页数
(4) 需要的参数:
1).totalPage 总页数
2).totalNumber 一共有多少条数据
3).pageSize 每页显示多少条数据
4)currentPage 当前第几页
当前偏移量的计算公式: (页数-1)*每页显示的数量
offset = (currentPage-1)*pageSize
2、编程: 实现分页查询,要求有上一下,下一页,直接跳到首页和尾页,中间页的生成,以及快速页码跳转功能
(1)类封装
<?php
//查询分页类
namespace model;
class Page{
//查询起始偏移量
private $offset;
//每页记录数
private $pageNum;
//数据库连接对象
private $pdo=null;
//构造方法
public function __construct($num=3) {
$this->pageNum=$num;
$this->offset= ($this->getNum()-1)*$this->pageNum;
}
//连接数据库
public function connect($type,$host,$dbname,$user,$pass){
try {
$this->pdo = new \PDO("{$type}:host={$host};dbname={$dbname}", $user, $pass);
} catch (\PDOException $e) {
echo $e->getMessage();
}
}
//获取当前页码
public function getNum(){
return isset($_GET['p'])?$_GET['p']:1;
}
//获取总页数
public function getTotalNum($table){
//$stmt= $this->pdo->prepare("SELECT COUNT(*) FROM `{$table}`");
$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM `{$table}` ");
$stmt->execute();
$total=$stmt->fetchColumn(0);
return ceil($total/$this->pageNum);
}
//获取分页数据
public function getCurrentPage($table) {
$sql="SELECT *FROM {$table} LIMIT {$this->offset},{$this->pageNum}";
$stmt= $this->pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
}点击 "运行实例" 按钮查看在线实例
(2)分页查询
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="./inc/style.css"/>
<title>手工分页查询</title>
</head>
<body>
<?php
require 'inc/Page.php';
use model\Page;
define('JUMP_URL',basename($_SERVER['PHP_SELF']));
$page=new Page(4);
//连接数据库
$page->connect("mysql", "127.0.0.1", "php", "root", "root123");
//获取当前页
$currentPage=$page->getNum();
//获取总页数
$totalNum=$page->getTotalNum("staff");
//获取分页数据
$data=$page->getCurrentPage("staff");
?>
<table>
<caption>信息</caption>
<tr>
<th>ID</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>数字</th>
</tr>
<?php foreach ($data as $rows):?>
<tr>
<th><?php echo $rows['staff_id']?></th>
<th><?php echo $rows['name']?></th>
<th><?php echo $rows['age'] ?></th>
<th><?php echo $rows['sex'] ?></th>
<th><?php echo $rows['salary'] ?></th>
</tr>
<?php endforeach;?>
</table>
<h3>
<!--上一页-->
<?php if($currentPage!=1):?>
<a href="./<?php echo JUMP_URL; ?>?p=1">首页</a>
<a href="./<?php echo JUMP_URL; ?>?p=<?php
echo ($currentPage>1) ? ($currentPage - 1) : 1;
?>">上一页</a>
<?php endif; ?>
<!--生成中间页码-->
<?php for($i=1;$i<=$totalNum;$i++): ?>
<a class="<?php if($i==$currentPage)echo 'active'; ?>"
href="./<?php echo JUMP_URL; ?>?p=<?php echo $i ?>"> <?php echo $i?></a>
<?php endfor?>
<!--下一页-->
<?php if ($currentPage != $totalNum): ?>
<a href="./<?php echo JUMP_URL; ?>?p=<?php echo $totalNum; ?>">尾页</a>
<a href="./<?php echo JUMP_URL; ?>?p=<?php
echo ($currentPage+1)<=$totalNum?($currentPage+1): $currentPage;
?>">下一页</a>
<?php endif; ?>
<form action="" method="get">
<select name="p" id="">
<?php for($i=1;$i<=$totalNum;$i++): ?>
<option value="<?php echo $i;?>"><?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号