Correction status:qualified
Teacher's comments:
php分页原理与实现
主要知识点
1)分页查询的原理
a. LIMIT 参数的作用: 偏移量与显示数量
b. 控制每页显示的数量
c. 接收GET参数,用page表示当前页数,每页显示设置的数量
d. 需要基本的参数:总页数,当前页,每页显示数量,偏移量
2)当前偏移量的计算公式:当前偏移量 = ( 当前页 - 1 ) * 每页显示数量
代码
Page.php
<?php namespace page; class Page { // 偏移量 private $offset; // 每页记录数量 private $pageNums; // 数据库连接 private $pdo; public function __construct($pageNums=5) { // 每页记录的数量 $this->pageNums = $pageNums; // 查询起始偏移量: (页码-1)*数量 $this->offset = ($this->getPage()-1)*$this->pageNums; } // 连接数据库 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['page']) ? $_GET['page'] : 1; } // 获取总页数 public function getTotalPages($table) { $stmt = $this->pdo->prepare("SELECT COUNT(*) FROM {$table}"); $stmt->execute(); $total = $stmt->fetchColumn(0); $totalPages = ceil($total / $this->pageNums); return $totalPages; } // 获取分页数据 public function getPageData($table) { $sql = "SELECT * FROM `{$table}` LIMIT {$this->offset}, {$this->pageNums} "; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(\PDO::FETCH_ASSOC); } }
demo1.php
<?php require "Page.php"; use page\Page; $page = new Page(3); // 连接数据库 $page->connect('mysql','127.0.0.1','php','root','root'); // 获取当前页 $currentPage = $page->getPage(); // 获取总页数 $totalPages = $page->getTotalPages('staff'); // echo $totalPages;die(); //获取分页数据 $data = $page->getPageData('staff'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> a{ display:inline-block; text-decoration:none; background:#C2C2C2; color: #1E90FF; padding: 8px; } a.active{ color: #fff; } table{ margin: 0 auto; } div{ margin-top:20px; text-align:center; } form{ display:inline-block; } </style> </head> <body> <table> <caption>员工信息表</caption> <tr> <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> <?php if($currentPage != 1): ?> <a href="http://127.0.0.1/test/0910/demo1.php?page=1">首页</a> <a href="http://127.0.0.1/test/0910/demo1.php?page=<?php echo $currentPage-1 ?>">上一页</a> <?php endif; ?> <?php for($i=1; $i<=$totalPages; $i++): ?> <a class="<?php echo $currentPage==$i? 'active' : '' ; ?>" href="http://127.0.0.1/test/0910/demo1.php?page=<?php echo $i; ?>"><?php echo $i; ?></a> <?php endfor; ?> <?php if($currentPage != $totalPages) :?> <a href="http://127.0.0.1/test/0910/demo1.php?page=<?php echo $currentPage+1; ?>">下一页</a> <a href="http://127.0.0.1/test/0910/demo1.php?page=<?php echo $totalPages; ?>">尾页</a> <?php endif; ?> <form action="" method="get"> 第 <select name="page" id=""> <?php for($i=1; $i<=$totalPages; $i++): ?> <option value="<?php echo $i; ?>" ><?php echo $i; ?></option> <?php endfor;?> </select> 页 <button>跳转</button> </form> </div> </body> </html>
运行结果