Blogger Information
Blog 48
fans 0
comment 0
visits 34163
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分页效果和扩展(0803)
丶久而旧之丶
Original
558 people have browsed it

分页

  1. <?php
  2. $pdo = new PDO("mysql:host=localhost;dbname=user;charset=utf8", "root", "root");
  3. // var_dump($pdo);
  4. // 获取每页显示数量
  5. $num = 5;
  6. // 获取页码
  7. $page = $_GET['p'] ?? 1;
  8. // 获取偏移量
  9. $offset = ($page - 1) * $num;
  10. $sql = "SELECT * FROM `apple` LIMIT {$num} OFFSET {$offset}";
  11. $users = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
  12. // 计算总页数
  13. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `apple`";
  14. $pages = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC)['total'];
  15. ?>
  16. <!DOCTYPE html>
  17. <html lang="en">
  18. <head>
  19. <meta charset="UTF-8">
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <title>分页</title>
  22. <style>
  23. p {
  24. text-align: center;
  25. }
  26. table {
  27. border-collapse: collapse;
  28. width: 300px;
  29. text-align: center;
  30. margin: auto;
  31. font-size: 18px;
  32. }
  33. tr:first-of-type {
  34. background-color: lightskyblue;
  35. }
  36. p:last-of-type {
  37. display: flex;
  38. justify-content: center;
  39. }
  40. a {
  41. text-decoration: none;
  42. text-align: center;
  43. /* border: 0.5px solid black; */
  44. padding: 0 5px;
  45. margin-left: 3px;
  46. color: lightslategrey;
  47. font-size: small;
  48. }
  49. .active {
  50. background-color: lightsalmon;
  51. border: lightsalmon;
  52. color: white;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <p>用户信息</p>
  58. <table border=1>
  59. <tr>
  60. <th>id</th>
  61. <th>姓名</th>
  62. <th>性别</th>
  63. <th>操作</th>
  64. </tr>
  65. <?php foreach ($users as $user) : ?>
  66. <tr>
  67. <td><?= $user['id'] ?></td>
  68. <td><?= $user['username'] ?></td>
  69. <td><?= $user['sex'] ?></td>
  70. <td><button onclick="location.href='hendle.php?action=action&id=<?= $user['id'] ?>&p=<?= $page ?>'">编辑</button>
  71. <button onclick="location.href='hendle.php?action=del&id=<?= $user['id'] ?>&p=<?= $page ?>'">删除</button>
  72. </td>
  73. </tr>
  74. <?php endforeach ?>
  75. </table>
  76. <p>
  77. <!-- 上一页 -->
  78. <?php
  79. // 防止上一页会跳转越界
  80. $prev = $page - 1;
  81. if ($page == 1) $prev = 1;
  82. ?>
  83. <!-- 判断是否要显示上一页和首页 -->
  84. <?php if ($page != 1) : ?>
  85. <a href="<?= $_SERVER['PHP_SELF'] . '?P=1' ?>">首页</a>
  86. <a href="<?= $_SERVER['PHP_SELF'] . '?p=' . $prev ?>">上一页</a>
  87. <?php endif ?>
  88. <!-- 分页条主体内容 -->
  89. <!-- 分页前面部分 -->
  90. <?php if ($page <= 3) : ?>
  91. <?php for ($i = 1; $i <= 5; $i++) : ?>
  92. <?php
  93. // 动态生成跳转地址
  94. $jump = $_SERVER['PHP_SELF'] . '?p=' . $i;
  95. // 页码高亮的动态生成
  96. $active = ($i == $page) ? 'active' : null;
  97. ?>
  98. <a href="<?= $jump ?>" class="<?= $active ?>"><?= $i ?></a>
  99. <?php endfor ?>
  100. <a href="">...</a>
  101. <a href="<?= $_SERVER['PHP_SELF'] . '?p=' . ($pages - 1) ?>" class="<?= $active ?>"><?= $pages - 1 ?></a>
  102. <a href="<?= $_SERVER['PHP_SELF'] . '?p=' . $pages ?>" class="<?= $active ?>"><?= $pages ?></a>
  103. <!-- 分页中间部分 -->
  104. <?php elseif ($page >= 4 && $page < $pages - 4) : ?>
  105. <a href="<?= $_SERVER['PHP_SELF'] . '?p=1' ?>" class="<?= $active ?>">1</a>
  106. <a href="<?= $_SERVER['PHP_SELF'] . '?p=2' ?>" class="<?= $active ?>">2</a>
  107. <a href="">...</a>
  108. <?php for ($i = $page - 1; $i <= $page + 1; $i++) : ?>
  109. <?php
  110. // 动态生成跳转地址
  111. $jump = $_SERVER['PHP_SELF'] . '?p=' . $i;
  112. // 页码高亮的动态生成
  113. $active = ($i == $page) ? 'active' : null;
  114. ?>
  115. <a href="<?= $jump ?>" class="<?= $active ?>"><?= $i ?></a>
  116. <?php endfor ?>
  117. <a href="">...</a>
  118. <a href="<?= $_SERVER['PHP_SELF'] . '?p=' . ($pages - 1) ?>" class="<?= $active ?>"><?= $pages - 1 ?></a>
  119. <a href="<?= $_SERVER['PHP_SELF'] . '?p=' . $pages ?>" class="<?= $active ?>"><?= $pages ?></a>
  120. <!-- 分页后半部分 -->
  121. <?php else : ?>
  122. <a href="<?= $_SERVER['PHP_SELF'] . '?p=1' ?>" class="<?= $active ?>">1</a>
  123. <a href="<?= $_SERVER['PHP_SELF'] . '?p=2' ?>" class="<?= $active ?>">2</a>
  124. <a href="">...</a>
  125. <?php for ($i = $pages - 4; $i <= $pages; $i++) : ?>
  126. <?php
  127. // 动态生成跳转地址
  128. $jump = $_SERVER['PHP_SELF'] . '?p=' . $i;
  129. // 页码高亮的动态生成
  130. $active = ($i == $page) ? 'active' : null;
  131. ?>
  132. <a href="<?= $jump ?>" class="<?= $active ?>"><?= $i ?></a>
  133. <?php endfor ?>
  134. <?php endif ?>
  135. <!-- 下一页 -->
  136. <?php
  137. // 防止下一页越界
  138. $next = $page + 1;
  139. if ($next > $pages) $next = $pages;
  140. ?>
  141. <!-- 判断是否要显示下一页和尾页 -->
  142. <?php if ($page != $pages) : ?>
  143. <a href="<?= $_SERVER['PHP_FILE'] . '?p=' . $next ?>">下一页</a>
  144. <a href="<?= $_SERVER['PHP_FILE'] . '?p=' . $pages ?>">尾页</a>
  145. <?php endif ?>
  146. </p>
  147. </body>
  148. </html>

控制器

  1. <?php
  2. $pdo = new PDO("mysql:host=localhost;dbname=user;", "root", "root");
  3. $action = $_GET['action'];
  4. $id = $_GET['id'];
  5. $page = $_GET['p'];
  6. // echo $action, $id;
  7. // var_dump($pdo->query("SELECT * FROM apple where `id`=$id")->fetch(PDO::FETCH_ASSOC));
  8. // var_dump($a);
  9. switch ($action) {
  10. case 'action':
  11. include 'yonghu.php';
  12. break;
  13. case 'doedit':
  14. if (!empty($_POST)) {
  15. $name = $_POST['name'];
  16. $sex = $_POST['sex'] ? '男' : '女';
  17. $sql = "UPDATE `apple` SET `username`=?, `sex`=? WHERE `id`=?";
  18. $stmt = $pdo->prepare($sql);
  19. $stmt->execute([$name, $sex, $id]);
  20. if ($stmt->rowCount() == 1) {
  21. echo "<script>alert('编辑程功');location.href='0803.php?p='+'$page'</script>";
  22. }
  23. }
  24. break;
  25. case 'del':
  26. if ($pdo->query("DELETE FROM `apple` WHERE `id`=$id")->rowCount() == 1)
  27. echo "<script>alert('删除成功');location.href='0803.php?p='+'$page'</script>";
  28. break;
  29. }

用户编辑

  1. <?php
  2. $user = $pdo->query("select * from apple where id=$id")->fetch(PDO::FETCH_ASSOC);
  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>用户编辑</title>
  10. </head>
  11. <body>
  12. <form action="<?= $_SERVER['PHP_FILE'] . '?action=doedit&id=' . $id . '&p=' . $page ?>" method="POST">
  13. <fieldset>
  14. <legend>用户编辑</legend>
  15. <label for='name'>用户名:</label>
  16. <input type='text' name='name' id='name' placeholder='<?= $user['username'] ?>' required>
  17. <label for='sex'>性别:</label>
  18. <input type='radio' name='sex' id='sex' value='1'>
  19. <input type='radio' name='sex' id='sex1' value='0'>
  20. <button>保存</button>
  21. </fieldset>
  22. </form>
  23. </body>
  24. </html>

编辑和删除

总结

1.了解分页条效果的处理(但感觉冗余了不知怎么精简)
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