博客列表 >实现分页以及数据删改

实现分页以及数据删改

昔年
昔年原创
2020年05月15日 17:23:43815浏览

PHP实现数据分页

1.数据库连接文件

  1. <?php
  2. $dsn = "mysql:host=localhost;dbname=phpedu;charset=utf8;port=3306";
  3. $username = 'root';
  4. $password = 'root';
  5. try {
  6. $pdo = new PDO($dsn, $username, $password);
  7. } catch (Exception $e) {
  8. die($e->getMessage());
  9. }

2.显示页面

  1. <?php
  2. require 'connect.php';
  3. //1.获取当前页码
  4. $page = $_GET['p'] ?? 1;
  5. //2.每页显示的记录数量
  6. $num = 5;
  7. //3.获取记录总数
  8. $sql = "select count(`id`) as `total` from `staffs`";
  9. $total = $pdo->query($sql)->fetch()['total'];
  10. //4.计算总页数
  11. $pages = ceil($total / $num);
  12. //5.偏移量
  13. $offset = $num * ($page - 1);
  14. //6.获取显示的数据
  15. $sql = "select * from `staffs` limit {$num} offset {$offset}";
  16. $staffs = $pdo->query($sql)->fetchAll();
  17. // var_dump($staffs);
  18. // die;
  19. ?>
  20. <!DOCTYPE html>
  21. <html lang="en">
  22. <head>
  23. <meta charset="UTF-8">
  24. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  25. <title>分页数据显示</title>
  26. <link rel="stylesheet" href="style.css">
  27. </head>
  28. <body>
  29. <table>
  30. <caption>员工管理系统</caption>
  31. <thead>
  32. <tr>
  33. <th>ID</th>
  34. <th>姓名</th>
  35. <th>年龄</th>
  36. <th>性别</th>
  37. <th>职位</th>
  38. <th>手机</th>
  39. <th>入职时间</th>
  40. <th>操作</th>
  41. </tr>
  42. </thead>
  43. <tbody>
  44. <?php foreach ($staffs as $staff) : ?>
  45. <tr>
  46. <td><?php echo $staff['id'] ?></td>
  47. <td><?php echo $staff['name'] ?></td>
  48. <td><?php echo $staff['age'] ?></td>
  49. <td><?php echo $staff['sex'] ? '男' : '女' ?></td>
  50. <td><?php echo $staff['position'] ?></td>
  51. <td><?php echo $staff['mobile'] ?></td>
  52. <td><?php echo $staff['hiredate'] ?></td>
  53. <td>
  54. <button onclick="location.href='handle.php?action=edit&id=<?php echo $staff['id'] ?>'">编辑</button>
  55. <button onclick="del(<?php echo $staff['id'] ?>)">删除</button>
  56. </td>
  57. </tr>
  58. <? endforeach; ?>
  59. </tbody>
  60. </table>
  61. <hr>
  62. <hr>
  63. <hr>
  64. <hr>
  65. <!-- 添加跳转到首页, 前一页, 下一页, 尾页的功能 -->
  66. <p>
  67. <?php
  68. //1.设置分页条显示的页码个数
  69. $showPage = 5;
  70. //2.设置分页条的起始页
  71. $startPage = 1;
  72. //3.设置分页条的终止页
  73. $endPage = $pages;
  74. //4.计算偏移量
  75. $offsetPage = ($showPage - 1) / 2;
  76. if ($showPage < $pages) {
  77. if ($page > $offsetPage + 1) {
  78. $startOmit = '...';
  79. $startPage = $page - $offsetPage;
  80. }
  81. if ($page + $offsetPage < $pages) {
  82. $endOmit = '...';
  83. $endPage = $page + $offsetPage;
  84. }
  85. //其他情况下要将起始终止页码进行重置
  86. if ($page < $offsetPage + 1) {
  87. $endPage = $showPage;
  88. }
  89. if ($page + $offsetPage > $pages) {
  90. $startPage = $pages - $showPage + 1;
  91. }
  92. }
  93. ?>
  94. <!-- 首页 -->
  95. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=1' ?>">首页</a>
  96. <!-- 上一页 -->
  97. <?php if ($page - 1 >= 1) $pre = $page - 1;
  98. else $pre = 1 ?>
  99. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $pre ?>">上一页</a>
  100. <!-- 显示左边的省略号 -->
  101. <?php if (isset($startOmit)) : ?>
  102. <a href="#"><?php echo $startOmit ?></a>
  103. <?php endif; ?>
  104. <?php for ($i = $startPage; $i <= $endPage; $i++) : ?>
  105. <?php
  106. $jump = sprintf("%s?p=%s", $_SERVER['PHP_SELF'], $i);
  107. $active = ($page == $i) ? 'active' : null;
  108. ?>
  109. <a href="<?php echo $jump ?>" class="<?php echo $active ?>"><?php echo $i ?></a>
  110. <?php endfor; ?>
  111. <!-- 显示左边的省略号 -->
  112. <?php if (isset($endOmit)) : ?>
  113. <a href="#"><?php echo $endOmit; ?></a>
  114. <?php endif; ?>
  115. <!-- 下一页 -->
  116. <?php if ($page + 1 <= $pages) $next = $page + 1;
  117. else $next = $pages ?>
  118. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $next ?>">下一页</a>
  119. <!-- 尾页 -->
  120. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $pages ?>">尾页</a>
  121. </p>
  122. <script>
  123. function del(id) {
  124. return confirm('是否删除?') ? location.href = "handle.php?action=del&id=" + id : false;
  125. }
  126. </script>
  127. </body>
  128. </html>

3.处理脚本

  1. <?php
  2. require 'connect.php';
  3. $action = $_GET['action'];
  4. $id = $_GET['id'];
  5. switch ($action) {
  6. case 'edit':
  7. require 'edit.php';
  8. break;
  9. case 'doedit':
  10. $data = $_POST;
  11. $sql = 'UPDATE `staffs` SET `name`=:name, `age`=:age,`sex`=:sex,`position`=:position ,`mobile`=:mobile,`hiredate`=:hiredate WHERE `id`=:id';
  12. $stmt = $pdo->prepare($sql);
  13. $stmt->execute($data);
  14. if ($stmt->rowCount() === 1) echo '<script>alert("更新成功");location.href="demo1.php";</script>';
  15. else print_r($pdo->errorInfo());
  16. break;
  17. case 'del':
  18. var_dump($id);
  19. $sql = "delete from `staffs` where id={$id}";
  20. $stmt = $pdo->prepare($sql);
  21. $stmt->execute();
  22. if ($stmt->rowCount() === 1) {
  23. echo '<script>alert("删除成功");location.href="demo1.php";</script>';
  24. } else {
  25. echo '<script>alert("删除删除");location.href="demo1.php";</script>';
  26. }
  27. }

4.处理编辑信息显示

  1. <?php
  2. $staff = $pdo->query("SELECT * FROM `staffs` where `id` = {$id}")->fetch();
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7. <meta charset="UTF-8">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <title>Document</title>
  10. </head>
  11. <body>
  12. <h3>编辑员工信息</h3>
  13. <form action="<?php echo $_SERVER['PHP_SELF'] . '?action=doedit&id=' . $id ?>" method="post">
  14. <p>
  15. <label for="name">姓名</label>
  16. <input type="text" id="name" name="name" value="<?php echo $staff['name'] ?>">
  17. </p>
  18. <p>
  19. <label for="age">年龄:</label>
  20. <input type="number" id="age" name="age" value="<?php echo $staff['age'] ?>">
  21. </p>
  22. <p>
  23. <label for="sex">性别:</label>
  24. <input type="radio" id="sex" name="sex" value="1" <?php if ($staff['sex'] == 1) echo 'checked' ?>><label for=""></label>
  25. <input type="radio" id="sex" name="sex" value="0" <?php if ($staff['sex'] == 0) echo 'checked' ?>><label for=""></label>
  26. </p>
  27. <p>
  28. <label for="position">职位:</label>
  29. <input type="text" id="position" name="position" value="<?php echo $staff['position'] ?>">
  30. </p>
  31. <p>
  32. <label for="mobile">手机号:</label>
  33. <input type="text" id="tel" name="mobile" value="<?php echo $staff['mobile'] ?>">
  34. </p>
  35. <p>
  36. <label for="hiredate">入职时间:</label>
  37. <input type="text" id="hiredate" name="hiredate" value="<?php echo $staff['hiredate'] ?>">
  38. </p>
  39. <input type="hidden" name="id" value="<?php echo $staff['id'] ?>">
  40. <p>
  41. <button>保存</button>
  42. </p>
  43. </form>
  44. </body>
  45. </html>

5.css文件

  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. color: #555;
  6. }
  7. body {
  8. display: flex;
  9. flex-direction: column;
  10. align-items: center;
  11. }
  12. /* 表格样式 */
  13. table {
  14. width: 80%;
  15. border: 1px solid;
  16. border-collapse: collapse;
  17. text-align: center;
  18. }
  19. table caption {
  20. font-size: 1.2rem;
  21. margin: 10px;
  22. }
  23. table td,
  24. table th {
  25. border: 1px solid;
  26. padding: 5px;
  27. }
  28. table tr:hover {
  29. background-color: #eee;
  30. }
  31. table thead tr:only-of-type {
  32. background-color: lightblue;
  33. }
  34. table button {
  35. width: 56px;
  36. height: 26px;
  37. }
  38. table button:last-of-type {
  39. color: red;
  40. }
  41. table button {
  42. cursor: pointer;
  43. margin: 0 3px;
  44. }
  45. /* 分页样式 */
  46. p > a {
  47. text-decoration: none;
  48. color: #555;
  49. border: 1px solid;
  50. padding: 5px 10px;
  51. margin: 10px 2px;
  52. }
  53. .active {
  54. background-color: red;
  55. color: white;
  56. border: 1px solid red;
  57. }



总结:分页难点在于那个省略号的实现,针对不同的情况要考虑将起始页码和终止页码进行重置。

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