Blogger Information
Blog 29
fans 0
comment 0
visits 26996
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础:数据展示页的分页显示条设置
暴宇
Original
1220 people have browsed it

PHP基础:数据展示页的分页显示条设置

1.分页显示条功能

显示所在页数,点击页数即可直接跳转到对应的页数上

2.代码示例

  1. <?php
  2. // require 'demo1.php' ;
  3. // 连接数据库
  4. require 'connect.php';
  5. // 1. 当前的页数/页码
  6. $page = $_GET['p'] ?? 1;
  7. // 2. 每页显示的记录数量
  8. $num = 5;
  9. // 3. 总页数
  10. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `goods`";
  11. $pages = $pdo->query($sql)->fetch()['total'];
  12. // 4. 偏移量
  13. $offset = $num * ($page - 1);
  14. // 5. 分页数据
  15. $sql = "SELECT * FROM `goods` LIMIT {$num} OFFSET {$offset}";
  16. $goods = $pdo->query($sql)->fetchAll();
  17. ?>
  18. <!DOCTYPE html>
  19. <html lang="en">
  20. <head>
  21. <meta charset="UTF-8">
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <title>分页数据展示</title>
  24. <link rel="stylesheet" href="style.css">
  25. </head>
  26. <body>
  27. <table>
  28. <caption>材料管理系统</caption>
  29. <thead>
  30. <tr>
  31. <th>ID</th>
  32. <th>名称</th>
  33. <th>规格</th>
  34. <th>描述</th>
  35. <th>数量</th>
  36. <th>单位</th>
  37. <th>操作</th>
  38. </tr>
  39. </thead>
  40. <tbody>
  41. <?php foreach ($goods as $good) : ?>
  42. <tr>
  43. <td><?php echo $good['id'] ?></td>
  44. <td><?php echo $good['name'] ?></td>
  45. <td><?php echo $good['DN'] ?></td>
  46. <td><?php echo $good['describe'] ?></td>
  47. <td><?php echo $good['number'] ?></td>
  48. <td><?php echo $good['unit'] ?></td>
  49. <td><button onclick="location.href='server.php?action=edit&id=<?php echo $good['id'] ?>'">编辑</button>
  50. <button onclick="del(<?php echo $good['id'] ?>)">删除</button></td>
  51. </tr>
  52. <?php endforeach; ?>
  53. </tbody>
  54. </table>
  55. <!-- 添加跳转到首页, 前一页, 下一页, 尾页的功能 -->
  56. <p>
  57. <?php
  58. // 1. 分页条显示5个页码
  59. $showPages = 5;
  60. // 2. 分页条的起始页码
  61. $startPage = 1;
  62. // 3. 分页条的终止页码
  63. $endPage = $pages; // 当前总页数: 14
  64. // 4. 分页条的偏移量: (当前分页条显示的页码数 - 1) / 2
  65. $offsetPage = ($showPages -1) / 2; // 2
  66. // 只有当前分页条数量 < 总页数, 才有必要显示出省略标记
  67. if ($showPages < $pages) {
  68. // 如果当前页 > 偏移量 + 1 , 应该显示...
  69. if ($page > $offsetPage + 1) {
  70. $startOmit = '...';
  71. }
  72. // 将当前分页条页码重置
  73. if ($page > $offsetPage) {
  74. $startPage = $page - $offsetPage;
  75. $endPage = $page + $offsetPage;
  76. if ($endPage > $pages) {$endPage = $pages;}
  77. } else {
  78. $startPage = 1;
  79. $endPage = $showPages;
  80. }
  81. // 如果当前页 + 偏移量 > 总页数
  82. if ($page + $offsetPage > $pages) {
  83. // 原理, 就是向当前页前面进行借位
  84. // 此时, 新的起点 = 当前位置 - (当前页 + 偏移量 - 原始位置)
  85. $startPage = $startPage - ($page + $offsetPage - $endPage);
  86. }
  87. if ($showPages < $pages && $page + $offsetPage < $pages) $endOmit = '...';
  88. }
  89. ?>
  90. <!-- 首页 -->
  91. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=1' ?>">首页</a>
  92. <!-- 前一页 -->
  93. <?php
  94. $prev = $page - 1;
  95. if ($page == 1) $prev = 1;
  96. ?>
  97. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $prev ?>">前一页</a>
  98. <?php if (isset($startOmit)) : ?> <a href="#"><?php echo $startOmit ?></a> <?php endif ?>
  99. <?php for ($i=$startPage; $i<=$endPage; $i++): ?>
  100. <?php
  101. $jump = sprintf('%s?p=%s', $_SERVER['PHP_SELF'], $i );
  102. $active = ($i == $page) ? 'active' :null;
  103. ?>
  104. <a href="<?php echo $jump ?>" class="<?php echo $active ?>"><?php echo $i ?></a>
  105. <?php endfor ?>
  106. <?php if (isset($endOmit)) : ?> <a href="#"><?php echo $endOmit ?></a> <?php endif ?>
  107. <!-- 下一页 -->
  108. <?php
  109. $next = $page + 1;
  110. if ($page == $pages) $next = $pages;
  111. ?>
  112. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $next ?>">下一页</a>
  113. <!-- 尾页 -->
  114. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p='. $pages ?>">尾页</a>
  115. </p>
  116. <p>
  117. <!-- 直接跳转 -->
  118. <form id="topage" action="fenye.php" method="get">
  119. <label for="p">跳转到:</label>
  120. <input id='p' type="text" name='p'>
  121. <input type="submit" value="跳转" form="topage">
  122. </form>
  123. </p>
  124. <script>
  125. function del(id) {
  126. return confirm('是否删除?') ? alert('删除成功') : false;
  127. }
  128. </script>
  129. </body>
  130. </html>

2.运行效果

3.总结

分页的原理就是根据总数据条数和每页显示的条数计算出要分多少页,然后再计算出偏移量,根据偏移量进行查询数据并展示

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!