博客列表 >PHP - 实现分页以及数据删改 实例升级版

PHP - 实现分页以及数据删改 实例升级版

晴天
晴天原创
2020年05月14日 15:17:45605浏览

演示代码

演示地址 http://php.rc238.cn/0513/

index.php

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  6. <title>员工管理系统</title>
  7. <link rel="stylesheet" href="style.css"/>
  8. </head>
  9. <body>
  10. <table>
  11. <caption>
  12. 员工管理系统
  13. </caption>
  14. <thead>
  15. <tr>
  16. <th>ID</th>
  17. <th>姓名</th>
  18. <th>年龄</th>
  19. <th>性别</th>
  20. <th>职位</th>
  21. <th>手机</th>
  22. <th>入职时间</th>
  23. <th>操作</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <?php
  28. require 'parameter.php';
  29. foreach ($staffs as $staff) {
  30. // 数组转名值对
  31. extract($staff);
  32. /* @var string $id ;
  33. * @var string $name ;
  34. * @var string $age ;
  35. * @var string $sex ;
  36. * @var string $position ;
  37. * @var string $mobile ;
  38. * @var string $hiredate ;
  39. * */
  40. $sex = $sex ? '男' : '女';
  41. $hiredate = date('Y / m / d', $hiredate);
  42. echo <<<AAA
  43. <tr>
  44. <td>{$id}</td>
  45. <td>{$name}</td>
  46. <td>{$age}</td>
  47. <td>{$sex}</td>
  48. <td>{$position}</td>
  49. <td>{$mobile}</td>
  50. <td>{$hiredate}</td>
  51. <td><button onclick="location.href='handle.php?action=edit&p={$page}&id={$id}'" >编辑</button><button onclick="del('{$id}','{$name}','{$page}')">删除</button></td>
  52. </tr>
  53. AAA;
  54. }
  55. //删除数据
  56. ?>
  57. </tbody>
  58. </table>
  59. <div>
  60. <?php
  61. // 分页条数
  62. $showpage = 5;
  63. //起始页码 永远显示前两页
  64. $startpage = 3;
  65. //结束页 永远显示后两页
  66. $endpage = $pages - 2;
  67. //页码偏移量
  68. $offsetpage = ($showpage - 1) / 2;
  69. //开始判断是否有必要显示...
  70. if ($showpage < $pages) {
  71. // 大于6显示前面的省略号
  72. if ($page >= 6){
  73. $startOmit = '...';
  74. }
  75. // 小于总页数-分页条数显示后面省略号
  76. if ($page <= ($pages-$showpage)) {
  77. $endOmit = '...';
  78. }
  79. // 修改起始页码
  80. if ($page >= 6){
  81. $startpage = $page-$offsetpage;
  82. if ($startpage >= $pages-$showpage ){
  83. $startpage = $pages-$showpage;
  84. }
  85. }
  86. // 修改结束页码
  87. if ($page <= $pages-$showpage){
  88. $endpage = $page+$offsetpage;
  89. if ($endpage <= 6){
  90. $endpage = 6;
  91. }
  92. }
  93. }
  94. // 显示首页 和 上一页
  95. if ($page > 1) {
  96. echo "<a href='?p=1' class='headtail'>首页</a>";
  97. $start = $page - 1;
  98. echo "<a href='?p={$start}' class='around'>上一页</a>";
  99. }
  100. // 输出方法
  101. function Output($i)
  102. {
  103. global $page;
  104. $jump = sprintf('?p=%s', $i);
  105. $active = ($i == $page) ? 'active' : null;
  106. echo "<a href='{$jump}' class='{$active}'>{$i}</a>";
  107. }
  108. // 永远显示前两页
  109. for ($i = 1; $i <= 2; $i++) {
  110. Output($i);
  111. }
  112. // 判断显示前面...
  113. if (isset($startOmit)) {
  114. echo '<span>'.$startOmit.'</span>';
  115. }
  116. // 显示中间页码
  117. for ($i = $startpage; $i <= $endpage; $i++) {
  118. Output($i);
  119. }
  120. // 判断显示后面...
  121. if (isset($endOmit)) {
  122. echo '<span>'.$endOmit.'</span>';
  123. }
  124. // 永远显示后两页/
  125. for ($i = $pages - 1; $i <= $pages; $i++) {
  126. Output($i);
  127. }
  128. // 判断显示下一页和尾页
  129. if ($page < $pages) {
  130. $end = $page + 1;
  131. echo "<a href='?p={$end}' class='around'>下一页</a>";
  132. echo "<a href='?p={$pages}' class='headtail'>尾页</a>";
  133. }
  134. // 如果总页数大于10 显示跳转
  135. if ($pages > 10) {
  136. echo <<<bbb
  137. <form class="form1" action="" method="get">
  138. <input type="number" name="p"min="1" max=" {$pages}" required />
  139. <button>跳转</button>
  140. </form>
  141. bbb;
  142. }
  143. ?></div>
  144. <script>
  145. function del(id,name,page) {
  146. return confirm('确认删除姓名为"'+name+'"的员工吗?') ? location.href="handle.php?action=del&p="+page+"&id="+id : false;
  147. }
  148. </script>
  149. </body>
  150. </html>

parameter.php

  1. <?php
  2. //连接数据库
  3. require 'connect.php';
  4. //获取当前页码
  5. $page = $_GET['p'] ?? 1 ;
  6. //每页显示数量
  7. $num = 13;
  8. //总页数
  9. $pages = $pdo->query("SELECT CEIL(COUNT(`id`)/{$num}) AS `tatol` FROM `staffs`")->fetch()['tatol'];
  10. //偏移量
  11. $offset = $num * ($page-1);
  12. //分页数据
  13. $staffs = $pdo->query("SELECT * FROM `staffs` LIMIT {$num} OFFSET {$offset}")->fetchAll();

connect.php

  1. <?php
  2. //创建连接参数
  3. $dsn = 'mysql:host=localhost;dbname=php11.edu';
  4. $username = 'php11.edu';
  5. $password = 'php11.edu';
  6. //创建异常类
  7. try {
  8. $pdo = new PDO($dsn,$username,$password);
  9. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
  10. }catch (Exception $e){
  11. die($e->getMessage());
  12. }

handle.php

  1. <?php
  2. //先判断有没有传值过来
  3. if (!isset($_GET['action']) || !isset($_GET['p']) || !isset($_GET['id'])) {
  4. die('<script>alert("非法请求");location.href="index.php"</script>');
  5. }
  6. //获取数据
  7. $action = strtolower($_GET['action']);
  8. $p = $_GET['p'];
  9. $id = $_GET['id'];
  10. //连接数据库
  11. require 'connect.php';
  12. //执行
  13. switch ($action) {
  14. case 'edit':
  15. require 'edit.php';
  16. break;
  17. case 'doedit':
  18. // print_r($_POST);
  19. // 执行修改
  20. //执行更新
  21. $sql = "UPDATE `staffs` SET `name` = :name ,`age` = :age , `position` = :position,`mobile` = :mobile WHERE `id` = :id";
  22. $stmt = $pdo->prepare($sql);
  23. $stmt->execute($_POST);
  24. //判断是否执行成功
  25. if ($stmt->rowCount()===1){
  26. echo "<script>alert('修改成功');location.href='index.php?p={$p}'</script>";
  27. }else{
  28. if ($stmt->errorInfo()[0] == 00000){
  29. echo "<script>alert('没有值被修改');location.href='index.php?p={$p}'</script>";
  30. }else{
  31. echo "<script>alert('修改失败');location.href='index.php?p={$p}'</script>";
  32. }
  33. }
  34. break;
  35. case 'del':
  36. $stmt = $pdo->prepare("DELETE FROM `staffs` WHERE `id`={$id}");
  37. $stmt->execute();
  38. if ($stmt->rowCount() === 1) {
  39. echo "<script>alert('删除成功');location.href='index.php?p={$p}'</script>";
  40. } else {
  41. echo "<script>alert('删除失败');location.href='index.php?p={$p}'</script>";
  42. }
  43. break;
  44. }

edit.php

  1. <?php
  2. //更新修改数据库
  3. //先查询到信息显示
  4. $sql = "SELECT * FROM `staffs` WHERE `id`={$id}";
  5. $user = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
  6. extract($user);
  7. /**
  8. * @var string $id ;
  9. * @var string $name ;
  10. * @var string $age ;
  11. * @var string $sex ;
  12. * @var string $position ;
  13. * @var string $mobile ;
  14. * @var string $hiredate ;
  15. **/
  16. $sex = $sex ? '男' : '女';
  17. $date = date('Y / m / d', $hiredate);
  18. echo <<< aa
  19. <style>
  20. @import "style.css";
  21. </style>
  22. <form class="update" action="?action=doedit&id={$id}&p={$p}" method="post">
  23. <div>
  24. <label for="id">ID:</label>
  25. <input type="text" id="id" disabled value="{$id}" />
  26. </div>
  27. <div>
  28. <label for="name">姓名:</label>
  29. <input type="text" id="name" name="name" required value="{$name}" />
  30. </div>
  31. <div>
  32. <label for="age">年龄:</label>
  33. <input type="number" max="99" min="18" id="age" name="age" required value="{$age}" />
  34. </div>
  35. <div>
  36. <label for="sex">性别:</label>
  37. <input type="text" id="sex" disabled value="{$sex}" />
  38. </div>
  39. <div>
  40. <label for="position">职位:</label>
  41. <input type="text" id="position" name="position" required value="{$position}" />
  42. </div>
  43. <div>
  44. <label for="mobile">手机号:</label>
  45. <input
  46. type="text"
  47. id="mobile"
  48. name="mobile"
  49. required
  50. minlength="11"
  51. maxlength="11"
  52. value="{$mobile}"
  53. />
  54. </div>
  55. <div>
  56. <label for="hiredate">入职时间:</label>
  57. <input type="text" id="hiredate" disabled value="{$date}" />
  58. </div>
  59. <button name="id" value="{$id}">提交</button>
  60. </form>
  61. aa;

style.css

  1. * {
  2. margin: 0px;
  3. padding: 0px;
  4. box-sizing: border-box;
  5. color: #555;
  6. }
  7. body {
  8. display: flex;
  9. flex-direction: column;
  10. align-items: center;
  11. }
  12. table {
  13. width: 1000px;
  14. border: 1px solid;
  15. text-align: center;
  16. border-collapse: collapse;
  17. }
  18. table > caption {
  19. font-size: 1.2rem;
  20. margin: 10px;
  21. }
  22. table td,
  23. table th {
  24. border: 1px solid;
  25. padding: 5px;
  26. }
  27. table tr:hover {
  28. background-color: #eee;
  29. }
  30. div {
  31. display: flex;
  32. flex-flow: row nowrap;
  33. align-items: center;
  34. }
  35. div > a {
  36. padding: 5px;
  37. border: 1px solid;
  38. margin: 10px;
  39. width: 30px;
  40. height: 30px;
  41. text-decoration: none;
  42. text-align: center;
  43. }
  44. div > a:hover {
  45. background-color: violet;
  46. color: #fff;
  47. }
  48. .headtail {
  49. width: 50px;
  50. }
  51. .around {
  52. width: 80px;
  53. }
  54. .active {
  55. background-color: lightskyblue;
  56. color: linen;
  57. }
  58. .form1 > input {
  59. width: 50px;
  60. height: 30px;
  61. }
  62. .form1 > input:out-of-range,
  63. .form1 > input::-webkit-inner-spin-button {
  64. appearance: none;
  65. }
  66. .update {
  67. display: flex;
  68. flex-flow: column;
  69. margin: 30px auto;
  70. }
  71. .update > div {
  72. width: 220px;
  73. margin-bottom: 10px;
  74. display: flex;
  75. justify-content: space-between;
  76. }
  77. tbody button {
  78. margin: 0px 10px;
  79. border: none;
  80. border-radius: 5px;
  81. padding: 5px 10px;
  82. background-color: lightskyblue;
  83. color: #fff;
  84. }
  85. tbody button:hover {
  86. background-color: lightgreen;
  87. }

总结

比之前的代码流畅多了

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议