原理:
分页查询的原理与偏移量的计算方法
{
分页查询:
一、准备
1、获取总数据的总条数
2、设置每页显示的记录数
3、总计路数÷每页的记录数向上取整得到总页数
4、设置偏移量:(当前页数-1)×每页记录数
5、查询数据库限制每页显示记录数,返回结果集
二、显示数据
1、循环显示数据
2、循环显示分页页码
3、页码跳转:表单实现
}
分页显示
实例
<!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>nba18-19赛季薪资表</title> <style> table{ width: 600px; } table,th,td{ border: black solid 1px; border-collapse: collapse; padding: 5px; margin: 10px auto; text-align: center; } caption{ font-size: large; font-weight: bold; margin-bottom: 10px; } table tr:first-child{ background: #00CC66; } h3{ text-align: center; } h3 a{ border: black 1px solid; padding: 2px 5px; text-decoration: none; border-radius: 2px; margin: 3px; display: inline; } a:hover{ background: lightcoral; color: white; } form{ display: inline; } </style> </head> <body> <?php //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //设置url中的页码get参数 $page = isset($_GET['p'])?$_GET['p']: 1; //设置偏移量 $offset = ($page-1)*5; //准备sql语句 $sql = "SELECT * FROM `player` LIMIT {$offset},5"; //预处理对象 $stmt = $pdo->prepare($sql); //执行sql语句 $stmt->execute(); //返回结果集 $res = $stmt->fetchAll(PDO::FETCH_ASSOC); //获取总页数 $stmt = $pdo->prepare("SELECT COUNT(*) FROM `player`"); $stmt->execute(); $total = $stmt->fetchColumn(); $pages = ceil($total / 5); ?> <table> <caption>球员薪资表</caption> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>年薪(万)</th> </tr> <?php foreach ($res 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> <a href="http://php.io/0910/demo1.php?p=1">首页</a> <a href="http://php.io/0910/demo1.php?p=<?php echo (($page-1)==0)?1:($page-1);?>">上一页</a> <!--中间页码--> <?php for($i=1;$i<=$pages;$i++): ?> <a href="http://php.io/0910/demo1.php?p=<?php echo $i;?>"<?php echo ($i==$page)?'style="background:lightcoral;color:white;"':'';?>><?php echo $i;?></a> <?php endfor;?> <a href="http://php.io/0910/demo1.php?p=<?php echo (($page+1)>$pages)?$pages:($page+1);?>">下一页</a> <a href="http://php.io/0910/demo1.php?p=<?php echo $pages;?>">末页</a>跳转到第 <!--实现页面的快速跳转--> <form action=""method="get"> <select name="p" > <!--循环显示页码--> <?php for($i=1;$i<=$pages;$i++): ?> <option value="<?php echo $i; ?>" <?php if($page == $i){ echo 'selected'; } ?>><?php echo $i; ?> </option> <?php endfor;?> </select>页 <button>go</button> </form> </h3> </body> </html> <?php
运行实例 »
点击 "运行实例" 按钮查看在线实例
封装分页查询
实例
<?php namespace model; class Page { //偏移量 private $offset; //每页记录数 private $num; //数据库对象 private $pdo; //构造方法 public function __construct($num = 5) { //初始化每页记录数 $this->num = $num; $this->offset = ($this->getPage()-1)*$this->num; } //连接数据库 public function connect($type,$host,$dbname,$user,$pass) { $this->pdo = new \PDO("{$type}:host={$host};dbname={$dbname}",$user,$pass); } //获取当前页码 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(); 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); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
<!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="css.css"> </head> <body> <?php use model\Page; //导入分页类 require 'Page.php'; //实例化 $objPage = new Page(); //连接数据库 $objPage->connect('mysql','127.0.0.1','php','root','root'); //获取当前页 $page = $objPage->getPage(); //获取总页数 $pages = $objPage->getPages('player'); //获取分页内容 $res = $objPage->getData('player'); //print_r($res); ?> <table> <caption>球员薪资表</caption> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>年薪(万)</th> </tr> <?php foreach ($res 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 ($page!=1):?> <a href="http://php.io/0910/demo2.php?p=1">首页</a> <a href="http://php.io/0910/demo2.php?p=<?php echo (($page-1)==0)?1:($page-1);?>">上一页</a> <?php endif;?> <!--中间页码--> <?php for($i=1;$i<=$pages;$i++): ?> <a href="http://php.io/0910/demo2.php?p=<?php echo $i;?>"<?php echo ($i==$page)?'style="background:lightcoral;color:white;"':'';?>><?php echo $i;?></a> <?php endfor;?> <?php if ($page!=$pages):?> <a href="http://php.io/0910/demo2.php?p=<?php echo (($page+1)>$pages)?$pages:($page+1);?>">下一页</a> <a href="http://php.io/0910/demo2.php?p=<?php echo $pages;?>">末页</a> <?php endif;?>跳转到第 <!--实现页面的快速跳转--> <form action=""method="get"> <select name="p" > <!--循环显示页码--> <?php for($i=1;$i<=$pages;$i++): ?> <option value="<?php echo $i; ?>" <?php if($page == $i){ echo 'selected'; } ?>><?php echo $i; ?> </option> <?php endfor;?> </select>页 <button>go</button> </form> </h3> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例
实例
table{ width: 600px; } table,th,td{ border: black solid 1px; border-collapse: collapse; padding: 5px; margin: 10px auto; text-align: center; } caption{ font-size: large; font-weight: bold; margin-bottom: 10px; } table tr:first-child{ background: #00CC66; } h3{ text-align: center; } h3 a{ border: black 1px solid; padding: 2px 5px; text-decoration: none; border-radius: 2px; margin: 3px; display: inline; } a:hover{ background: lightcoral; color: white; } form{ display: inline; }
运行实例 »
点击 "运行实例" 按钮查看在线实例