博客列表 >分页查询、服务容器

分页查询、服务容器

乐作人生
乐作人生原创
2020年11月02日 12:01:01700浏览
  1. 数据表的分页查询

    1. <?php require '0912-2.php' ?>
    2. <?php
    3. // 省略号设置:必须将显示的分页码的数量固定下来,当大于这个数量的时候才显示省略号
    4. // 新分页条的页码数量:一定是奇数
    5. $showPages = 5;
    6. // 新分页条的起始页码的初值,默认是1
    7. $startPage = 1;
    8. // 新分页条的终止页码,默认是总页数
    9. $endPage = $pages;
    10. // 偏移量:新分页条相当于当前页码的偏移量,当前页码与省略号之间的间距,新分页条的偏移量是2
    11. $offsetPage = ($showPages - 1) /2 ;
    12. // 新分页条中的页码数量 小于 总页数才出现...
    13. if ($showPages < $pages) {
    14. // 只有当前的页码 大于偏移量 加1 时,才显示左边的省略标记 ... ;只有当页码大于3的时候才会出现
    15. if ($page > $offsetPage + 1) $startOmit = '...';
    16. // 如果当前页 大于 偏移量时,需要改变新分页条的起始页码和终止页码
    17. if ($page > $offsetPage) {
    18. // 当前分页条的起始页码 = 当前页码 - 偏移量(本质就是页码前移)
    19. $startPage = $page - $offsetPage;// 如果当前页码是5,起始应为5-2=3
    20. // 重置分页条的结束页码 = 当前页码 + 偏移量
    21. $endPage = $page + $offsetPage;
    22. // 当结束页码越界时,新分页条的终止页码设置为总页数
    23. if ($endPage > $pages) {
    24. $endPage = $pages;
    25. // 起始页码设置为总页数-(展示页数-1)
    26. $startPage = $pages - ($showPages - 1);
    27. }
    28. } else {
    29. // 如果当前页码 小于 偏移量;新分页条的起始页是1
    30. $startPage = 1;
    31. // 新分页条的结束页码 = 展示页数的值
    32. $endPage = $showPages;
    33. }
    34. // 当前页码大于展示页数的值,并且当前页码+偏移量仍小于总页数,显示右边的省略号
    35. // 只要当前显示的页码数量, 小于 总页数并且当前页加上偏移量也小于总页数,应该显示出后面的 ...
    36. if ($pages > $page && $page+$offsetPage < $pages) $endOmit = '...';
    37. }
    38. ?>
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>带省略号的分页数据</title>
    7. </head>
    8. <style>
    9. * {
    10. margin: 0;
    11. padding: 0;
    12. box-sizing: border-box;
    13. }
    14. ul{
    15. list-style:none;
    16. }
    17. body {
    18. display: flex;
    19. flex-direction: column;
    20. align-items: center;
    21. }
    22. table{
    23. width:80%;
    24. border:1px solid;
    25. border-collapse: collapse;
    26. text-align: center;
    27. }
    28. table caption {
    29. font-size: 1.2rem;
    30. margin: 10px;
    31. }
    32. table td,table th {
    33. border: 1px solid;
    34. padding: 5px;
    35. }
    36. table tr:hover {
    37. background-color: #eee;
    38. }
    39. table thead tr:only-of-type {
    40. background-color: lightblue;
    41. }
    42. table button {
    43. width: 56px;
    44. height: 26px;
    45. }
    46. table button:last-of-type {
    47. color: red;
    48. }
    49. table button {
    50. cursor: pointer;
    51. margin: 0 3px;
    52. }
    53. /* 分页 */
    54. .paging>p{
    55. display:flex;
    56. }
    57. .paging >p>a{
    58. text-decoration: none;
    59. color: #555;
    60. border: 1px solid;
    61. padding: 5px 10px;
    62. margin: 10px 2px;
    63. }
    64. .paging >p>a.active {
    65. background-color: red;
    66. color: white;
    67. border: 1px solid red;
    68. }
    69. </style>
    70. <body>
    71. <table>
    72. <caption>用户信息表</caption>
    73. <thead>
    74. <tr>
    75. <th>ID</th>
    76. <th>姓名</th>
    77. <th>邮箱</th>
    78. <th>操作</th>
    79. </tr>
    80. </thead>
    81. <tbody>
    82. <?php foreach($users as $user): ?>
    83. <tr>
    84. <td><?php echo $user['id'] ?></td>
    85. <td><?=$user['name']?></td><!-- 使用php短标签来快速打印变量 -->
    86. <td><?php echo $user['email'] ?></td>
    87. <td><button onclick="location.href='handle.php?action=edit&id=<?=$user['id']?>'">编辑</button>
    88. <button onclick="del(<?=$user['id']?>)">删除</button>
    89. </td>
    90. </tr>
    91. <?php endforeach ?>
    92. </tbody>
    93. </table>
    94. <div class="paging">
    95. <p>
    96. <!-- 当前页数不等于1时,显示上一页和首页,否则全部隐藏 -->
    97. <?php if($page!=1) :?>
    98. <!-- 获取上一页的页码 -->
    99. <?php $prev=$page-1;if($page<=1) $prev=1 ?>
    100. <a href="<?=$_SERVER['PHP_SELF'].'?p=1'?>">首页</a>
    101. <a href="<?=$_SERVER['PHP_SELF'].'?p='.$prev?>">上一页</a>
    102. <!-- 判断是否有值 -->
    103. <?php if(isset($startOmit)) :?>
    104. <a href><?=$startOmit?></a>
    105. <?php endif ?>
    106. <?php endif ?>
    107. <?php for($i=$startPage;$i<=$endPage;$i++) :?>
    108. <?php
    109. // 页码的跳转地址
    110. $jump = sprintf('%s?p=%s', $_SERVER['PHP_SELF'], $i);
    111. // 设置当前页码高亮显示
    112. $active = ($i == $page) ? 'active' : null;
    113. ?>
    114. <a href="<?=$jump?>" class="<?=$active?>"><?=$i?></a>
    115. <?php endfor ?>
    116. <!-- 当前页数不等于总页数时,显示下一页和尾页,否则全部隐藏 -->
    117. <?php if($page!=$pages) :?>
    118. <?php if(isset($endOmit)) :?>
    119. <a href><?=$endOmit?></a>
    120. <?php endif ?>
    121. <!-- 获取下一页的页码 -->
    122. <?php $next=$page+1;if($page>=$pages) $next=$pages ?>
    123. <a href="<?=$_SERVER['PHP_SELF'].'?p='.$next?>">下一页</a>
    124. <a href="<?=$_SERVER['PHP_SELF'].'?p='.$pages?>">尾页</a>
    125. <?php endif ?>
    126. </p>
    127. </div>
    128. <script>
    129. function del(id) {
    130. let url = 'http://php.edu/php作业/0912/handle.php?action=delete&id='+id;
    131. return confirm('是否删除?') ? location.href= url : false;
    132. }
    133. </script>
    134. </body>
    135. </html>


  • 删除、新增操作
    • handle.php
      1. <?php
      2. // 1、连接数据库
      3. $dsn = 'mysql:host=localhost;dbname=phpedu';
      4. $username = 'root';
      5. $password = 'root';
      6. $pdo = new PDO($dsn, $username, $password);
      7. // 2、获取操作类型
      8. $action= $_GET['action'];
      9. $id=$_GET['id'];
      10. // 3、操作
      11. switch ($action){
      12. // 第一步 渲染出一个编辑表单
      13. case 'edit':
      14. // 加载编辑表单
      15. include 'edit.php';
      16. break;
      17. // 第二步 执行编辑操作,将新数据写入数据表中
      18. case 'doedit':
      19. // 更新操作
      20. $sql = 'UPDATE `user` SET `name`=?, `email`=? WHERE `id`=?;';
      21. $stmt = $pdo->prepare($sql);
      22. if (!empty($_POST)) {
      23. $stmt->execute([$_POST['name'], $_POST['email'], $id]);
      24. if ($stmt->rowCount() == 1)
      25. echo '<script>alert("更新成功");location.href="0912-4.php";</script>';
      26. }
      27. break;
      28. case 'insert':
      29. //新增数据
      30. $stmt = $pdo->prepare('INSERT INTO `user` SET `name`=?,`email`=?,`password`=?;');
      31. $stmt->execute([$_POST['name'], $_POST['email'], sha1($_POST['password'])]);
      32. // echo $stmt;
      33. if($stmt->rowCount() == 1){
      34. echo '<script>alert("添加成功");location.href="0912-4.php";</script>';
      35. }
      36. break;
      37. case 'delete':
      38. // 删除操作
      39. $stmt = $pdo->prepare('DELETE FROM `user` WHERE `id`=?;');
      40. $stmt->execute([$id]);
      41. if ($stmt->rowCount() == 1)
      42. echo '<script>alert("删除成功");location.href="0912-4.php";</script>';
      43. break;
      44. }
    • 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="<?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="<?=$user['name']?>"/>
      19. </p>
      20. <p>
      21. <label for="email">邮箱:</label>
      22. <input type="text" id="email" name="email" value="<?=$user['email']?>"/>
      23. </p>
      24. <p><button>保存</button></p>
      25. </form>
      26. </body>
      27. </html>
    • 用户添加页面
      1. <!DOCTYPE html>
      2. <html lang="en">
      3. <head>
      4. <meta charset="UTF-8">
      5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
      6. <title>用户添加</title>
      7. </head>
      8. <body>
      9. <h3>用户添加</h3>
      10. <form action="<?php echo 'http://php.edu/php%E4%BD%9C%E4%B8%9A/0912/handle.php?action=insert' ?>" method="post">
      11. <p>
      12. <label for="name">用户名:</label>
      13. <input type="text" id="name" name="name"/>
      14. </p>
      15. <p>
      16. <label for="email">邮箱:</label>
      17. <input type="text" id="email" name="email"/>
      18. </p>
      19. <p>
      20. <label for="password">密码:</label>
      21. <input type="text" id="password" name="password"/>
      22. </p>
      23. <p><button>保存</button></p>
      24. </form>
      25. </body>
      26. </html>

  1. 服务容器
  • Model
    1. <?php
    2. //model模型
    3. namespace mvc_demo;
    4. use PDO;//声明命名空间后需进行use别名设置
    5. //数据库操作
    6. class Model
    7. {
    8. //数据获取
    9. public function getData(){
    10. return (new PDO('mysql:host=localhost;dbname=phpedu', 'root','root'))
    11. ->query('select id,name,email from user limit 5')
    12. ->fetchAll(PDO::FETCH_ASSOC);
    13. }
    14. }
    15. ?>
  • View
    1. <?php
    2. //视图
    3. namespace mvc_demo;
    4. class View
    5. {
    6. // 将模型中获取的数据进行展示
    7. public function fetch($data){
    8. $table='<table>';
    9. $table .= '<caption>用户信息表</caption>';
    10. $table .= '<tr><th>ID</th><th>姓名</th><th>邮箱</th></tr>';
    11. // 遍历用户数据
    12. foreach ($data as $user) {
    13. $table .= '<tr>';
    14. $table .= '<td>'.$user['id'].'</td>';
    15. $table .= '<td>'.$user['name'].'</td>';
    16. $table .= '<td>'.$user['email'].'</td>';
    17. $table .='</tr>';
    18. }
    19. $table .='</table>';
    20. return $table;
    21. }
    22. }
    23. echo '<style>
    24. table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
    25. caption {font-size: 1.2rem; margin-bottom: 10px;}
    26. tr:first-of-type { background-color:yellow;}
    27. td,th {border: 1px solid; padding:5px}
    28. </style>';
    29. ?>
  • Controller
    1. <?php
    2. //控制器4:将当前依赖的多个对象,放在一个"服务容器"的对象池中进行统一管理
    3. namespace mvc_demo;
    4. use Closure;
    5. //加载模型
    6. require __DIR__ .'/Model.php';
    7. //加载视图
    8. require __DIR__ .'/View.php';
    9. //服务容器
    10. class Container
    11. {
    12. //对象容器
    13. protected $instances=[];
    14. //添加对象:参数1:对象别名;参数2:对象(new)
    15. public function bind($alias,Closure $process){
    16. $this->instances[$alias]=$process;
    17. }
    18. //容器内取出对象(实例化)
    19. public function make($alias,$params=[]){
    20. // 回调执行
    21. return call_user_func_array($this->instances[$alias],[]);
    22. }
    23. }
    24. //将依赖的外部对象绑定到服务容器中
    25. $container=new Container;
    26. //绑定模型、视图
    27. $container->bind('model',function(){return new Model();});
    28. $container->bind('view',function(){return new View();});
    29. //控制器
    30. class Controller4
    31. {
    32. public function index(Container $container){
    33. //获取模型中的数据
    34. $data = $container->make('model')->getData();
    35. //渲染模板视图
    36. return $container->make('view')->fetch($data);
    37. }
    38. }
    39. //客户端测试
    40. $controller=new Controller4();//实例化
    41. echo $controller->index($container);
    42. ?>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议