Blogger Information
Blog 145
fans 7
comment 7
visits 164506
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP实战:数据分页显示案例
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
Original
789 people have browsed it

分页显示案例

1、代码(数据连接查询略)

  1. <?php
  2. $staffs=require 'contect.php';
  3. // echo $pages;
  4. // print_r($staffs);
  5. ?>
  6. <!DOCTYPE html>
  7. <html lang="en">
  8. <head>
  9. <meta charset="UTF-8">
  10. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  11. <title>员工信息表</title>
  12. <style>
  13. body>div {
  14. width: 700px;
  15. margin: 0 auto;
  16. outline: 1px solid red;
  17. display: flex;
  18. flex-flow: column nowrap;
  19. align-items: center;
  20. }
  21. p {
  22. height: 25px;
  23. display: flex;
  24. justify-content: flex-start;
  25. align-items: center;
  26. }
  27. p a {
  28. text-decoration: none;
  29. width: 38px;
  30. text-align: center;
  31. margin: 0 3px;
  32. }
  33. .active {
  34. background-color: tomato;
  35. color: white;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div>
  41. <table border=1 cellspacing=0 cellpadding=10>
  42. <caption style="font-size:28px">员工信息表</caption>
  43. <tr>
  44. <th>ID</th>
  45. <th>姓名</th>
  46. <th>年龄</th>
  47. <th>性别</th>
  48. <th>职位</th>
  49. <th>电话</th>
  50. <th>操作</th>
  51. </tr>
  52. <?php foreach($staffs as $staff):?>
  53. <tr>
  54. <td><?php echo $staff['id'];?></td>
  55. <td><?php echo $staff['name'];?></td>
  56. <td><?php echo $staff['age'];?></td>
  57. <td><?php echo $staff['sex'] ? '男' : '女';?></td>
  58. <td><?php echo $staff['position'];?></td>
  59. <td><?php echo $staff['mobile'];?></td>
  60. <td><button>编辑</button><button>删除</button></td>
  61. </tr>
  62. <?php endforeach; ?>
  63. </table>
  64. <p>
  65. <a href="demo.php?p=1">首页</a>
  66. <?php
  67. $current=filter_input(INPUT_GET,'p',FILTER_VALIDATE_INT);
  68. // echo $current;
  69. if($current<=1){
  70. $prev=1;
  71. }elseif($current>$pages){
  72. $prev=$pages;
  73. }
  74. else{
  75. $prev=$current-1;
  76. }
  77. if($current>=$pages){
  78. $next=$pages;
  79. }elseif($current<1){
  80. $next=1;
  81. }
  82. else{
  83. $next=$current+1;
  84. }
  85. ?>
  86. <a href="demo.php?p=<?php echo $prev ?>">前一页</a>
  87. <?php for($i=1;$i<=$pages;$i++): ?>
  88. <a href="demo.php?p=<?php echo $i;?>"
  89. class='page <?php echo ($i===$current) ? 'active':null ?>'><?php echo $i;?></a>
  90. <?php endfor; ?>
  91. <a href="demo.php?p=<?php echo $next?>">下一页</a>
  92. <a href="demo.php?p=<?php echo $pages?>">尾页</a>
  93. </p>
  94. <form action="demo.php" method='GET'>
  95. <input type="number" name="p" min='1' max='<?php echo $pages ?>'>
  96. <button>跳转</button>
  97. </form>
  98. <p>
  99. <a href="demo.php?p=1">首页</a>
  100. <?php
  101. $current=filter_input(INPUT_GET,'p',FILTER_VALIDATE_INT);
  102. // echo $current;
  103. if($current<=1){
  104. $prev=1;
  105. }elseif($current>$pages){
  106. $prev=$pages;
  107. }
  108. else{
  109. $prev=$current-1;
  110. }
  111. if($current>=$pages){
  112. $next=$pages;
  113. }elseif($current<1){
  114. $next=1;
  115. }
  116. else{
  117. $next=$current+1;
  118. }
  119. ?>
  120. <a href="demo.php?p=<?php echo $prev ?>">前一页</a>
  121. <?php $showpages=5; $offset=($showpages-1)/2;
  122. if($current>$offset+1){
  123. echo "<a href=''>……</a>";
  124. $showstart=$current-$offset;
  125. $showend=$current+$offset;
  126. }
  127. else{
  128. $showstart=1;
  129. $showend=$current+($offset+($offset-$current)+$showstart);
  130. }
  131. if($showend>$pages){
  132. $showstart=$current-($offset+($showend-$pages));
  133. $showend=$pages;
  134. }
  135. ?>
  136. <?php for($i=$showstart;$i<=$showend;$i++): ?>
  137. <a href="demo.php?p=<?php echo $i;?>"
  138. class='page <?php echo ($i===$current) ? 'active':null ?>'><?php echo $i;?></a>
  139. <?php endfor; ?>
  140. <?php if($showend<$pages){ echo "<a href=''>……</a>" ; } ?>
  141. <a href="demo.php?p=<?php echo $next?>">下一页</a>
  142. <a href="demo.php?p=<?php echo $pages?>">尾页</a>
  143. </p>
  144. </div>
  145. </body>
  146. </html>

2、演示结果:

总结:

1、数据库分页查询:SELECT * FROMstaffsLIMIT 5 OFFSET 0;
(LIMIT 每页显示的记录数,OFFSET 偏移量=每页显示的数量 * (当前页数 - 1))
2、数据统计总数:SELECT COUNT('id') AS sum FROM `staffs;
3、跳转页码:利用form中method=”GET”;设置input的name=”p”;即可实现跳转至输入页码;利用input中(min|max)来限制页码超出范围
4、利用showpages页码、开始页码、结束页码、和页码偏移量来结算显示省率号;
5、获取当前脚本地址:$_SERVER['PHP_SELF']

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:现在体会到了$_SERVER变量的强大作用吧, 里面的每一个键都值得反复研究, 许多功能 都要靠它来实现
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