Blogger Information
Blog 55
fans 3
comment 0
visits 54656
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分页
王佳祥
Original
1332 people have browsed it

PHP分页

1.连接数据库

  1. <?php
  2. //连接数据库
  3. $pdo = new PDO('mysql:host=localhost;dbname=tp5','root','wang1111');
  4. //设置默认的提取模式
  5. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);

2.分页原理

  1. <?php
  2. //分页原理
  3. //SELECT * FROM `user` 每页的数量 显示的偏移量
  4. //每页显示的数量 LIMIT n
  5. //显示的偏移量:从哪个索引开始 OFFSET m
  6. //偏移量 = (页码-1)* 每页的显示数量
  7. require 'connect.php';
  8. //获取分页数据,一定要知道的二个数据
  9. //1.每页显示的数量
  10. $num = 5;
  11. //2.当前页码
  12. $page = $_GET['p'] ?? 1;
  13. //3.计算每一页的第一条记录的显示偏移量
  14. $offset = ($page-1)*$num;
  15. //4.获取分页数据
  16. $sql = "SELECT * FROM `user` LIMIT {$num} OFFSET {$offset}";
  17. $users = $pdo->query($sql)->fetchAll();
  18. //print_r($users);
  19. //计算总页数
  20. //ceil()向上取整
  21. //总页数 = ceil(记录总数/每页的记录数)
  22. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `user`";
  23. $pages = $pdo->query($sql)->fetch()['total'];
  24. //print_r($res);


3.显示分页

  1. <?php require 'demo1.php'; ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>数据展示</title>
  8. <style>
  9. body{
  10. padding:0;
  11. margin:0;
  12. box-sizing:border-box;
  13. display:flex;
  14. justify-content:center;
  15. }
  16. thead > tr{
  17. background:#77DDFF;
  18. }
  19. table{
  20. text-align:center;
  21. width:800px;
  22. }
  23. caption{
  24. padding:10px 0;
  25. }
  26. .delete{
  27. color:#E63F00;
  28. }
  29. p{
  30. display:flex;
  31. justify-content:center;
  32. }
  33. p > a{
  34. display:block;
  35. border:1px solid #000;
  36. text-align:center;
  37. text-decoration:none;
  38. width:20px;
  39. height:20px;
  40. color:#000;
  41. margin:0 5px;
  42. }
  43. p > a:hover{
  44. background:#000;
  45. color:#fff;
  46. border:1px solid #fff;
  47. }
  48. .active{
  49. background:#000;
  50. color:#fff;
  51. border:1px solid #fff;
  52. }
  53. .shouye{
  54. width:40px;
  55. height:20px;
  56. font-size:15px;
  57. }
  58. .shang{
  59. width:60px;
  60. height:20px;
  61. font-size:15px;
  62. }
  63. </style>
  64. </head>
  65. <body>
  66. <div>
  67. <table border="1px" cellspacing="0" cellpadding="10px">
  68. <caption>用户信息表</caption>
  69. <thead>
  70. <tr>
  71. <td>id</td>
  72. <td>name</td>
  73. <td>username</td>
  74. <td>操作</td>
  75. </tr>
  76. </thead>
  77. <tbody>
  78. <?php foreach ($users as $user): ?>
  79. <tr>
  80. <td><?=$user['id']?></td>
  81. <td><?php echo $user['name'] ?></td>
  82. <td><?php echo $user['username'] ?></td>
  83. <td>
  84. <button onclick="location.href='handle.php?action=edit&id=<?=$user['id']?>'">编辑</button>
  85. <button class="delete" onclick="del(<?=$user['id']?>)">删除</button></td>
  86. </tr>
  87. <?php endforeach; ?>
  88. </tbody>
  89. </table>
  90. <!-- 分页条 -->
  91. <p>
  92. <?php
  93. $previous = $page-1;
  94. if($previous < 1) $previous = 1;
  95. ?>
  96. <?php if($page != 1): ?>
  97. <a href="<?=$_SERVER['PHP_SELF']?>?p=1" class="shouye">首页</a>
  98. <a href="<?=$_SERVER['PHP_SELF']?>?p=<?=$previous?>" class="shang">上一页</a>
  99. <?php endif; ?>
  100. <!-- ********************************************************** -->
  101. <?php
  102. if($page<=10):
  103. for($i=1;$i<=10;$i++):
  104. ?>
  105. <?php
  106. $jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i );
  107. //当前页码与当前页相等的时候
  108. $active = ($i == $page) ? 'active' : null;
  109. ?>
  110. <!-- 将类名变量化 -->
  111. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
  112. <?php
  113. endfor;
  114. endif;
  115. ?>
  116. <!-- ********************************************************** -->
  117. <?php
  118. //$twenty = ceil($page/10)*10;
  119. //$ten = floor($page);
  120. $a = $page + 5;
  121. $b = $page -4;
  122. $c = $pages - 10;
  123. if($page>10 && $page < $c):
  124. for($i=$b;$i<=$a;$i++):
  125. ?>
  126. <?php
  127. $jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i );
  128. //当前页码与当前页相等的时候
  129. $active = ($i == $page) ? 'active' : null;
  130. ?>
  131. <!-- 将类名变量化 -->
  132. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
  133. <?php
  134. endfor;
  135. endif;
  136. ?>
  137. <!-- ********************************************************** -->
  138. <?php
  139. $c = $pages - 10;
  140. if($page>=$c):
  141. for($i=$c;$i<=$pages;$i++):
  142. ?>
  143. <?php
  144. $jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i );
  145. //当前页码与当前页相等的时候
  146. $active = ($i == $page) ? 'active' : null;
  147. ?>
  148. <!-- 将类名变量化 -->
  149. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
  150. <?php
  151. endfor;
  152. endif;
  153. ?>
  154. <!-- ********************************************************** -->
  155. <?php
  156. $next = $page+1;
  157. if($next >= $pages) $next = $pages;
  158. ?>
  159. <?php if($page < $pages): ?>
  160. <a href="<?=$_SERVER['PHP_SELF']?>?p=<?=$next?>" class="shang">下一页</a>
  161. <a href="<?=$_SERVER['PHP_SELF']?>?p=<?=$pages?>" class="shouye">尾页</a>
  162. <?php endif; ?>
  163. </p>
  164. <script>
  165. //删除
  166. function del(id){
  167. str = 'handle.php?action=delete&id=';
  168. str+=id;
  169. if(confirm('是否删除?')){
  170. location.href=str;
  171. }
  172. }
  173. </script>
  174. </div>
  175. </body>
  176. </html>


4.编辑与删除

  • 编辑页面:
  1. <?php
  2. //获取要被编辑的数据
  3. $user = $pdo->query('SELECT * from `user` where `id` =' . $id)->fetch();
  4. print_r($user);
  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. </head>
  13. <body>
  14. <form action="<?php echo $_SERVER['PHP_SELF'] . '?action=doedit&id='.$id?>" method="post">
  15. <p>
  16. <label for="name">用户名</label>
  17. <input type="text" name="name" id="name" value="">
  18. </p>
  19. <p>
  20. <label for="username">账号名</label>
  21. <input type="text" name="username" id="username" value="">
  22. </p>
  23. <input type="hidden" name="id" value="">
  24. <p>
  25. <button>保存</button>
  26. </p>
  27. </form>
  28. </body>
  29. </html>
  • 删除页面
  1. <?php
  2. require 'connect.php';
  3. //获取操作
  4. $action = $_GET['action'];
  5. $id= $_GET['id'];
  6. switch ($action){
  7. //编辑操作
  8. case 'edit':
  9. include 'edit.php';
  10. break;
  11. //2.执行编辑操作
  12. case 'doedit':
  13. //更新
  14. $sql = 'UPDATE `user` SET `name`=?, `username`=? where `id`=?';
  15. $stmt = $pdo->prepare($sql);
  16. //新的数据在$_Post
  17. if(!empty($_POST)){
  18. $stmt->execute([$_POST['name'],$_POST['username'],$id]);
  19. if($stmt->rowCount() == 1) echo '<script>alert("更新成功");location.href="demo2.php"</script>';
  20. }
  21. break;
  22. //3.删除
  23. case 'delete':
  24. $sql = "DELETE FROM `user` where `id`=" . $id;
  25. $res = $pdo->exec($sql);
  26. if($res){
  27. echo '<script>alert("删除成功");location.href="demo2.php"</script>';
  28. }
  29. break;
  30. }


5.学习总结

  • 分页要确定每页显示几条数据,当前页码,总记录数,总页数(向上取整),然后通过循环遍历数据库中的内容,编辑需要跳转到一个编辑表单中更改数据,然后保存到数据库中,报存成功,提示更新成功并跳转;删除直接执行删除语句,如果删除成功则提示删除成功并跳转。
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