Blogger Information
Blog 36
fans 1
comment 0
visits 26086
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP实现分页
早晨
Original
631 people have browsed it

php分页操作演示

page.php代码

  1. <?php
  2. namespace _0819;
  3. use PDO;
  4. // 连接
  5. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  6. // 1. 页数
  7. $page = $_GET['p'] ?? 1;
  8. echo '当前页数: p = ' . $page . '<br>';
  9. // 2. 数量
  10. $num = 5;
  11. echo '当前数量: num = ' . $num . '<br>';
  12. // 3. 偏移量 = (页数 - 1) * 数量
  13. $offset = ($page - 1) * $num;
  14. echo '当前偏移量: offset = ' . $offset . '<br>';
  15. // 4. 计算总记录数
  16. // SELECT CEIL(COUNT(*)/5) AS `total` FROM `staff`
  17. // SELECT COUNT(*) AS `total` FROM `staff`
  18. $sql = 'SELECT COUNT(*) AS `total` FROM `staff`';
  19. $stmt = $db->prepare($sql);
  20. $stmt->execute();
  21. // 将总数量绑定到一个变量上
  22. $stmt->bindColumn('total', $total);
  23. $stmt->fetch();
  24. echo '当前总记录数量: total = ' . $total . '<br>';
  25. // 5. 计算总页数
  26. // 向上取整
  27. $pages = ceil($total / $num);
  28. echo '当前总页数: pages = ' . $pages . '<br>';
  29. $sql = <<< SQL
  30. SELECT *
  31. FROM `staff`
  32. LIMIT $offset, $num;
  33. SQL;
  34. $stmt = $db->prepare($sql);
  35. $stmt->execute();
  36. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  37. echo '<hr>';
  38. /**
  39. * 生成分页码
  40. *
  41. * @param integer $page 当前页
  42. * @param integer $pages 总页数
  43. * @return array
  44. */
  45. // function createPages(int $page, int $pages): array
  46. // {
  47. // 当前是第8页, 共计20页
  48. // [1, ... 6, 7, 8, 9, 10, .... 20]
  49. // 当前是第10页, 共计20页
  50. // [1, ... 8, 9, 10, 11, 12, .... 20]
  51. // 1. 生成与总页数长度相同的递增的整数数组
  52. $pageArr = range(1, $pages);
  53. // 2. 只需要当前和前后二页, 其它页码用 false/null 来标记
  54. $paginate = array_map(function ($p) use ($page, $pages) {
  55. return ($p == 1 || $p == $pages || abs($page - $p) <= 2) ? $p : null;
  56. }, $pageArr);
  57. // dump($paginate);
  58. // 去重, 替换
  59. $before = array_unique(array_slice($paginate, 0, $page));
  60. $after = array_unique(array_slice($paginate, $page));
  61. // 用解构进行合并
  62. // return [...$before, ...$after];
  63. $page_ret = [...$before, ...$after];
  64. // }
  1. <?php
  2. require 'page.php';
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="zh-CN">
  6. <head>
  7. <meta charset="UTF-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <title>PHP实现分页</title>
  11. </head>
  12. <style>
  13. table {
  14. width: 400px;
  15. border-collapse: collapse;
  16. text-align: center;
  17. }
  18. table th,
  19. table td {
  20. border: 1px solid;
  21. padding: 5px;
  22. }
  23. table thead {
  24. background-color: lightcyan;
  25. }
  26. table caption {
  27. font-size: larger;
  28. margin-bottom: 8px;
  29. }
  30. body>p {
  31. display: flex;
  32. }
  33. p>a {
  34. text-decoration: none;
  35. color: #555;
  36. border: 1px solid;
  37. padding: 5px 10px;
  38. margin: 10px 2px;
  39. }
  40. .active {
  41. background-color: seagreen;
  42. color: white;
  43. border: 1px solid seagreen;
  44. }
  45. </style>
  46. <body>
  47. <table>
  48. <caption>员工信息表</caption>
  49. <thead>
  50. <tr>
  51. <th>ID</th>
  52. <th>姓名</th>
  53. <th>性别</th>
  54. <th>邮箱</th>
  55. </tr>
  56. </thead>
  57. <tbody>
  58. <?php foreach ($staffs as $staff) : extract($staff) ?>
  59. <?php $sex = $sex ? '男' : '女';
  60. ?>
  61. <tr>
  62. <td><?= $id ?>
  63. </td>
  64. <td><?= $name ?>
  65. </td>
  66. <td><?= $sex ?>
  67. </td>
  68. <td><?= $email ?>
  69. </td>
  70. </tr>
  71. <?php endforeach ?>
  72. </tbody>
  73. </table>
  74. <p>
  75. <?php
  76. $a = $page - 1;
  77. if ($page <= 0) {
  78. echo "<script>alert('前面没有了');history.go(-1);</script>";
  79. $a = 1;
  80. }
  81. $dqpage = $_SERVER['PHP_SELF'] . '?p=' . $a;
  82. ?>
  83. <a href="<?= $dqpage ?>">上一页</a>
  84. <?php foreach ($page_ret as $k => $v) : ?>
  85. <?php
  86. // 页码跳转的url
  87. if ($v === null) {
  88. $v = '...';
  89. }
  90. if ($v === '...') {
  91. $url = '#';
  92. } else {
  93. $url = $_SERVER['PHP_SELF'] . '?p=' . $v;
  94. }
  95. // 实现页码高亮
  96. $page = $_GET['p'] ?? 1;
  97. $active = ($v == $page) ? 'active' : null;
  98. ?>
  99. <a href="<?= $url ?>" class="<?= $active ?>"><?= $v ?></a>
  100. <?php endforeach ?>
  101. <?php
  102. $b = $page + 1;
  103. if ($page > $pages) {
  104. echo "<script>alert('后面没有了');history.go(-1);</script>";
  105. $a = $pages;
  106. }
  107. $dqpage = $_SERVER['PHP_SELF'] . '?p=' . $b;
  108. ?>
  109. <a href="<?= $dqpage ?>">下一页</a>
  110. </p>
  111. </body>
  112. </html>

运行效果

Correcting teacher:PHPzPHPz

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!