博客列表 >数据分页显示

数据分页显示

江流
江流原创
2021年10月26日 19:55:52576浏览
  1. <?php require_once "header.php" ?>
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>显示用户信息</title>
  9. <link rel="stylesheet" href="bootstrap.css">
  10. <style>
  11. /* 1. 添加表格线,添加给单元格 */
  12. tbody td,
  13. th {
  14. border: 1px solid;
  15. }
  16. /* 2. 折叠表格线 */
  17. table {
  18. border-collapse: collapse;
  19. width: 90%;
  20. /* 对齐 */
  21. text-align: center;
  22. margin: 2em auto;
  23. }
  24. /* 标题 */
  25. table thead {
  26. background-color: lightskyblue;
  27. line-height: 80px;
  28. }
  29. table tbody{
  30. line-height: 80px;
  31. }
  32. tbody tr:nth-of-type(2n){
  33. background-color: #ddd;
  34. }
  35. .show h2{
  36. text-align: center;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <?php
  42. require_once "auto.php";
  43. $p=$_GET['p']??1;
  44. $count=5; //每页显示的记录条数
  45. $data=UserContr::GetAllUserPage($p,$count);
  46. //总记录行数
  47. $rowCount=UserContr::GetRowCount();
  48. $pages=ceil($rowCount/$count);
  49. ?>
  50. <div class="show">
  51. <h2>用户信息</h2>
  52. <table>
  53. <thead>
  54. <tr>
  55. <th><input type="checkbox" id="check_all"></th>
  56. <th>ID</th>
  57. <th>用户名</th>
  58. <th>姓名</th>
  59. <th>电话</th>
  60. <th>邮箱</th>
  61. <th>创建时间</th>
  62. <th>最后登录时间</th>
  63. <th>状态</th>
  64. </tr>
  65. </thead>
  66. <tbody>
  67. <?php foreach($data as $user): ?>
  68. <tr>
  69. <td><input type="checkbox" class="chk_item"></td>
  70. <td class="id"><?=$user->id?></td>
  71. <td><a href="edituser.php?id=<?=$user->id?>" title="点击修改用户信息" ><?=$user->account?></a></td>
  72. <td><?=$user->name?></td>
  73. <td><?=$user->phone?></td>
  74. <td><?=$user->email?></td>
  75. <td><?=date('Y-m-d',$user->createtime)?></td>
  76. <td><?=date('Y-m-d',$user->updatetime)?></td>
  77. <td><?=$user->state==1?'开启':'关闭'?></td>
  78. </tr>
  79. <?php endforeach ?>
  80. </tbody>
  81. <tfoot>
  82. <tr>
  83. <td colspan="9">
  84. <nav aria-label="...">
  85. <ul class="pagination">
  86. <li class="page-item <?=$p==1?'disabled':''?>">
  87. <a class="page-link" href="showuser.php?p=<?=$p-1 ?>" tabindex="-1" aria-disabled="true">上一页</a>
  88. </li>
  89. <?php for($i=1;$i<=$pages;$i++): ?>
  90. <li class="page-item <?=$p==$i?'active':''?>"><a class="page-link" href="showuser.php?p=<?=$i ?>"><?=$i ?></a></li>
  91. <?php endfor ?>
  92. <li class="page-item <?=$p==$pages?'disabled':''?>">
  93. <a class="page-link " href="showuser.php?p=<?=$p+1 ?>">下一页</a>
  94. </li>
  95. </ul>
  96. </nav>
  97. </td></tr>
  98. <tr><td colspan="3"><button onclick="del()">删除</button></td>
  99. <td><a href="adduser.php">添加用户</a></td>
  100. </tr>
  101. </tfoot>
  102. </table>
  103. </div>
  104. <script>
  105. const chkAll=document.querySelector("#check_all");
  106. const chkItems=document.querySelectorAll(".chk_item");
  107. chkAll.onchange=ev=>chkItems.forEach(item=>item.checked=ev.target.checked);
  108. chkItems.forEach(item=>item.onchange=()=>chkAll.checked=[...chkItems].every(item=>item.checked));
  109. function del(){
  110. if(confirm('您确定要删除这些用户吗?')){
  111. const chkItems=document.querySelectorAll(".chk_item");
  112. chkItems.forEach(item=>{
  113. if(item.checked){
  114. const id=item.parentElement.nextElementSibling.textContent;
  115. let isDel=delOpt(id);
  116. if(isDel){
  117. // console.log( item.parentElement.parentElement);
  118. item.parentElement.parentElement.remove();
  119. }
  120. }
  121. })
  122. }else{
  123. alert("NO");
  124. }
  125. }
  126. async function delOpt(id){
  127. const response = await fetch("del_user.php?id=" + id);
  128. const comments = await response.json();
  129. return comments;
  130. }
  131. </script>
  132. <script src="./js/jquery-3.5.1.min.js"></script>
  133. <script src="./js/bootstrap.bundle.js"></script>
  134. </body>
  135. </html>
  • UserContr.php 文件中分页和记录总数方法
  1. <?php
  2. require_once "auto.php";
  3. class UserContr{
  4. // 分页显示
  5. public static function GetAllUserPage($p,$count){
  6. $start=($p-1)*$count;
  7. $sql="SELECT * FROM `php_user` LIMIT $start ,$count";
  8. $pdo =CreatePDO::Create();
  9. $pre=$pdo->prepare($sql);
  10. $exec=$pre->execute();
  11. $data = $pre -> fetchAll(PDO::FETCH_OBJ);
  12. return $data;
  13. }
  14. //记录行数
  15. public static function GetRowCount(){
  16. $sql="SELECT count(*) as rowCount FROM `php_user`";
  17. $pdo=CreatePDO::Create();
  18. $pre=$pdo->prepare($sql);
  19. $exec=$pre->execute();
  20. $res=$pre->fetch(PDO::FETCH_ASSOC);
  21. return $res['rowCount'];
  22. }
  23. }
  • CreatePDO.php 文件
  1. <?php
  2. class CreatePDO{
  3. public static function Create(){
  4. try{
  5. $pdo = new PDO('mysql:host=127.0.0.1;dbname=prodb','root','123456');
  6. }catch(PDOException $e){
  7. // 抛出错误,错误是你可以定义的
  8. echo json_encode(['status'=>9,'msg'=>'数据库链接失败']);
  9. exit;
  10. }
  11. return $pdo;
  12. }
  13. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议