模型层
<?php /** * 分页查询 */ namespace model; class Model { //每页显示条数 private $num; //数据库链接 private $pdo; //查询起始偏移数量 private $offset; function __construct($num) { $this->num = $num; //获取当前的页 $this->offset = ($this->getPage()-1)*$this->num; } //获取当前页 public function getPage() { return isset($_GET['page'])?$_GET['page']:1; } //数据库链接 public function connect() { try{ $this->pdo = new \PDO('mysql:host=127.0.0.1;dbname=certificates','root','root'); }catch (\PDOException $e) { die($e->getMessage()); } } //获取总页数 public function getPages($table) { $sql = "select count(*) from {$table}"; $stmt = $this->pdo->prepare($sql); //var_dump($stmt); $stmt->execute(); $res = $stmt->fetchColumn(0); $pages = ceil($res/$this->num); return $pages; } //获取数据 public function getData($table){ $stmt1 = $this->pdo->prepare("select id,name,score from {$table} limit {$this->offset},{$this->num}"); $stmt1->execute(); $rows = $stmt1->fetchAll(\PDO::FETCH_ASSOC); return $rows; } }
点击 "运行实例" 按钮查看在线实例
分页模板页面
<!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>Document</title> </head> <body> <style> table,tr,td,th{width: 400px;margin:30px auto;border-collapse: collapse;border: 1px solid #000;text-align: center} tr{line-height: 30px;} th{background:#008800;color: #fff;} caption{padding: 20px 0;font-size: 1.5rem;} .pages{text-align: center;} a{text-decoration: none;padding:5px 10px;display: inline-block;border: solid 1px #008800;color:#008800;margin: 5px;} form{display: inline-block} .hover{background:#008800;color: #FFF;} </style> <?php require 'inc/Model.php'; use model\Model; $obj = new Model('20'); $obj->connect(); $page = $obj->getPage(); $pages = $obj->getPages('certificates'); $data = $obj->getData('certificates'); ?> <table> <caption>学生成绩表</caption> <tr> <th>ID</th> <th>姓名</th> <th>分数</th> </tr> <?php foreach ($data as $value):?> <tr> <td><?php echo $value['id'] ?></td> <td><?php echo $value['name'] ?></td> <td><?php echo $value['score'] ?></td> </tr> <?php endforeach;?> </table> <div class="pages"> <!-- 当前页不等于1显示首页和上一页 --> <?php if($page!=1):?> <a href="<?php $_SERVER['PHP_SELF']?>?page=1">首页</a> <a href="<?php $_SERVER['PHP_SELF']?>?page=<?php echo (($page-1)==0)?1:($page-1);?>">上一个</a> <?php endif;?> <!-- 生成中间页 --> <?php for($i=1; $i<=$pages; $i++): ?> <!------高亮显示当前页码-----------> <a class="<?php if($page==$i){echo 'hover';}?>" href="<?php $_SERVER['PHP_SELF']?>?page=<?php echo $i ?>"><?php echo $i ?></a> <?php endfor; ?> ?> <!-- 显示下一页和尾页--> <?php if($page!=$pages):?> <a href="<?php $_SERVER['PHP_SELF']?>?page=<?php echo (($page+1)>$pages)?$pages:($page+1);?>">下一页</a> <a href="<?php $_SERVER['PHP_SELF']?>?page=<?php echo $pages;?>">末页</a> <?php endif;?> <form action="">第: <select name="page" id="" > <?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>跳转</button> </form> </div> </body> </html>
点击 "运行实例" 按钮查看在线实例