Blogger Information
Blog 28
fans 0
comment 0
visits 21921
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php:分页实现
暝皑祯π_π
Original
921 people have browsed it

数据处理

  1. <?php
  2. // 连接数据库
  3. try {
  4. $pdo = new PDO('mysql:host=php.cn;dbname=php','root','root');
  5. // 结果集获取方式: 关联
  6. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  7. } catch (Exception $e) {
  8. die($e->getMessage());
  9. }
  10. // 当前页码
  11. $page = $_GET['p'];
  12. // 每页显示的数据量
  13. $num = '5';
  14. // 总页码
  15. $pages = $pdo->query("SELECT CEIL(COUNT(`id`)/{$num}) AS total FROM `staff` ")->fetch()['total'];
  16. // echo $pages;
  17. // 偏移量
  18. $offset = $num * ($page - 1);
  19. // echo $offset;
  20. // 分页数据
  21. $staffs = $pdo->query("SELECT * FROM `staff` LIMIT {$num} OFFSET {$offset}")->fetchAll();
  22. // print_r($staffs);

前端页面

  1. <?php require '003.php'?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>数据展示</title>
  8. <link rel="stylesheet" href="style.css">
  9. </head>
  10. <body>
  11. <table>
  12. <caption>员工信息</caption>
  13. <thead>
  14. <tr>
  15. <td>工号</td>
  16. <td>状态</td>
  17. <td>部门</td>
  18. <td>职务</td>
  19. <td>性别</td>
  20. </tr>
  21. </thead>
  22. <tbody>
  23. <?php foreach($staffs as $staff): ?>
  24. <tr>
  25. <td><?php echo $staff['id'] ?></td>
  26. <td><?php echo $staff['state'] ?></td>
  27. <td><?php echo $staff['section'] ?></td>
  28. <td><?php echo $staff['duty'] ?></td>
  29. <td><?php echo $staff['gender'] ?></td>
  30. </tr>
  31. <?php endforeach ?>
  32. </tbody>
  33. </table>
  34. <p>
  35. <?php
  36. // 1. 分页条显示5个页码
  37. $showPages = 5;
  38. // 2. 分页条的起始页码
  39. $startPage = 1;
  40. // 3. 分页条的终止页码
  41. $endPage = $pages; // 当前总页数: 14
  42. // 4. 分页条的偏移量: (当前分页条显示的页码数 - 1) / 2
  43. $offsetPage = ($showPages -1) / 2; // 2
  44. if($showPages < $pages){
  45. if($page > $offsetPage + 1){
  46. $startOmit = '...';
  47. }
  48. }
  49. // 将当前分页条页码重置
  50. if ($page > $offsetPage) {
  51. $startPage = $page - $offsetPage;
  52. $endPage = $page + $offsetPage;
  53. if ($endPage > $pages) {$endPage = $pages;}
  54. } else {
  55. $startPage = 1;
  56. $endPage = $showPages;
  57. }
  58. // 如果当前页 + 偏移量 > 总页数
  59. if ($page + $offsetPage > $pages) {
  60. // 原理, 就是向当前页前面进行借位
  61. // 此时, 新的起点 = 当前位置 - (当前页 + 偏移量 - 原始位置)
  62. $startPage = $startPage - ($page + $offsetPage - $endPage);
  63. }
  64. if ($showPages < $pages && $page + $offsetPage < $pages) $endOmit = '...';
  65. ?>
  66. <!-- 首页 -->
  67. <a href="<?php echo $_SERVER['PHP_SELF'] . "?p=1"?>">首页</a>
  68. <!-- 上一页 -->
  69. <?php $pre = $page - 1;
  70. if($page == 1) $pre = 1 ?>
  71. <a href="<?php echo $_SERVER['PHP_SELF'] . "?p=" . $pre ?>">上一页 </a>
  72. <!-- 省略号 -->
  73. <?php if(isset($startOmit)): ?><a href=""><?php echo $startOmit ?></a><?php endif ?>
  74. <?php for($i= $startPage; $i<= $endPage; $i++): ?>
  75. <?php $jump = sprintf('%s?p=%s', $_SERVER['PHP_SELF'], $i );
  76. $active = ($i == $page) ? 'active' :null;
  77. ?>
  78. <a href="<?php echo $jump ?>" class="<?php echo $active ?>"><?php echo $i ?></a>
  79. <?php endfor ?>
  80. <?php if (isset($endOmit)) : ?> <a href="#"><?php echo $endOmit ?></a> <?php endif ?>
  81. <!-- 下一页 -->
  82. <?php $pre = $page + 1;
  83. if($page == $pages) $pre = $pages ?>
  84. <a href="<?php echo $_SERVER['PHP_SELF'] . "?p=" . $pre ?>">下一页 </a>
  85. <!-- 尾页 -->
  86. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p='. $pages ?>">尾页</a>
  87. </p>
  88. </body>
  89. </html>

总结

  • 输入页码跳转的时候,form的method设置为get
  • 关键数据:页码,偏移量
  • 页面数据偏移量公式:每页显示的数据 * (当前页码 - 1)
  • 分页条偏移量公式:(当前分页条显示的页码数 - 1)/2
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:分页, 看上去挺简单的功能, 实际实现起来, 还是非常麻烦的, 不是吗?
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post