博主信息
博文 20
粉丝 0
评论 0
访问量 11664
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
分页展示数据—-上一页、下一页/仿PHP中文网分页
P粉191340380
原创
599人浏览过

仿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>

批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学