Blogger Information
Blog 37
fans 0
comment 0
visits 14251
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
仿PHP中文网分页
秋闲独醉
Original
580 people have browsed it

index.php

  1. <?php
  2. namespace _0819;
  3. use PDO;
  4. // 连接数据库
  5. $db = new PDO('mysql:dbname=phpedu;port=3308', 'user', '123456');
  6. //获取url的页码
  7. $page = $_GET['p'] ?? 1;
  8. //每页显示的记录数
  9. $num = 5;
  10. //当前页的起始记录数
  11. $offset = ($page - 1) * $num;
  12. //计算总记录数
  13. $sql = 'SELECT COUNT(*) AS `total` FROM `staff`';
  14. $stmt = $db->prepare($sql);
  15. $stmt->execute();
  16. // 将总数量绑定到一个变量上
  17. $stmt->bindColumn('total', $total);
  18. $stmt->fetch();
  19. // 向上取整总页数
  20. $pages = ceil($total / $num);
  21. //包含分页接口Page
  22. include 'Page.php';
  23. //调用Page接口的静态方法clickPage();
  24. $arr = \_0819\Page::clickPage('http://www.test.io/作业/PHP编程/0819/index.php',$page,$pages);
  25. $sql = <<< SQL
  26. SELECT *
  27. FROM `staff`
  28. LIMIT $offset, $num;
  29. SQL;
  30. $stmt = $db->prepare($sql);
  31. $stmt->execute();
  32. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  33. if (count($staffs) === 0) {
  34. echo '查询结果为空';
  35. }
  36. ?>
  37. <!DOCTYPE html>
  38. <html lang="zh-CN">
  39. <head>
  40. <meta charset="UTF-8">
  41. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  42. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  43. <title>分页展示数据</title>
  44. <style>
  45. table {
  46. width: 400px;
  47. border-collapse: collapse;
  48. text-align: center;
  49. margin: auto;
  50. }
  51. table th,
  52. table td {
  53. border: 1px solid;
  54. padding: 5px;
  55. }
  56. table thead {
  57. background-color: lightcyan;
  58. }
  59. table caption {
  60. font-size: larger;
  61. margin-bottom: 8px;
  62. }
  63. body>p {
  64. display: flex;
  65. place-content: center;
  66. margin: auto;
  67. }
  68. p>a {
  69. text-decoration: none;
  70. color: #555;
  71. border: 1px solid;
  72. padding: 5px 10px;
  73. margin: 10px 2px;
  74. }
  75. .active {
  76. background-color: seagreen;
  77. color: white;
  78. border: 1px solid seagreen;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <table>
  84. <caption>员工信息表</caption>
  85. <thead>
  86. <tr>
  87. <th>ID</th>
  88. <th>姓名</th>
  89. <th>性别</th>
  90. <th>邮箱</th>
  91. </tr>
  92. </thead>
  93. <tbody>
  94. <?php foreach ($staffs as $staff) :extract($staff) ?>
  95. <tr>
  96. <td><?=$id?>
  97. </td>
  98. <td><?=$name?>
  99. </td>
  100. <td><?=$sex?>
  101. </td>
  102. <td><?=$email?>
  103. </td>
  104. </tr>
  105. <?php endforeach ?>
  106. </tbody>
  107. </table>
  108. <p>
  109. <?php
  110. echo $arr;
  111. ?>
  112. </p>
  113. </body>
  114. </html>

分页接口

  1. <?php
  2. namespace _0819;
  3. //分页功能类
  4. class Page
  5. {
  6. /**
  7. * 分页功能:仿PHP中文网分页
  8. * @param string $url 基本链接('http://www.test.io/测试/demo.php')
  9. * @param int $page = 1,当前页码,默认第1页
  10. * @param int $pages 总页数。
  11. * @return string
  12. */
  13. public static function clickPage(string $url,int $page = 1,int $pages):string
  14. {
  15. //通过总页娄生成数组
  16. $pageArr = range(1,$pages);
  17. //首尾页数必须显示,当前页和前后两页显示
  18. $pageDisplay = array_map(function($p) use($page,$pages){
  19. //把其他不显示的元素置为null
  20. return ($p === 1 || $p === $pages || abs($page-$p) <= 2)? $p : null;
  21. },$pageArr);
  22. //以当前页为分界线,各自只保留一个null,
  23. $before = array_unique(array_slice($pageDisplay,0,$page));
  24. $after = array_unique(array_slice($pageDisplay,$page));
  25. //合并前后两个数组
  26. $pageArr = array_merge($before,$after);
  27. //判断上一页的值
  28. $prePage = ($page - 1) >1 ?($page - 1): 1;
  29. //判断下一页的值
  30. $nextPage = ($page + 1) < $pages ? ($page + 1): $pages;
  31. //并接上一页的html代码
  32. $preUrl = $url.'?p='.$prePage;
  33. $str = <<< Str
  34. <a href="$preUrl" >上一页</a>
  35. Str;
  36. //以数组的值来判断是否显示。
  37. //如果是当前页就给颜色
  38. for($i = 0; $i < count($pageArr);$i++){
  39. $active = "";
  40. if($pageArr[$i]){
  41. if($page === $pageArr[$i]){
  42. $active = "active";
  43. }
  44. $currentUrl = $url.'?p='.$pageArr[$i];
  45. $str .="<a href=$currentUrl class=$active >$pageArr[$i]</a>";
  46. }else{
  47. $str .="<a>...</a>";
  48. }
  49. }
  50. //并接上一页的html代码
  51. $nextUrl = $url.'?p='.$nextPage;
  52. $str .= <<< Str
  53. <a href="$nextUrl" >下一页</a>
  54. Str;
  55. return $str;
  56. }
  57. }
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