Blogger Information
Blog 43
fans 1
comment 0
visits 33971
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分页数据展示
蔚蓝世纪
Original
672 people have browsed it
PHP分页数据输出效果预览

基本流程

一、建立数据库连接桥梁connect.php

代码演示:
  1. <?php
  2. $dsn = 'mysql:host=localhost;dbname=phpedu;charset=utf8;port=3306';
  3. $username = 'root';
  4. $password = 'root';
  5. try {
  6. $pdo = new PDO($dsn, $username, $password);
  7. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  8. } catch (Exception $e) {
  9. die($e->getMessage());
  10. }

二、连接数据库并设置分页数据

代码演示:
  1. <?php
  2. require 'connect.php';// 连接数据库
  3. $page = $_GET['p'] ?? 1;//设置当前的页数/页码
  4. $num = 5;// 设置每页显示的记录数量
  5. $sql = "SELECT CEIL(COUNT(`id`)/{$num}) AS `total` FROM `staffs`";
  6. $pages = $pdo->query($sql)->fetch()['total'];// 设置总页数
  7. $offset = $num * ($page - 1);// 设置偏移量
  8. $sql = "SELECT * FROM `staffs` LIMIT {$num} OFFSET {$offset}";
  9. $staffs = $pdo->query($sql)->fetchAll();// 设置分页数据

三、导入数据库,遍历数组

代码演示:
  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. <link rel="stylesheet" href="style.css">
  9. </head>
  10. <body>
  11. <nav>
  12. <?php if (isset($user)) : ?>
  13. <a href="" id="logout"><span style="color:red"><?php echo $user['name']?></span>&nbsp;&nbsp;退出</a>
  14. <?php else: ?>
  15. <ul>
  16. <li><a href="#" title="首页" target="_blank">首页</a></li>
  17. <li><a href="#" title="音乐" target="_blank">音乐</a></li>
  18. <li><a href="#" title="舞蹈" target="_blank">舞蹈</a></li>
  19. <li><a href="#" title="绘画" target="_blank">绘画</a></li>
  20. <li><a href="#" title="雕塑" target="_blank">雕塑</a></li>
  21. <li><a href="#" title="书法" target="_blank">书法</a></li>
  22. <li><a href="#" title="戏曲" target="_blank">戏曲</a></li>
  23. <li><a href="#" title="相声" target="_blank">相声</a></li>
  24. <li><a href="#" title="小品" target="_blank">小品</a></li>
  25. <li><a href="#" title="戏曲" target="_blank">非遗</a></li>
  26. <li><a href="session/login.php" title="登录" target="_blank">登录</a></li>
  27. <li><a href="session/register.php" title="注册" target="_blank">注册</a></li>
  28. </ul>
  29. </nav>
  30. <?php endif ?>
  31. <table>
  32. <caption>文化馆职工管理系统</caption>
  33. <thead>
  34. <tr>
  35. <th>I D</th>
  36. <th>姓 名</th>
  37. <th>年 龄</th>
  38. <th>性 别</th>
  39. <th>职 位</th>
  40. <th>手 机</th>
  41. <th>入职时间</th>
  42. <th>操 作</th>
  43. </tr>
  44. </thead>
  45. <tbody>
  46. <?php foreach ($staffs as $staff) : ?>
  47. <tr>
  48. <td><?php echo $staff['id'] ?></td>
  49. <td><?php echo $staff['name'] ?></td>
  50. <td><?php echo $staff['age'] ?></td>
  51. <td><?php echo $staff['sex'] ? '男' : '女' ?></td>
  52. <td><?php echo $staff['position'] ?></td>
  53. <td><?php echo $staff['mobile'] ?></td>
  54. <td><?php echo date('Y-m-d', $staff['hiredate']) ?></td>
  55. <td><button onclick="location.href='handle.php?action=edit&id=<?php echo $staff['id'] ?>'">编辑</button>
  56. <button onclick="del(<?php echo $staff['id'] ?>)">删除</button></td>
  57. </tr>
  58. <?php endforeach; ?>
  59. </tbody>
  60. </table>
  61. <!-- 添加跳转到首页, 前一页, 下一页, 尾页的功能 -->
  62. <p>
  63. <?php
  64. $showPages = 5;// 分页条显示5个页码
  65. $startPage = 1;// 分页条的起始页码
  66. $endPage = $pages; // 分页条的终止页码
  67. $offsetPage = ($showPages -1) / 2; // (当前分页条显示的页码数 - 1) / 2
  68. // 设置显示省略标记
  69. if ($showPages < $pages) {
  70. if ($page > $offsetPage + 1) {
  71. $startOmit = '...';
  72. }
  73. if ($page > $offsetPage) {
  74. $startPage = $page - $offsetPage;
  75. $endPage = $page + $offsetPage;
  76. if ($endPage > $pages) {$endPage = $pages;}
  77. } else {
  78. $startPage = 1;
  79. $endPage = $showPages;
  80. }
  81. if ($page + $offsetPage > $pages) {
  82. $startPage = $startPage - ($page + $offsetPage - $endPage);
  83. }
  84. if ($showPages < $pages && $page + $offsetPage < $pages) $endOmit = '...';
  85. }
  86. ?>
  87. <!-- 首页 -->
  88. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=1' ?>">首页</a>
  89. <!-- 前一页 -->
  90. <?php
  91. $prev = $page - 1;
  92. if ($page == 1) $prev = 1;
  93. ?>
  94. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $prev ?>">前一页</a>
  95. <?php if (isset($startOmit)) : ?> <a href="#"><?php echo $startOmit ?></a> <?php endif ?>
  96. <?php for ($i=$startPage; $i<=$endPage; $i++): ?>
  97. <?php
  98. $jump = sprintf('%s?p=%s', $_SERVER['PHP_SELF'], $i );
  99. $active = ($i == $page) ? 'active' :null;
  100. ?>
  101. <a href="<?php echo $jump ?>" class="<?php echo $active ?>"><?php echo $i ?></a>
  102. <?php endfor ?>
  103. <?php if (isset($endOmit)) : ?> <a href="#"><?php echo $endOmit ?></a> <?php endif ?>
  104. <!-- 下一页 -->
  105. <?php
  106. $next = $page + 1;
  107. if ($page == $pages) $next = $pages;
  108. ?>
  109. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p=' . $next ?>">下一页</a>
  110. <!-- 尾页 -->
  111. <a href="<?php echo $_SERVER['PHP_SELF'] . '?p='. $pages ?>">尾页</a>
  112. </p>
  113. <script>
  114. function del(id) {
  115. return confirm('是否删除?') ? alert('删除成功') : false;
  116. }
  117. </script>
  118. </body>
  119. </html>

四、创建编辑员工信息数据文件edit.php

代码演示:
  1. <?php
  2. // 获取被编辑的员工信息
  3. $staff = $pdo->query("SELECT * FROM `staffs` WHERE `id`={$id}")->fetch();
  4. // print_r($staff);//测试打印数据
  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>Document</title>
  12. </head>
  13. <body>
  14. <h3>编辑员工信息</h3>
  15. <form action="<?php echo $_SERVER['PHP_SELF'].'?action=doedit&id='. $id ?>" method="post">
  16. <p>
  17. <label for="name">姓名</label>
  18. <input type="text" id="name" name="name" value="<?php echo $staff['name'] ?>">
  19. </p>
  20. <p>
  21. <label for="age">年龄:</label>
  22. <input type="number" id="age" name="age" value="<?php echo $staff['age'] ?>">
  23. </p>
  24. <input type="hidden" name="id" value="<?php echo $staff['id'] ?>">
  25. <p>
  26. <label for="sex">性别:</label>
  27. <input type="radio" id="sex" name="sex" value="1" <?php if ($staff['sex'] == 1) echo 'checked' ?>><label for=""></label>
  28. <input type="radio" id="sex" name="sex" value="0" <?php if ($staff['sex'] == 0) echo 'checked' ?>><label for=""></label>
  29. </p>
  30. <p>
  31. <label for="position">职位:</label>
  32. <input type="text" id="position" name="position" value="<?php echo $staff['position'] ?>">
  33. </p>
  34. <p>
  35. <label for="mobile">手机号:</label>
  36. <input type="text" id="tel" name="mobile" value="<?php echo $staff['mobile'] ?>">
  37. </p>
  38. <p>
  39. <label for="hiredate">手机号:</label>
  40. <input type="text" id="hiredate" name="hiredate" value="<?php echo date('Y-m-d', $staff['hiredate']) ?>">
  41. </p>
  42. <p>
  43. <button>保存</button>
  44. </p>
  45. </form>
  46. </body>
  47. </html>
输出效果:

五、删除信息数据

代码演示:
  1. <?php
  2. require 'demo1.php';
  3. $action = $_GET['action'];
  4. $id = $_GET['id'];
  5. switch ($action) {
  6. // 编辑需要进行两步
  7. // 1. 渲染编辑表单
  8. case 'edit':
  9. include 'edit.php';
  10. break;
  11. // 2. 执行编辑操作
  12. case 'doedit':
  13. $sql = 'UPDATE `staffs` SET `name`=:name, `age`=:age,`sex`=:sex,`position`=:position ,`mobile`=:mobile,`hiredate`=:hiredate WHERE `id`=:id';
  14. // print_r($_POST);
  15. // $_POST['id'] = $id;
  16. $_POST['hiredate'] = strtotime($_POST['hiredate']);
  17. $stmt = $pdo->prepare($sql);
  18. $stmt->execute($_POST);
  19. if ($stmt->rowCount() === 1) echo '<script>alert("更新成功");location.href="demo4.php";</script>';
  20. // 3. 删除员工数据
  21. case 'del':
  22. $stmt = $pdo->query('delete from `staffs` where `id`=$id');
  23. if ($stmt->rowCount() === 1) echo '<script>alert("删除成功");location.href="demo4.php";</script>';
  24. break;
  25. }
输出效果:

六、总结

对于在服务器上处理表格数据有了大体的概念,虽然目前学到的知识是零散的,当然还有些迷惑,但是脑子里已经有了基本的呈现思路,对于呈现这些思路的工具和命令必然要十分熟悉,而且我认为获得最佳的用户体验(俗称用得顺手)是程序员不懈追求的动力。

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