Blogger Information
Blog 119
fans 3
comment 1
visits 94376
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php分页实现原理演示(省略、跳转、修改、删除)
赵大叔
Original
928 people have browsed it

代码展示分页

1、php代码

  1. <?php
  2. // 连接数据库
  3. require 'connect.php';
  4. // 1. 当前的页数/页码
  5. $page = $_GET['p'] ?? 1;
  6. // 2. 每页显示的记录数量
  7. $num = 8;
  8. // 3. 总页数
  9. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `salary`";
  10. $pages = $pdo->query($sql)->fetch()['total'];
  11. // 4. 偏移量
  12. $offset = $num * ($page - 1);
  13. // 5. 分页数据
  14. $sql = "SELECT * FROM `salary` LIMIT {$num} OFFSET {$offset}";
  15. $salarys = $pdo->query($sql)->fetchAll();
  16. // print_r($salarys);
  17. ?>
  18. <!doctype html>
  19. <html lang="en">
  20. <head>
  21. <meta charset="UTF-8">
  22. <title>分页显示员工工资</title>
  23. <style>
  24. * {
  25. margin: 0;
  26. padding: 0;
  27. box-sizing: border-box;
  28. color: #555;
  29. }
  30. body {
  31. display: flex;
  32. flex-direction: column;
  33. align-items: center;
  34. }
  35. /*表格样式*/
  36. table {
  37. width: 80%;
  38. border: 1px solid;
  39. border-collapse: collapse;
  40. text-align: center;
  41. }
  42. table caption {
  43. font-size: 1.2rem;
  44. margin: 10px;
  45. }
  46. table td,
  47. table th {
  48. border: 1px solid;
  49. padding: 5px;
  50. }
  51. table tr:hover {
  52. background-color: #eee;
  53. }
  54. table thead tr:only-of-type {
  55. background-color: lightblue;
  56. }
  57. table button {
  58. width: 56px;
  59. height: 26px;
  60. }
  61. table button:last-of-type {
  62. color: red;
  63. }
  64. table button {
  65. /* 光标呈现为指示链接的指针(一只手) */
  66. cursor: pointer;
  67. margin: 0 3px;
  68. }
  69. table button:first-of-type:hover {
  70. background-color: red;
  71. }
  72. table button:last-of-type:hover {
  73. background-color: yellow;
  74. }
  75. /*分页条样式*/
  76. body > p {
  77. display: flex;
  78. }
  79. p > a {
  80. text-decoration: none;
  81. color: #555;
  82. border: 1px solid;
  83. padding: 5px 10px;
  84. margin: 10px 2px;
  85. }
  86. .active {
  87. background-color: red;
  88. color: white;
  89. border: 1px solid red;
  90. }
  91. .show1, .show2 {
  92. display: none;
  93. }
  94. </style>
  95. </head>
  96. <body>
  97. <table>
  98. <caption>员工工资明细表</caption>
  99. <thead>
  100. <tr>
  101. <th>ID</th>
  102. <th>工号</th>
  103. <th>姓名</th>
  104. <th>入职日期</th>
  105. <th>部门</th>
  106. <th>岗位</th>
  107. <th>工作天</th>
  108. <th>工资</th>
  109. <th>操作</th>
  110. </tr>
  111. </thead>
  112. <tbody>
  113. <?php foreach ($salarys as $salary) : ?>
  114. <tr>
  115. <td><?php echo $salary[id];?></td>
  116. <td><?php echo $salary[msnv];?></td>
  117. <td><?php echo $salary[name];?></td>
  118. <td><?php echo date('Y-m-d', $salary['hiredate']) ?></td>
  119. <td><?php echo $salary[donvi];?></td>
  120. <td><?php echo $salary[congviec];?></td>
  121. <td><?php echo $salary[ngaycong];?></td>
  122. <td><?php echo $salary[salary];?></td>\
  123. <td>
  124. <button onclick="location.href='handle.php?action=edit&id=<?php echo $salary['id'] ?>'">修改</button>
  125. <button onclick="location.href='handle.php?action=delet&id=<?php echo $salary['id'] ?>'">删除</button>
  126. </td>
  127. </tr>
  128. <?php endforeach; ?>
  129. </tbody>
  130. </table>
  131. <!--分页-->
  132. <p>
  133. <!--设置省略-->
  134. <?php
  135. // 1. 分页条显示7个页码
  136. $showPages = 7;
  137. // 2. 分页条的起始页码
  138. $startPage = 1;
  139. // 3. 分页条的终止页码
  140. $endPage = $pages; // 当前总页数:
  141. // 4. 分页条的偏移量: (当前分页条显示的页码数 - 1) / 2
  142. $offsetPage = ($showPages -1) / 2; // 3
  143. // 只有当前分页条数量 < 总页数, 才有必要显示出省略标记
  144. if ($showPages < $pages) {
  145. // 如果当前页 > 偏移量 + 1 , 应该显示...
  146. if ($page > $offsetPage + 1) {
  147. $startOmit = '...';
  148. }
  149. // 将当前分页条页码重置
  150. if ($page > $offsetPage) {
  151. $startPage = $page - $offsetPage;
  152. $endPage = $page + $offsetPage;
  153. if ($endPage > $pages) {$endPage = $pages;}
  154. } else {
  155. $startPage = 1;
  156. $endPage = $showPages;
  157. }
  158. // 如果当前页 + 偏移量 > 总页数
  159. if ($page + $offsetPage > $pages) {
  160. // 原理, 就是向当前页前面进行借位
  161. // 此时, 新的起点 = 当前位置 - (当前页 + 偏移量 - 原始位置)
  162. $startPage = $startPage - ($page + $offsetPage - $endPage);
  163. }
  164. if ($showPages < $pages && $page + $offsetPage < $pages) $endOmit = '...';
  165. }
  166. ?>
  167. <?php
  168. // 设置当在第一页和最后一页时不显示首页、上一页 或 尾页 下一页
  169. $show1 = ($page == 1) ? 'show1' :null;
  170. $show2 = ($page == $pages) ? 'show2' :null;
  171. ?>
  172. <!--首页-->
  173. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=1' ?>" class="<?php echo $show1; ?>">首页</a>
  174. <!--上一页-->
  175. <?php
  176. $prev = $page - 1;
  177. // 如果已经第1页,则定在第1页
  178. if ($page == 1) $prev = 1;
  179. ?>
  180. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p =' .$prev ?>" class="<?php echo $show1; ?>">上一页</a>
  181. <!--省略-->
  182. <?php if (isset($startOmit)) : ?> <a href="#"><?php echo $startOmit ?></a> <?php endif ?>
  183. <?php for ($i = $startPage; $i <= $endPage; $i++) : ?>
  184. <?php
  185. $jump = sprintf('%s?p=%s', $_SERVER['PHP_SELF'], $i);
  186. $active = ($i == $page) ? 'active' :null;
  187. ?>
  188. <a href="<?php echo $jump ?>" class="<?php echo $active ?>"><?php echo $i ?></a>
  189. <?php endfor; ?>
  190. <!--省略-->
  191. <?php if (isset($endOmit)) : ?> <a href="#"><?php echo $endOmit ?></a> <?php endif ?>
  192. <!--下一页-->
  193. <?php
  194. $next = $page + 1;
  195. // 如果已经最后一页,则定在最后一页
  196. if ($page == $pages) $next = $pages;
  197. ?>
  198. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' .$next ?>" class="<?php echo $show2; ?>">下一页</a>
  199. <!--尾页-->
  200. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' .$pages ?>" class="<?php echo $show2; ?>">尾页</a>
  201. </p>
  202. <!--跳转指定页-->
  203. <form name="form" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  204. <input name="p" type="number" size="3" min="1" max="<?php echo $pages; ?>">
  205. <input type="submit" value="跳转">
  206. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' .$_GET['p'] ?>"></a>
  207. </form>
  208. <script>
  209. function delet(id){
  210. if(confirm("是否删除?")){
  211. return "location.href='handle.php?action=delet&id=<?php echo $salary['id'] ?>";
  212. }
  213. return false;
  214. }
  215. </script>
  216. </body>
  217. </html>

2、运行效果图



删除、修改:

1、php代码

  1. // 修改表单代码
  2. <?php
  3. // 获取要被编辑的员工信息
  4. $salary = $pdo->query("SELECT * FROM `salary` WHERE `id`={$id}")->fetch();
  5. // print_r($salary);
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12. <title>调整员工工资</title>
  13. </head>
  14. <body>
  15. <h3>调整员工信息</h3>
  16. <form action="<?php echo $_SERVER['PHP_SELF'].'?action=doedit&id='. $id ?>" method="post">
  17. <p>
  18. <label for="msnv">工号:</label>
  19. <input type="text" id="msnv" name="msnv" value="<?php echo $salary['msnv'] ?>">
  20. </p>
  21. <p>
  22. <label for="name">姓名:</label>
  23. <input type="text" id="name" name="name" value="<?php echo $salary['name'] ?>">
  24. </p>
  25. <p>
  26. <label for="hiredate">入职日期:</label>
  27. <input type="text" id="hiredate" name="hiredate" value="<?php echo date('Y-m-d', $salary['hiredate']) ?>">
  28. </p>
  29. <p>
  30. <label for="donvi">部门:</label>
  31. <input type="text" id="donvi" name="donvi" value="<?php echo $salary['donvi'] ?>">
  32. </p>
  33. <p>
  34. <label for="congviec">岗位:</label>
  35. <input type="text" id="congviec" name="congviec" value="<?php echo $salary['congviec'] ?>">
  36. </p>
  37. <p>
  38. <label for="ngaycong">工作天:</label>
  39. <input type="text" id="ngaycong" name="ngaycong" value="<?php echo $salary['ngaycong'] ?>">
  40. </p>
  41. <p>
  42. <label for="salary">工资:</label>
  43. <input type="text" id="salary" name="salary" value="<?php echo $salary['salary'] ?>">
  44. </p>
  45. <input type="hidden" name="id" value="<?php echo $salary['id'] ?>">
  46. <p>
  47. <button>保存</button>
  48. </p>
  49. </form>
  50. </body>
  51. </html>
  52. // 控制器代码
  53. <?php
  54. require 'connect.php';
  55. $action = $_GET['action'];
  56. $id = $_GET['id'];
  57. switch ($action) {
  58. // 编辑需要进行二步
  59. // 1. 渲染编辑表单
  60. case 'edit':
  61. include 'edit.php';
  62. break;
  63. // 2. 执行修改操作
  64. case 'doedit':
  65. $sql = 'UPDATE `salary` SET `msnv`=:msnv, `name`=:name, `hiredate`=:hiredate, `donvi`=:donvi,`congviec`=:congviec,`ngaycong`=:ngaycong ,`salary`=:salary WHERE `id`=:id;';
  66. // print_r($_POST);
  67. // $_POST['id'] = $id;
  68. $_POST['hiredate'] = strtotime($_POST['hiredate']);
  69. // print_r($_POST);
  70. $stmt = $pdo->prepare($sql);
  71. $stmt->execute($_POST);
  72. // $stmt->debugDumpParams();
  73. if ($stmt->rowCount() === 1) echo '<script>alert("更新成功");location.href="demo1.php";</script>';
  74. // 3. 删除
  75. case 'delet':
  76. // echo $id;
  77. $stmt = $pdo->query("DELETE FROM `salary` WHERE `id`=$id");
  78. if ($stmt->rowCount() === 1) echo '<script>alert("删除成功");location.href="demo1.php";</script>';
  79. break;
  80. }

2、运行效果图


总结:关于分页,重点难点都主要在:显示起始页结束页偏移量,只要完全掌握住这三点,其它问题就不大了。

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
0 comments
Author's latest blog post