分页查询的原理与偏移量的计算方法
原理:拿到总页数,然后 / 每页要显示的条数;
偏移量的计算方法: 总页数 / 每页显示的条数,然后向上取整;
2.分页查询的代码:
<?php //导入分页类 require '9.10inc/Page.php'; use model\Page; //实例化分页类 $page = new Page(); //连接数据库 $page->connect('mysql','127.0.0.1','php','root','root'); //$con = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //获取当前页 $currentPage = $page->getPage('staff'); //获取总页数 $totalPages = $page->getPages('staff'); //获取分页数据 $data = $page->getData('staff'); ?> <!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"> <link rel="stylesheet" href="./9.10inc/bootstrap/css/bootstrap.css"> <title>框架</title> </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 col-md-offset-2"> <table class="table table-bordered table-hover text-center"> <caption class="h3 text-center">员工信息表</caption> <tr class="info"> <td>ID</td> <td>姓名</td> <td>年龄</td> <td>性别</td> <td>工资</td> </tr> <?php foreach ($data 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> </div> </div> <div class="ro"> <div class="col-md-8 col-md-offset-2 text-center"> <nav> <ul class="pagination"> <li class="disabled"><a href="http://b.cn/9.10demo6.php?p=1" aria-label="Previous"><span aria-hidden="true">«</span></a></li> <li><a href="http://b.cn/9.10demo6.php?p=1">首页</a></li> <li><a href="http://b.cn/9.10demo6.php?p=<?php echo (($currentPage-1)==0) ? 1 :($currentPage-1);?>">上一页</a></li> <!-- 中间页码--> <?php for($i=1;$i<=$totalPages;$i++):?> <li class="<?php echo ($i == $currentPage) ? 'active' : '';?>"> <a href="http://b.cn/9.10demo6.php?p=<?php echo $i;?>"><?php echo $i?></a> </li> <?php endfor;?> <li><a href="http://b.cn/9.10demo6.php?p=<?php echo (($currentPage+1)>$totalPages) ? $totalPages :$currentPage+1;?>">下一页</a></li> <li><a href="http://b.cn/9.10demo6.php?p=<?php echo $totalPages;?>">尾页</a></li> </ul> </nav> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <scritp src="./9.10inc/bootstrap/js/bootstrap.js"></scritp> <scritp src="./9.10inc/bootstrap/js/npm.js"></scritp> </body> </html>