Blogger Information
Blog 14
fans 0
comment 1
visits 12727
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php分页(页码跳转+页码省略)
王珂
Original
1461 people have browsed it

分页(页码跳转+页码省略)

表格页面

  1. <?php
  2. namespace pdo_edu;
  3. use PDO;
  4. // 1. 连接数据库
  5. require 'conn.php';
  6. //获取第几页
  7. $page = $_GET['page'] ?? 1;
  8. //每页条数
  9. $num = 3;
  10. //总页数
  11. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `user`";
  12. $stmt = $pdo->prepare($sql);
  13. $stmt->execute();
  14. $pages = $stmt->fetch()['total'];
  15. $sql = "SELECT COUNT(`id`) AS `countid` FROM `user`";
  16. $stmt = $pdo->prepare($sql);
  17. $stmt->execute();
  18. $countid = $stmt->fetch()['countid'];
  19. //echo $pages;
  20. //偏移量
  21. $offset = $num * ($page - 1);
  22. //
  23. $sql = "SELECT * FROM `user` LIMIT {$num} OFFSET {$offset}";
  24. $stmt = $pdo->prepare($sql);
  25. $stmt->execute();
  26. $users = $stmt->fetchAll();
  27. //设置分页页码的样式
  28. $headpages = 2;//头和尾各显示几个
  29. $midpages = 3;//中间显示几个(单数)
  30. ?>
  31. <!DOCTYPE html>
  32. <html lang="en">
  33. <head>
  34. <meta charset="UTF-8">
  35. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  36. <title>分页数据展示</title>
  37. <link rel="stylesheet" href="style.css">
  38. </head>
  39. <body>
  40. <table>
  41. <caption>用户列表</caption>
  42. <thead>
  43. <tr>
  44. <th>ID</th>
  45. <th>姓名</th>
  46. <th>邮箱</th>
  47. <th>状态</th>
  48. <th>创建时间</th>
  49. <th>操作</th>
  50. </tr>
  51. </thead>
  52. <tbody>
  53. <?php foreach ($users as $user) : ?>
  54. <tr>
  55. <th><? echo $user['id'] ?></th>
  56. <th><? echo $user['name'] ?></th>
  57. <th><? echo $user['email'] ?></th>
  58. <th><? echo $user['status'] ?></th>
  59. <th><? echo date('Y年m月d日', $user['create_time']) ?></th>
  60. <th><button>编辑</button><button>删除</button></th>
  61. </tr>
  62. <?php endforeach ?>
  63. </tbody>
  64. </table>
  65. <!-- 添加跳转到首页, 前一页, 下一页, 尾页的功能 -->
  66. <p>
  67. <!-- 首页 -->
  68. <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=1' ?>">首页</a>
  69. <!-- 前一页 -->
  70. <?php
  71. $prev = $page - 1;
  72. if ($page == 1) $prev = 1;
  73. ?>
  74. <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $prev ?>">前一页</a>
  75. <?php
  76. if($pages<=($headpages*2+$midpages)){
  77. for ($i=1; $i<=$pages; $i++){
  78. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  79. $active = ($i == $page) ? 'active' :null;
  80. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  81. }
  82. }else{
  83. if($page<=($headpages+ceil($midpages/2))){
  84. for ($i=1; $i<=($headpages+$midpages); $i++){
  85. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  86. $active = ($i == $page) ? 'active' :null;
  87. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  88. }
  89. printf('<a href="#" class="">...</a>');
  90. for ($i=($pages-$headpages+1); $i<=$pages; $i++){
  91. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  92. $active = ($i == $page) ? 'active' :null;
  93. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  94. }
  95. }elseif($page>($pages-$headpages-ceil($midpages/2))){
  96. for ($i=1; $i<=($headpages); $i++){
  97. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  98. $active = ($i == $page) ? 'active' :null;
  99. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  100. }
  101. printf('<a href="#" class="">...</a>');
  102. for ($i=($pages-$headpages-$midpages+1); $i<=$pages; $i++){
  103. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  104. $active = ($i == $page) ? 'active' :null;
  105. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  106. }
  107. }else{
  108. for ($i=1; $i<=($headpages); $i++){
  109. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  110. $active = ($i == $page) ? 'active' :null;
  111. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  112. }
  113. printf('<a href="#" class="">...</a>');
  114. for ($i=($page-floor($midpages/2)); $i<=($page+floor($midpages/2)); $i++){
  115. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  116. $active = ($i == $page) ? 'active' :null;
  117. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  118. }
  119. printf('<a href="#" class="">...</a>');
  120. for ($i=($pages-$headpages+1); $i<=$pages; $i++){
  121. $jump = sprintf('%s?page=%s', $_SERVER['PHP_SELF'], $i );
  122. $active = ($i == $page) ? 'active' :null;
  123. printf('<a href="%s" class="%s">%s</a>',$jump,$active,$i);
  124. }
  125. }
  126. }
  127. ?>
  128. <!-- 下一页 -->
  129. <?php
  130. $next = $page + 1;
  131. if ($page == $pages) $next = $pages;
  132. ?>
  133. <a href="<?php echo $_SERVER['PHP_SELF'] . '?page=' . $next ?>">下一页</a>
  134. <!-- 尾页 -->
  135. <a href="<?php echo $_SERVER['PHP_SELF'] . '?page='. $pages ?>">尾页</a>
  136. <form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>" onSubmit="return chk(this)">
  137. <input name="page" type="number" size="3" mix="1" max="<?php echo $pages ?>">
  138. <input type="submit" value="跳转">
  139. </form>
  140. </p>
  141. </body>
  142. </html>

conn.php

  1. <?php
  2. namespace pdo_edu;
  3. use Exception;
  4. use PDO;
  5. $config = [
  6. 'type' => $type ?? 'mysql',
  7. 'host' => $host ?? 'localhost',
  8. 'dbname' => $dbname ?? 'phpedu',
  9. 'username' => $username ?? 'root',
  10. 'password' => $password ?? 'root',
  11. 'charset' => $charset ?? 'utf8',
  12. 'port' => $port ?? '3306',
  13. ];
  14. $type = $config['type'];
  15. $host = $config['host'];
  16. $dbname = $config['dbname'];
  17. $username = $config['username'];
  18. $password = $config['password'];
  19. $charset = $config['charset'];
  20. $port = $config['port'];
  21. $dsn = sprintf('%s:host=%s;dbname=%s;charset=%s;port=%s',$type,$host,$dbname,$charset,$port);
  22. try{
  23. $pdo = new PDO($dsn,$username,$password);
  24. }catch (Exception $e){
  25. die($e->getMessage());
  26. }

Correcting teacher:天蓬老师天蓬老师

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
2 comments
2022-04-13 12:42:45
你确定你的内容是正确无误么。误人子弟。要写就写正确。不要装逼。
2 floor
王珂 2020-05-14 22:09:52
能。从网上搜的,实测通过。
1 floor
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!