博客列表 >PHP分页功能扩展及数据的修改删除

PHP分页功能扩展及数据的修改删除

零龙
零龙原创
2020年08月10日 20:49:08614浏览

PHP分页功能扩展及数据的修改删除

  • connect.php 连接数据库
  • page-list.php显示分页效果
  • page-data.php分页处理
  • handle.php 处理修改和删除
  • edit.php 修改的信息提交
  • style.css 分页显示样式

1.connect.php 连接数据库

  1. <?php
  2. // 连接数据库
  3. // PDO连接对象
  4. $pdo = new PDO('mysql:host=localhost;dbname=mysqli', 'root', '142536');
  5. // 设置结果默认获取方式: 关联数组
  6. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

2.page-list.php

  1. <?php
  2. require 'page-data.php';?>
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6. <meta charset="UTF-8">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>数据展示</title>
  9. <link rel="stylesheet" href="style.css">
  10. </head>
  11. <body>
  12. <table>
  13. <caption>用户信息表</caption>
  14. <thead>
  15. <tr>
  16. <td>id</td>
  17. <td>姓名</td>
  18. <td>性别</td>
  19. <td>年龄</td>
  20. <td>邮件</td>
  21. <td>手机</td>
  22. <td>操11作</td>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <?php foreach ($show as $users):?>
  27. <tr>
  28. <!-- 短标签来简化变量的显示 -->
  29. <td><?=$users['id']?></td>
  30. <td><?=$users['username'] ?></td>
  31. <td><?=$users['sex'] ?></td>
  32. <td><?=$users['age'] ?></td>
  33. <td><?=$users['email']?></td>
  34. <td><?=$users['mobile'] ?></td>
  35. <td><button onclick="location.href='handle.php?action=edit&id=<?=$users['id']?>'" >编辑</button>
  36. <button onclick="location.href='handle.php?action=del&id=<?=$users['id']?>'" >删除</button></td>
  37. </tr>
  38. <?php endforeach ?>
  39. </tbody>
  40. </table>
  41. <p>
  42. <?php show($pages,$page)?>
  43. </p>
  44. </body>
  45. </html>

3.page-data.php分页处理

  1. <?php
  2. // 连接数据库
  3. require 'connect.php';
  4. //获取分页数据,一定要知道的二个数据
  5. //1.每页显示的数量
  6. $zpage = $_GET['zpage'];
  7. if($zpage == "")
  8. {
  9. $num = 5;
  10. }
  11. else
  12. {
  13. $num = $zpage;
  14. }
  15. //2.当前页码,默认为1
  16. $page = $_GET['p'] ?? 1;
  17. //3.计算每一页的第一条记录的显示偏移量
  18. $offset =($page - 1) * $num;
  19. //4.获取分页数据
  20. $sql = "SELECT * FROM `user` LIMIT {$num} OFFSET {$offset}";
  21. $show = $pdo->query($sql)->fetchAll();
  22. //计算总页数 SELECT COUNT(`ID`) AS `total` FROM `user`
  23. //计算表中共有多少条记录
  24. //每一页显示几条
  25. //总页数 = ceil(记录总数/每页的记录数) 向上取整 相当于9.1等于10
  26. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `user`";
  27. $pages=$pdo->query($sql)->fetch()['total'];
  28. function show($pages,$page)
  29. {
  30. $w = $pages-1;
  31. $c = $pages-10;
  32. $w1 =$pages-13;
  33. $w2 =$page-7;
  34. $w3 =$page+7;
  35. $show = "<a href=".$_SERVER['PHP_SELF'].">首页</a>";
  36. $prev =$page-1;
  37. if($prev == 0) $prev = 1;
  38. $show .= "<a href='{$_SERVER['PHP_SELF']}?p={$prev}'>上一页</a>";
  39. if($pages<10)
  40. {
  41. for($i = 1 ;$i<=$pages;$i++)
  42. {
  43. $href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
  44. $active = ($i == $page) ? 'active':NULL;
  45. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
  46. }
  47. }
  48. if($pages>=10 && $prev<10)
  49. {
  50. for($i = 1 ;$i<=10;$i++)
  51. {
  52. $href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
  53. $active = ($i == $page) ? 'active':NULL;
  54. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
  55. }
  56. $show.= "<a href='{$_SERVER['PHP_SELF']}?p=14'>...</a>";
  57. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w}'>{$w}</a>";
  58. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$pages}'>{$pages}</a>";
  59. }
  60. if($prev >= 10 && $prev < $c)
  61. {
  62. $show.= "<a href='{$_SERVER['PHP_SELF']}?p=1'>1</a>";
  63. $show.= "<a href='{$_SERVER['PHP_SELF']}?p=2'>2</a>";
  64. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w2}'>...</a>";
  65. for($i = $prev-2 ;$i<=$prev+4;$i++)
  66. {
  67. $href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
  68. $active = ($i == $page) ? 'active':NULL;
  69. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
  70. }
  71. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w3}'>...</a>";
  72. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w}'>{$w}</a>";
  73. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$pages}'>{$pages}</a>";
  74. }
  75. if($prev >=$c)
  76. {
  77. $show.= "<a href='{$_SERVER['PHP_SELF']}?p=1'>1</a>";
  78. $show.= "<a href='{$_SERVER['PHP_SELF']}?p=2'>2</a>";
  79. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$w1}'>...</a>";
  80. for($i = $pages-9 ;$i<=$pages;$i++)
  81. {
  82. $href=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i);
  83. $active = ($i == $page) ? 'active':NULL;
  84. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$i}' class='{$active}'>{$i}</a>";
  85. }
  86. }
  87. $next = $page + 1;
  88. if($next >=$pages) $next = $pages;
  89. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$next}'>下一页</a>";
  90. $show.= "<a href='{$_SERVER['PHP_SELF']}?p={$pages}'>尾页</a>";
  91. echo $show;
  92. }

4.handle.php 处理修改和删除

  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. case 'doedit':
  12. $sql = 'UPDATE `user` SET `username`=?,`password`=? WHERE `id`=?;';
  13. $stmt = $pdo->prepare($sql);
  14. if (!empty($_POST)) {
  15. $stmt->execute([$_POST['username'],sha1($_POST['password']), $id]);
  16. if ($stmt->rowCount() == 1) echo '<script>alert("更新成功");location.href="page-list.php"</script>';
  17. }
  18. break;
  19. case 'del':
  20. $sql ='DELETE FROM `user` WHERE `id`=?';
  21. $stmt = $pdo->prepare($sql);
  22. $stmt->execute([$id]);
  23. if ($stmt->rowCount() == 1) echo '<script>alert("删除成功");location.href="page-list.php"</script>';
  24. }

5.edit.php 修改的信息提交

  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. <h3>用户编辑</h3>
  15. <form action="<? echo $_SERVER['PHP_SELF']. '?action=doedit&id='.$id?>" method="POST">
  16. <p>
  17. <label for="name">用户名:</label>
  18. <input type="text" name="username" id="username" value="<?=$user['username']?>">
  19. </p>
  20. <p>
  21. <label for="email">密码:</label>
  22. <input type="password" name="password" id="password" value="******">
  23. </p>
  24. <p>
  25. <button>保存</button>
  26. </p>
  27. </form>
  28. </body>
  29. </html>

示例图:

总结:在给分页添加功能,主要是对pages和prev进行处理。

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