Blogger Information
Blog 62
fans 3
comment 1
visits 29682
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php分页操作演示
kiraseo_wwwkiraercom
Original
306 people have browsed it

php分页操作演示

代码

implement.php

  1. <?php
  2. // * 获取总页数
  3. namespace _0819;
  4. use PDO;
  5. // 连接
  6. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  7. // 1. 页数
  8. $page = $_GET['p'] ?? 1;
  9. echo '当前页数: p = ' . $page . '<br>';
  10. // 2. 数量
  11. $num = 5;
  12. echo '当前数量: num = ' . $num . '<br>';
  13. // 3. 偏移量 = (页数 - 1) * 数量
  14. $offset = ($page - 1) * $num;
  15. echo '当前偏移量: offset = ' . $offset . '<br>';
  16. // 4. 计算总记录数
  17. $sql = 'SELECT COUNT(*) AS `total` FROM `user`';
  18. $stmt = $db->prepare($sql);
  19. $stmt->execute();
  20. // 将总数量绑定到一个变量上
  21. $stmt->bindColumn('total', $total);
  22. $stmt->fetch();
  23. echo '当前总记录数量: total = ' . $total . '<br>';
  24. // 5. 计算总页数
  25. // 向上取整
  26. $pages = ceil($total / $num);
  27. echo '当前总页数: pages = ' . $pages . '<br>';
  28. $sql = <<< SQL
  29. SELECT *
  30. FROM `user`
  31. LIMIT $offset, $num;
  32. SQL;
  33. $stmt = $db->prepare($sql);
  34. $stmt->execute();
  35. $users = $stmt->fetchAll(PDO::FETCH_ASSOC);
  36. echo '<hr>';
  37. if (count($users) === 0) {
  38. echo '查询结果为空';
  39. } else {
  40. foreach ($users as $user) {
  41. extract($user); // $id,$name,$age,$email
  42. printf('%d-%s-%s-%s<br>', $id, $name, $age, $email);
  43. }
  44. }
  45. echo '<hr>';

page_refer.php

  1. /**
  2. * 生成分页码
  3. *
  4. * @param integer $page 当前页
  5. * @param integer $pages 总页数
  6. * @return array
  7. */
  8. function createPages(int $page, int $pages): array
  9. {
  10. // 当前是第8页, 共计20页
  11. // [1, ... 6, 7, 8, 9, 10, .... 20]
  12. // 当前是第10页, 共计20页
  13. // [1, ... 8, 9, 10, 11, 12, .... 20]
  14. // 1. 生成与总页数长度相同的递增的整数数组
  15. $pageArr = range(1, $pages);
  16. // 2. 只需要当前和前后二页, 其它页码用 false/null 来标记
  17. $paginate = array_map(function ($p) use ($page, $pages) {
  18. return ($p == 1 || $p == $pages || abs($page-$p) <=2) ? $p : null;
  19. }, $pageArr);
  20. // 去重, 替换
  21. //处理的是当前页码左边显示上一页的页码(替换掉数组中null的元素)
  22. $before = array_unique(array_slice($paginate, 0, $page));
  23. //处理的是当前页码右边显示下一页的页码(替换掉数组中null的元素)
  24. $after = array_unique(array_slice($paginate,$page));
  25. //上一页
  26. if($page>1) $pre = $page-1;
  27. //下一页
  28. if($page!=$pages) $next = $page+1;
  29. // 用解构进行合并
  30. // return [$pre,...$before, ...$after,$next];
  31. //对返回值进行重构
  32. return [$pre,[...$before, ...$after],$next];
  33. }

最终页面显示的代码

index.php

  1. <?php
  2. require 'page_refer.php';
  3. require 'implement.php';
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="zh-CN">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <title>分页展示数据</title>
  12. <style>
  13. table {width: 400px;border-collapse: collapse;text-align: center;}
  14. table th,
  15. table td {border: 1px solid;padding: 5px;}
  16. table thead {background-color: lightcyan; }
  17. table caption {font-size: larger;margin-bottom: 8px;}
  18. body>p {display: flex; }
  19. p>a {text-decoration: none;color: #555;border: 1px solid;padding: 5px 10px;margin: 10px 2px; }
  20. .active {background-color: seagreen;color: white;border: 1px solid seagreen;}
  21. </style>
  22. </head>
  23. <body>
  24. <table>
  25. <caption>员工信息表</caption>
  26. <thead>
  27. <tr>
  28. <th>ID</th>
  29. <th>姓名</th>
  30. <th>性别</th>
  31. <th>邮箱</th>
  32. </tr>
  33. </thead>
  34. <tbody>
  35. <?php foreach ($users as $user) :extract($user) ?>
  36. <tr>
  37. <td><?=$id?>
  38. </td>
  39. <td><?=$name?>
  40. </td>
  41. <td><?=$age?>
  42. </td>
  43. <td><?=$email?>
  44. </td>
  45. </tr>
  46. <?php endforeach ?>
  47. </tbody>
  48. </table>
  49. <p>
  50. <?php
  51. //利用当前url的页数与总页数对比,来判断
  52. if($page<= $pages){
  53. $url = $_SERVER['PHP_SELF'] . '?p=';
  54. $page_nums =createPages($page,$pages);
  55. foreach($page_nums as $k=>$v){
  56. if($k==0 ){
  57. if($v !=null){
  58. echo "<a href='$url$v' >上一页</a>";
  59. }
  60. }
  61. if($k==1){
  62. foreach($v as $v1){
  63. $active = ($v1 == $page) ? 'active' : '';
  64. if($v1 == null){ echo "<a>....</a>";}else{
  65. echo "<a href='$url$v1' class='$active'>$v1</a>";
  66. }
  67. }
  68. }
  69. if($k==2 ){
  70. if($v !=null){
  71. echo "<a href='$url$v' >下一页</a>";
  72. }
  73. }
  74. }
  75. }
  76. ?>
  77. </p>
  78. </body>
  79. </html>

输出效果

正常输出结果

当url输入的页码数> 总页数的结果

当页码为1时或者默认当前第一页的情况

当页码为最后一样的时候

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