实例
<?php namespace Model\page { //分页类 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,$psw) { try { $this->pdo = new \PDO("{$type}:host={$host};dbname={$dbname}",$user,$psw); } catch (\PDOException $e) { die($e->getMessage()); } } //获取当前页码 public function getPage() { //如果存在页码变量则获取,否则默认为1 return isset($_GET['page']) ? $_GET['page'] : 1; } //获取总页数 public function getPageCount($tbName) { $stmt = $this->pdo->prepare("select count(*) from {$tbName}"); $stmt->execute(); //获取结果集的首行首列 $count = $stmt->fetchColumn(0); //向上取整,得到总页数 return ceil($count / $this->num); } //获取分页数据 public function getPageData($tbName) { $stmt = $this->pdo->prepare("select id,username,sex,phone from {$tbName} limit {$this->offset},{$this->num}"); $stmt->execute(); //获取数据并返回关联数据部分 return $stmt->fetchAll(\PDO::FETCH_ASSOC); } } }
分页数据的处理
实例
<?php //导入分页类 require 'page.php'; //引入命名空间 use Model\page\page; $page = new page(); //连接数据库 $page->connect('mysql','127.0.0.1','pdotest','root','root'); //获取当前页 $CurrentPage = $page->getPage(); //获取总页数 $ToalPage = $page->getPageCount('user'); // echo $ToalPage; //获取分页数据 $data = $page->getPageData('user'); //超出总页数的尾页限定 if($CurrentPage > $ToalPage) { echo '<script type="text/javascript"> location.href="http://127.0.0.1/PHP_Task/paging.php?page='.$ToalPage.'" </script>'; } //小于首页的首页限定 if($CurrentPage < 1) { echo '<script type="text/javascript"> location.href="http://127.0.0.1/PHP_Task/paging.php?page=1" </script>'; } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>分页</title> <style type="text/css"> body{ width: 800px; margin:0 auto; } .container{ margin:0 auto; width: 800px; height: 400px; text-align: center; } table{ margin: 0 auto; width: 550px; height: 250px; border: 1px solid gray; } table caption{ height: 50px; line-height: 50px; font-size: 1.5rem; } table tr{ background-color: #e7e7e7; } table tr:first-child{ background-color: gray; } table tr:hover { background-color:#C7C7C7; } table tr:active{ background-color: #969696; } a{ text-decoration-line: none; border:1px solid #5e5f5d; color:#333333; } a:hover{ border:1px solid gray; background-color: #acb0b4; color: white; } </style> </head> <body> <div class="container"> <table> <caption>用户信息表</caption> <tr style="background-color: #8C8C8C"> <td>ID</td> <td>姓名</td> <td>性别</td> <td>电话</td> </tr> <?php foreach ($data as $row): ?> <tr onclick="return trc()"> <td><?php echo $row['id']; ?></td> <td><?php echo $row['username']; ?></td> <td><?php echo $row['sex']; ?></td> <td><?php echo $row['phone']; ?></td> </tr> <?php endforeach; ?> </table> <br> <?php if($CurrentPage == 1){ ?> <a href="http://127.0.0.1/PHP_Task/paging.php?page=1" style="border:1px solid gray;color: gray;pointer-events: none;text-decoration-line: none;"> 首页 </a> <a href="http://127.0.0.1/PHP_Task/paging.php?page=<?php echo $CurrentPage - 1; ?>" style="border:1px solid gray;color: gray;pointer-events: none;text-decoration-line: none;"> 上一页 </a> <?php }else{ ?> <a href="http://127.0.0.1/PHP_Task/paging.php?page=1"style=""> 首页 </a> <a href="http://127.0.0.1/PHP_Task/paging.php?page=<?php echo $CurrentPage - 1; ?> "style=""> 上一页 </a> <?php } ?> <?php for ($i=1; $i <= $ToalPage ; $i++) { if($i ==$CurrentPage) { echo '<a href="http://127.0.0.1/PHP_Task/paging.php?page='. $i .'" style="border:1px dashed #4682B4;color:#008B00;text-decoration-line:none;pointer-events: none;font-size:17px;"> '.$i.' </a>',' '; }else { echo '<a href="http://127.0.0.1/PHP_Task/paging.php?page='. $i .'" style=";border:1px solid gray;"> '.$i.' </a>',' '; } } ?> <?php if($CurrentPage==$ToalPage){ ?> <a href="http://127.0.0.1/PHP_Task/paging.php?page=<?php echo $CurrentPage + 1; ?>" style="border:1px solid gray;color: gray;pointer-events: none;text-decoration-line: none;"> 下一页 </a> <a href="http://127.0.0.1/PHP_Task/paging.php?page=<?php echo $ToalPage; ?>" style="border:1px solid gray;color: gray;pointer-events: none;text-decoration-line: none;"> 尾页 </a> <?php }else{?> <a href="http://127.0.0.1/PHP_Task/paging.php?page=<?php echo $CurrentPage + 1; ?>" style=""> 下一页 </a> <a href="http://127.0.0.1/PHP_Task/paging.php?page=<?php echo $ToalPage;?>"style=""> 尾页 </a> <?php } ?> <!-- <form action="?action=jump" method="post"> 跳转至 <select name="jump"> <?php for ($i=1; $i <= $ToalPage ; $i++) { ?> <option value="<?php echo $i ?>"><?php echo $i ?></option>'; <?php } ?> </select> 页 <input type="submit" value="跳转"> </form> --> 跳转至 <select name="jump" onchange="location.href=this.options[selectedIndex].value"> <?php for ($i=1; $i <= $ToalPage ; $i++) { if($i==$CurrentPage) { echo '<option selected="selected">'.$i.'</option>'; }else{ ?> <option value="<?php echo 'http://127.0.0.1/PHP_Task/paging.php?page='.$i ?>"><?php echo $i ?></option>'; <?php }} ?> </select> 页 </div> </body> </html>
视图层与数据的交互,完成了分页的功能,有上下页,首尾页,中间页.还对地址栏的page参数进行限制,如果人为操作大于总页数,限制跳转至尾页,人为操作小于首页,限制跳转至首页.
对于快速跳转,直接利用了select的onchange方法,select列表值改变后即刻跳转至相应的页面.缩减了button的代码.