Blogger Information
Blog 17
fans 1
comment 0
visits 20141
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分页_MVC_服务容器综合示例
大A
Original
791 people have browsed it

代码

model.php

  1. <?php
  2. class Db
  3. {
  4. private $pdo;
  5. public function __construct()
  6. {
  7. $this->pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  8. }
  9. //查询显示数据
  10. public function table_select($limit, $page, &$count)
  11. {
  12. $sql = "SELECT COUNT(*) AS 'count' FROM `staffs`";
  13. $sql_obj = $this->pdo->prepare($sql);
  14. $sql_obj->execute();
  15. $count = ceil($sql_obj->fetch()['count'] / 10);
  16. $offset = $limit * ($page - 1);
  17. $sql = "SELECT * FROM `staffs` LIMIT $limit OFFSET $offset";
  18. $sql_obj = $this->pdo->prepare($sql);
  19. $sql_obj->execute();
  20. return $sql_obj->fetchall();
  21. }
  22. //更新数据
  23. public function update($array)
  24. {
  25. extract($array);
  26. $time = strtotime($time);
  27. $sql = "UPDATE `staffs` SET `name`='$name',`sex`='$sex',`position`='$position',`tel`='$tel',`time`='$time' WHERE `id`='$id'";
  28. $sql_obj = $this->pdo->prepare($sql);
  29. $sql_obj->execute();
  30. if ($sql_obj->rowCount() > 0) :
  31. return true;
  32. else :
  33. return false;
  34. endif;
  35. }
  36. //删除数据
  37. public function del($id)
  38. {
  39. $sql="DELETE FROM `staffs` WHERE `id`=$id";
  40. $sql_obj = $this->pdo->prepare($sql);
  41. $sql_obj->execute();
  42. if ($sql_obj->rowCount() > 0) :
  43. return true;
  44. else :
  45. return false;
  46. endif;
  47. }
  48. }

view.php

  1. <?php
  2. class View
  3. {
  4. private $html = <<< head
  5. <link rel="stylesheet" href="style.css">
  6. <table>
  7. <caption>会员管理系统</caption>
  8. <tr>
  9. <th>ID</th><th>姓名</th><th>性别</th><th>职业</th><th>电话</th><th>注册时间</th><th>操作</th>
  10. </tr>
  11. head;
  12. //显示主页
  13. public function index($array, $count, $page)
  14. {
  15. $path = $_SERVER['PHP_SELF'];
  16. $tr = <<< tr
  17. <tr>
  18. <th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th><th>%s</th>
  19. <tr>
  20. tr;
  21. $deit = <<<button
  22. <button onclick="location.href='%s'">编辑</button>
  23. <button onclick="location.href='%s'" style="color:red">删除</button>
  24. button;
  25. foreach ($array as $value) {
  26. $time = date('Y/m/d', $value[5]);
  27. $request_edit = $path . "?action=edit&ID=$value[0]&name=$value[1]&sex=$value[2]&position=$value[3]&tel=$value[4]&time=$time";
  28. $request_del = $path . '?action=del&id=' . $value[0];
  29. $html .= sprintf(
  30. $tr,
  31. $value[0],
  32. $value[1],
  33. $value[2],
  34. $value[3],
  35. $value[4],
  36. $time,
  37. sprintf($deit, $request_edit, $request_del)
  38. );
  39. }
  40. return $this->html . $html . '</table>' . $this->show_page($count, $page);
  41. }
  42. //显示分页页码
  43. private function show_page($count, $page)
  44. {
  45. $path = $_SERVER['PHP_SELF'];
  46. $a = "<a href='$path?p=%s' class='%s'>%s</a>";
  47. $start = $page - 3;
  48. if ($start < 1) : $start = 1;
  49. else : $frist = true;
  50. endif;
  51. if ($count - $page < 3) $start = $count - 6;
  52. if ($page <= 1) $page = 1;
  53. $html .= sprintf($a, $page - 1 ? $page - 1 : 1, '', '上一页');
  54. if ($frist && $page > 5) :
  55. $html .= sprintf($a, 1, '', '1');
  56. $html .= sprintf($a, 1, '', '2');
  57. endif;
  58. for ($i = $start; $i < $start + 7; $i++) {
  59. $class = $i == $page ? 'active' : '';
  60. $title = $i;
  61. if ($i == ($start + 6) && $count - $page > 3) $title = '......';
  62. if ($frist) : $title = '......';
  63. $frist = false;
  64. endif;
  65. $html .= sprintf($a, $i, $class, $title);
  66. }
  67. if ($count - $page > 4) :
  68. $html .= sprintf($a, $count - 1, '', $count - 1);
  69. $html .= sprintf($a, $count, '', $count);
  70. endif;
  71. $html .= sprintf($a, ($page + 1) < $count ? $page + 1 : $count, '', '下一页');
  72. return '<p>' . $html . '</p>';
  73. }
  74. public function edit($array)
  75. {
  76. extract($array);
  77. $request = $_SERVER['PHP_SELF'] . '?action=update';
  78. $html = require('edit.php');
  79. return sprintf($html, $request, $ID, $name, $sex, $position, $tel, $time);
  80. }
  81. }

index.php

  1. <?php
  2. require('view.php');
  3. require('model.php');
  4. //服务容器类
  5. class container
  6. {
  7. private $obj = [];
  8. public function bind($name, $func)
  9. {
  10. $this->obj[$name] = $func;
  11. }
  12. public function make($name, $params = [])
  13. {
  14. return call_user_func_array($this->obj[$name], $params);
  15. }
  16. public function destroy()
  17. {
  18. unset($this->obj);
  19. }
  20. }
  21. //绑定
  22. $con = new container();
  23. $con->bind('Db', function () {
  24. return new Db();
  25. });
  26. $con->bind('View', function () {
  27. return new View();
  28. });
  29. $db = $con->make('Db');
  30. $index = $con->make('View');
  31. //判断显示内容
  32. switch (filter_input(INPUT_GET, 'action')) {
  33. case null:
  34. $p = $_GET['p'];
  35. if ($p === null) $p = 1;
  36. echo $index->index($db->table_select(10, $p, $count), $count, $p);
  37. break;
  38. case 'edit':
  39. echo $index->edit($_GET);
  40. break;
  41. case 'del':
  42. if ($db->del($_GET['id'])) :
  43. echo '删除成功<br><a href="index.php">返回</a>';
  44. else :
  45. echo '删除失败<br><a href="index.php">返回</a>';
  46. endif;
  47. break;
  48. case 'update':
  49. if ($db->update($_POST)) :
  50. echo '修改成功<br><a href="index.php">返回</a>';
  51. else :
  52. echo '修改失败<br><a href="index.php">返回</a>';
  53. endif;
  54. break;
  55. default:
  56. break;
  57. }
  58. $con->destroy();

edit.php

  1. <?php
  2. return <<< html
  3. <link rel="stylesheet" href="style.css">
  4. <div>
  5. <h3 class='in'>修改用户信息</h3>
  6. <form action="%s" method="post">
  7. <div class='in'>
  8. <label for="id">ID</label>
  9. <input type="text" name="id" id="id" value="%s"></div>
  10. <div class='in'><label for="name">姓名</label>
  11. <input type="text" name="name" id="name" value="%s"></div>
  12. <div class='in'><label for="sex">性别</label>
  13. <input type="text" name="sex" id="sex" value="%s"></div>
  14. <div class='in'><label for="position">职业</label>
  15. <input type="text" name="position" id="position" value="%s"></div>
  16. <div class='in'><label for="tel">电话</label>
  17. <input type="text" name="tel" id="tel" value="%s"></div>
  18. <div class='in'><label for="time">注册时间</label>
  19. <input type="text" name="time" id="time" value="%s"></div>
  20. <input type="submit" id='submit' value="修改">
  21. </form>
  22. </div>
  23. html;

style.css

  1. td,
  2. th {
  3. border: 1px solid black;
  4. }
  5. table {
  6. width : 80%;
  7. border-collapse: collapse;
  8. }
  9. caption {
  10. font-size: 1.9rem;
  11. margin : 10px;
  12. }
  13. body {
  14. display : flex;
  15. flex-direction: column;
  16. align-items : center;
  17. }
  18. p {
  19. margin-top: 20px;
  20. }
  21. p>a {
  22. text-decoration: none;
  23. color : #555;
  24. border : 1px solid;
  25. padding : 5px 10px;
  26. margin : 10px 2px;
  27. }
  28. .active {
  29. background-color: red;
  30. color : white;
  31. border : 1px solid red;
  32. }
  33. label {
  34. width : 80px;
  35. display: inline-block;
  36. }
  37. h3 {
  38. text-align: center;
  39. }
  40. #submit {
  41. float : right;
  42. width : 100px;
  43. height : 30px;
  44. margin-top: 20px;
  45. }

演示效果


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