Blogger Information
Blog 20
fans 0
comment 0
visits 8140
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分页展示数据—-上一页、下一页/仿PHP中文网分页
P粉191340380
Original
450 people have browsed it

仿PHP中文网分页

  1. <?php
  2. require 'page_refer.php' ;
  3. // $pageChange = createPages()
  4. // use PDO;
  5. // 连接
  6. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  7. // 1. 页数
  8. $page = $_GET['p'] ?? 1;
  9. // 2. 数量
  10. $num = 5;
  11. // 3. 偏移量 = (页数 - 1) * 数量
  12. $offset = ($page - 1) * $num;
  13. // 4. 计算总记录数
  14. $sql = 'SELECT COUNT(*) AS `total` FROM `staff`';
  15. $stmt = $db->prepare($sql);
  16. $stmt->execute();
  17. // 将总数量绑定到一个变量上
  18. $stmt->bindColumn('total', $total);
  19. $stmt->fetch();
  20. $sql = <<< SQL
  21. SELECT *
  22. FROM `staff`
  23. LIMIT $offset, $num;
  24. SQL;
  25. $stmt = $db->prepare($sql);
  26. $stmt->execute();
  27. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  28. // 表示第5页, 一共有20页
  29. $page = $_GET['page'] ?? 1;
  30. $pagelist = createPages($page, 20);
  31. ?>
  32. <!DOCTYPE html>
  33. <html lang="zh-CN">
  34. <head>
  35. <meta charset="UTF-8">
  36. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  37. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  38. <title>分页展示数据</title>
  39. <style>
  40. table {
  41. width: 500px;
  42. border-collapse: collapse;
  43. text-align: center;
  44. }
  45. table th,
  46. table td {
  47. border: 1px solid;
  48. padding: 5px;
  49. }
  50. table thead {
  51. background-color: lightcyan;
  52. }
  53. table caption {
  54. font-size: larger;
  55. margin-bottom: 8px;
  56. }
  57. body>p {
  58. display: flex;
  59. }
  60. p>a {
  61. text-decoration: none;
  62. color: #555;
  63. border: 1px solid;
  64. padding: 5px 10px;
  65. margin: 10px 2px;
  66. }
  67. .active {
  68. background-color: seagreen;
  69. color: white;
  70. border: 1px solid seagreen;
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <table>
  76. <caption>员工信息表</caption>
  77. <thead>
  78. <tr>
  79. <th>ID</th>
  80. <th>姓名</th>
  81. <th>性别</th>
  82. <th>邮箱</th>
  83. </tr>
  84. </thead>
  85. <tbody>
  86. <?php foreach ($staffs as $staff) :extract($staff) ?>
  87. <tr>
  88. <td><?=$id?>
  89. </td>
  90. <td><?=$name?>
  91. </td>
  92. <td><?=$sex?>
  93. </td>
  94. <td><?=$email?>
  95. </td>
  96. </tr>
  97. <?php endforeach ?>
  98. </tbody>
  99. </table>
  100. <p>
  101. <?php $url = 'http://phpedu.io/0819/demo4.php?page=' ?>
  102. <?php foreach ($pagelist as $staff) : ?>
  103. <?php if(isset($staff)): ?>
  104. <a href="<?php echo $url . $staff; ?>"<?php echo $page == $staff ? 'class="active"' : null ?>><?=$staff ?></a>
  105. <?php else: ?>
  106. <a>......</a>
  107. <?php endif ?>
  108. <?php endforeach ?>
  109. <!-- <?php for ($i=1; $i <= $pages; $i++): ?> -->
  110. <?php
  111. // 实现页码高亮
  112. $page = $_GET['p'] ?? 1;
  113. $active = ($i == $page) ? 'active' : null;
  114. ?>
  115. <!-- <a href="<?=$url?>"
  116. class="<?=$active?>"><?=$i?></a> -->
  117. <?php endfor ?>
  118. <br>
  119. </p>
  120. </body>
  121. </html>

Correction status:Uncorrected

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