Blogger Information
Blog 60
fans 5
comment 3
visits 65264
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分页操作数据表
longlong
Original
789 people have browsed it

1. 数据表分页

  • config.php
  1. <?php
  2. return [
  3. 'type' => $type ?? 'mysql',
  4. 'host' => $host ?? 'php.edu',
  5. 'dbname' => $dbname ?? 'first',
  6. 'charset' => $charset ?? 'utf8',
  7. 'port' => $port ?? '3306',
  8. 'username' => $username ?? 'root',
  9. 'password' => $password ?? 'root',
  10. ];
  • connect.php
  1. <?php
  2. $config = require "config.php";
  3. extract($config);
  4. // echo $type,$host;
  5. // $dsn = 'mysql:host=php.edu;dbname=first;charset=utf8;port=3306';
  6. $dsn = sprintf('%s:host=%s;dbname=%s;charset=%s;port=%s',$type,$host,$dbname,$charset,$port);
  7. try {
  8. $pdo = new PDO($dsn,$username,$password);
  9. // 设置结果集获取方式:关联数组
  10. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
  11. } catch (Throwable $e) {
  12. exit($e->getMessage());
  13. } catch (PDOException $e) {
  14. exit($e->getMessage());
  15. }
  16. // var_dump($pdo);
  • page-data.php
  1. <?php
  2. // 1. 连接数据库
  3. require "connect.php";
  4. // 2. 获取分页的两个已知条件
  5. // 2.1. 每页显示的记录数量
  6. $num = 8;
  7. // 2.2 获取当前页数(通过url传输 p ),默认为1,p=1
  8. $page = $_GET['p'] ?? 1;
  9. // 3. 计算偏移量
  10. $offset = ($page - 1) * $num;
  11. // 4. 获取分页数据
  12. $sql = "SELECT * FROM `student` LIMIT {$num} OFFSET {$offset}";
  13. $users = $pdo->query($sql)->fetchAll();
  14. // print_r($result);
  15. // 5. 计算总页数
  16. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `student`";
  17. $pages = $pdo->query($sql)->fetch()['total'];
  18. // var_dump($pages);
  19. // echo $pages;
  • page-list.php
  1. <?php
  2. // 引入分页数据
  3. require "page-data.php";
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <title>数据分页</title>
  11. <link rel="stylesheet" href="style.css">
  12. </head>
  13. <body>
  14. <!-- 显示数据表的信息 -->
  15. <table>
  16. <caption><h2>用户信息表<h2></caption>
  17. <thead>
  18. <tr>
  19. <th>id</th>
  20. <th>用户名</th>
  21. <th>性别</th>
  22. <th>年龄</th>
  23. <th>联系电话</th>
  24. <th>操作</th>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <!-- 使用模板语法循环每个用户信息 -->
  29. <?php foreach ($users as $user): ?>
  30. <tr>
  31. <!-- 使用短标签简化变量的显示 -->
  32. <td><?=$user['id']?></td>
  33. <td><?=$user['username']?></td>
  34. <td><?=$user['sex']?></td>
  35. <td><?php echo $user['age']; ?></td>
  36. <td><?php echo $user['tel']; ?></td>
  37. <td>
  38. <!-- 点击编辑时跳转到一个编辑页面,通过GET方式 -->
  39. <button onclick="location.href='handle.php?action=edit&id=<?=$user['id']?>'">编辑</button>
  40. <!-- 点击删除时,调用del()函数 -->
  41. <button onclick="del(<?=$user['id']?>)">删除</button>
  42. </td>
  43. </tr>
  44. <?php endforeach ?>
  45. </tbody>
  46. </table>
  47. <!-- 分页条 -->
  48. <div>
  49. <!-- 2. 首页 -->
  50. <a href="<?=$_SERVER['PHP_SELF'].'?p=1'?>">首页</a>
  51. <!-- 3.上一页 -->
  52. <?php
  53. $prev = $page - 1;
  54. // 当前页数为第一页时就不再前移
  55. if ($page == 1) $prev = 1;
  56. ?>
  57. <!-- 当前页为第一页时,不显示上一页 -->
  58. <?php if ($page != 1) : ?>
  59. <a href="<?=$_SERVER['PHP_SELF'].'?p='.$prev?>">上一页</a>
  60. <?php endif ?>
  61. <!-- 1. 分页条显示数量 -->
  62. <!-- 1.1 小于10时 -->
  63. <?php if ($page <= 10) : ?>
  64. <?php for($i=1; $i<=10; $i++): ?>
  65. <!-- 点击分页条时跳转 -->
  66. <?php $jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i); ?>
  67. <!-- 当前页码高亮 -->
  68. <?php
  69. // 当前页数和url中的参数p 相等时高亮
  70. $active = ($i == $page) ? 'active' : null;
  71. ?>
  72. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
  73. <?php endfor ?>
  74. <a>...</a>
  75. <!-- 显示最后两页,这里不需要加class,因为后面还会有判断 -->
  76. <a href="<?=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$pages-1)?>"><?=$pages-1?></a>
  77. <a href="<?=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$pages)?>"><?=$pages?></a>
  78. <?php endif ?>
  79. <!-- 1.2 大于10且小于最后10页时 -->
  80. <?php if ($page > 10 && $page<$pages-10) : ?>
  81. <!-- 显示1、2 + 左3 + 当前 + 右3 + 最后两页 -->
  82. <!-- 显示1、2 -->
  83. <a href="<?=sprintf('%s?p=1',$_SERVER['PHP_SELF'])?>">1</a>
  84. <a href="<?=sprintf('%s?p=2',$_SERVER['PHP_SELF'])?>">2</a>
  85. <a>...</a>
  86. <!-- 显示左3、当前、右3 -->
  87. <?php for ($i=$page-3;$i<=$page+3;$i++) : ?>
  88. <?php $jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i); ?>
  89. <?php $active = ($i == $page) ? 'active' : null; ?>
  90. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
  91. <?php endfor ?>
  92. <a>...</a>
  93. <!-- 显示最后两页 -->
  94. <a href="<?=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$pages-1)?>"><?=$pages-1?></a>
  95. <a href="<?=sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$pages)?>"><?=$pages?></a>
  96. <?php endif ?>
  97. <!-- 1.3 大于最后10页时 -->
  98. <!-- 显示1、2、最后10页 -->
  99. <?php if ($page>=$pages-10) : ?>
  100. <!-- 显示1、2 -->
  101. <a href="<?=sprintf('%s?p=1',$_SERVER['PHP_SELF'])?>">1</a>
  102. <a href="<?=sprintf('%s?p=2',$_SERVER['PHP_SELF'])?>">2</a>
  103. <a>...</a>
  104. <!-- 显示最后10页 -->
  105. <?php for ($i=$pages-10;$i<=$pages;$i++) :?>
  106. <?php $jump = sprintf('%s?p=%s',$_SERVER['PHP_SELF'],$i); ?>
  107. <?php $active = ($i == $page) ? 'active' : null; ?>
  108. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
  109. <?php endfor ?>
  110. <?php endif ?>
  111. <!-- 4.尾页 -->
  112. <a href="<?=$_SERVER['PHP_SELF'].'?p='.$pages?>">尾页</a>
  113. <!-- 5.下一页 -->
  114. <?php
  115. $next = $page + 1;
  116. // 下一页页数大于等于总页数时,不再后移
  117. if ($next >= $pages) $next=$pages;
  118. ?>
  119. <!-- 当前页是最后一页时,不显示下一页 -->
  120. <?php if ($page != $pages) : ?>
  121. <a href="<?=$_SERVER['PHP_SELF'].'?p='.$next?>">下一页</a>
  122. <?php endif ?>
  123. </div>
  124. <script>
  125. // del()函数:点击确定时跳转到handle.php进行数据删除操作
  126. function del(id){
  127. var isDelete = confirm('此操作不可恢复,确定删除吗?')
  128. if (isDelete) {
  129. location.href = 'handle.php?action=delete&id='+id;
  130. }else{
  131. return false;
  132. }
  133. }
  134. </script>
  135. </body>
  136. </html>
  • handle.php
  1. <?php
  2. // 连接数据库
  3. require "connect.php";
  4. $action = $_GET['action'];
  5. $id = $_GET['id'];
  6. switch ($action)
  7. {
  8. // 点击编辑按钮时
  9. case 'edit' :
  10. // 加载渲染表单
  11. include "edit.php";
  12. break;
  13. // 在edit.php编辑表单中,提交新编辑的数据过来时,执行更新操作
  14. case 'doedit' :
  15. $sql = "UPDATE `student` SET `username`=?, `sex`=?, `age`=?, `tel`=? WHERE `id`=?";
  16. $stmt = $pdo->prepare($sql);
  17. // 新编辑的数据以POST的方式提交过来
  18. if(!empty($_POST))
  19. $stmt->execute([$_POST['username'],$_POST['sex'],$_POST['age'],$_POST['tel'],$id]);
  20. if($stmt->rowCount()==1)
  21. {
  22. echo "
  23. <script>
  24. alert('更新信息成功');
  25. location.href = 'page-list.php';
  26. </script>
  27. ";
  28. }
  29. break;
  30. // 点击删除按钮并确定时,执行删除操作
  31. case 'delete' :
  32. $sql = "DELETE FROM `student` WHERE `id`=?";
  33. $stmt = $pdo->prepare($sql);
  34. $stmt->execute([$id]);
  35. if( $stmt->rowCount() == 1 )
  36. {
  37. echo "
  38. <script>
  39. alert('删除信息成功');
  40. location.href = 'page-list.php';
  41. </script>
  42. ";
  43. }
  44. break;
  45. }
  • edit.php
  1. <?php
  2. // 通过GET传过来的id,将用户信息取到
  3. $sql = "SELECT * FROM `student` WHERE `id`={$id}";
  4. $user = $pdo->query($sql)->fetch();
  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. <link rel="stylesheet" href="edit.css">
  12. <title>用户编辑</title>
  13. </head>
  14. <body>
  15. <h2>用户信息编辑</h2>
  16. <table>
  17. <caption><h3>原始信息如下:<h3></caption>
  18. <thead>
  19. <tr>
  20. <td>id</td>
  21. <td>用户名</td>
  22. <td>性别</td>
  23. <td>年龄</td>
  24. <td>联系电话</td>
  25. </tr>
  26. </thead>
  27. <tbody>
  28. <tr>
  29. <td><?=$user['id']?></td>
  30. <td><?=$user['username']?></td>
  31. <td><?=$user['sex']?></td>
  32. <td><?=$user['age']?></td>
  33. <td><?=$user['tel']?></td>
  34. </tr>
  35. </tbody>
  36. </table>
  37. <hr>
  38. <h3>请编辑您的要修改的信息:</h3>
  39. <form action="<?=$_SERVER['PHP_SELF'].'?action=doedit&id='.$id?>" method="POST">
  40. <div>
  41. <label for="username">用户名:</label>
  42. <input type="text" name="username" id="username">
  43. </div>
  44. <div>
  45. <label for="male">性别:</label>
  46. <div>
  47. <input type="radio" name="sex" id="male" value="男">
  48. <input type="radio" name="sex" id="female" value="女">
  49. </div>
  50. </div>
  51. <div>
  52. <label for="age">年龄:</label>
  53. <input type="text" name="age" id="age">
  54. </div>
  55. <div>
  56. <label for="tel">电话:</label>
  57. <input type="tel" name="tel" id="tel">
  58. </div>
  59. <button>保存</button>
  60. </form>
  61. </body>
  62. </html>
  • style.css
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. /* 表格样式 */
  7. table {
  8. border: 1px solid black;
  9. border-collapse: collapse;
  10. text-align: center;
  11. width: 800px;
  12. margin: auto;
  13. }
  14. th,
  15. td {
  16. border: 1px solid black;
  17. line-height: 50px;
  18. }
  19. table h2 {
  20. padding: 10px 0;
  21. }
  22. th {
  23. background-color: lightcoral;
  24. }
  25. tr:hover {
  26. background-color: lightcyan;
  27. }
  28. button {
  29. padding: 5px 15px;
  30. background-color: rgb(9, 9, 71);
  31. color: white;
  32. border-radius: 5px;
  33. outline: none;
  34. border: none;
  35. }
  36. button:hover {
  37. background-color: rgb(77, 77, 163);
  38. cursor: pointer;
  39. }
  40. /* 分页条样式 */
  41. div {
  42. text-align: center;
  43. margin-top: 30px;
  44. }
  45. div > a {
  46. text-decoration: none;
  47. color: black;
  48. padding: 5px 10px;
  49. vertical-align: middle;
  50. margin: 0 5px;
  51. border-radius: 5px;
  52. }
  53. /* 当前页码高亮 */
  54. .active {
  55. background-color: red;
  56. color: white;
  57. }
  • edit.css
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. body {
  7. display: flex;
  8. flex-flow: column nowrap;
  9. align-items: center;
  10. }
  11. h2 {
  12. padding: 20px 0;
  13. border-bottom: 1px solid black;
  14. }
  15. table {
  16. border: 1px solid black;
  17. border-collapse: collapse;
  18. width: 500px;
  19. }
  20. table td {
  21. border: 1px solid black;
  22. line-height: 50px;
  23. text-align: center;
  24. }
  25. table > caption > h3 {
  26. padding: 10px 0;
  27. }
  28. form > div {
  29. display: grid;
  30. grid-template-columns: 80px 300px;
  31. grid-template-rows: 30px;
  32. margin: 10px 0;
  33. }
  34. body > h3 {
  35. padding-bottom: 10px;
  36. margin-top: 50px;
  37. }
  38. form {
  39. text-align: center;
  40. }
  41. form > div:nth-of-type(2) input {
  42. margin: 0 20px;
  43. }
  44. button {
  45. width: 100px;
  46. padding: 5px 0;
  47. outline: none;
  48. }
  49. button:hover {
  50. cursor: pointer;
  51. background-color: black;
  52. color: white;
  53. }

2. 分页效果如下:

3 总结

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
2 comments
果冻 2020-08-05 14:06:36
老周威武!
2 floor
老周 2020-08-04 22:15:56
你是来踢馆的,鉴定完毕~~~~
1 floor
Author's latest blog post