Blogger Information
Blog 17
fans 1
comment 0
visits 20045
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分页与MVC示例
大A
Original
792 people have browsed it

介绍

用MVC的模式演示分页

代码

model.php

  1. <?php
  2. class Dbdata
  3. {
  4. private $pdo;
  5. public function __construct()
  6. {
  7. $this->pdo = new PDO('mysql:host=localhost;dbname=phpedu', 'root', 'root');
  8. }
  9. /**
  10. * 录入数据
  11. *
  12. * @param [type] $array 要录入的数据数组
  13. * @return void
  14. */
  15. public function insert($array)
  16. {
  17. $sql = "INSERT `staffs` SET
  18. `name`='$array[0]',
  19. `sex`='$array[1]',
  20. `position`='$array[2]',
  21. `tel`='$array[3]',
  22. `time`=$array[4]";
  23. $this->pdo->prepare($sql)->execute();
  24. return var_dump($this->pdo->lastInsertId()) ? true : false;
  25. }
  26. /**
  27. * 返回每一页的查询结果
  28. *
  29. * @param [type] $limit 每页显示的条数
  30. * @param [type] $offset 查询偏移
  31. * @param [type] $count 页数
  32. * @return void
  33. */
  34. public function select($limit, $offset, &$count)
  35. {
  36. $sql = "SELECT * FROM `staffs` LIMIT $limit OFFSET $offset";
  37. $sql_count = "SELECT COUNT(*) AS `count` FROM `staffs` LIMIT 10 OFFSET 0";
  38. $sql_obj = $this->pdo->prepare($sql_count);
  39. $sql_obj->execute();
  40. $count = ceil(($sql_obj->fetch()['count'] / $limit));
  41. $sql_obj = $this->pdo->prepare($sql);
  42. $sql_obj->execute();
  43. return $sql_obj->fetchAll();
  44. }
  45. }
  46. /*
  47. function name($size)
  48. {
  49. $name = 'abcdefghijklmnopqrstuvwxyz';
  50. for ($i = 0; $i < $size; $i++) {
  51. $result .= $name{
  52. rand(0, 25)};
  53. }
  54. return $result;
  55. }
  56. $b = new Dbdata();
  57. $name = name(8);
  58. $sex = ['男', '女'][rand(0, 1)];
  59. $zy = ['工人', '学生', '医生', '律师', '军人', '销售', '文员'][rand(0, 6)];
  60. $tel = rand(13000000000, 13999999999);
  61. $time = rand(1500000000, 1599999999);
  62. $a = [$name, $sex, $zy, $tel, $time];
  63. //$b->insert($a);
  64. print_r($b->select('10','10',$count));
  65. echo '运行完毕';
  66. */

view.php

  1. <?php
  2. class view
  3. {
  4. /**
  5. * 显示主页表格
  6. *
  7. * @param [type] $array 要输入到表格中的数据
  8. * @param [type] $count 数据的总页数
  9. * @param [type] $page 当前所在的页数
  10. * @return void
  11. */
  12. public function index($array, $count = null, $page = null)
  13. {
  14. $html = <<< head
  15. <link rel="stylesheet" href="style.css">
  16. <table><caption>员工管理系统</caption>
  17. <tr>
  18. <th>ID</th>
  19. <th>姓名</th>
  20. <th>性别</th>
  21. <th>职业</th>
  22. <th>电话</th>
  23. <th>注册时间</th>
  24. </tr>
  25. head;
  26. foreach ($array as $key => $value) {
  27. $date = date('Y/m/d', $value[5]);
  28. $html .= <<< tab
  29. <tr>
  30. <th>$value[0]</th>
  31. <th>$value[1]</th>
  32. <th>$value[2]</th>
  33. <th>$value[3]</th>
  34. <th>$value[4]</th>
  35. <th>$date</th>
  36. </tr>
  37. tab;
  38. }
  39. $html .= '</table>';
  40. $html .= $this->pageshow($count, $page);
  41. echo $html;
  42. }
  43. /**
  44. * 返回翻页标签的mthl代码
  45. *
  46. * @param [type] $count 总页数
  47. * @param [type] $page 当前页数
  48. * @return void
  49. */
  50. private function pageshow($count, $page)
  51. {
  52. $path = $_SERVER['PHP_SELF'];
  53. $html .= '<p>';
  54. $p_a = $page - 1;
  55. if ($page > 1) $html .= "<a href='$path?p=$p_a'>上一页</a>";
  56. $p_c = $page - 4;
  57. if ($page - 5 > 0) :
  58. $p_d = $p_c - 1;
  59. $html .= <<<a
  60. <a href="$path?p=$p_d" >...</a>
  61. a;
  62. if ($count - $page <= 4) :
  63. $ii = $count - 9;
  64. else :
  65. $ii = $p_c;
  66. endif;
  67. else :
  68. $ii = 1;
  69. endif;
  70. for ($i = $ii; $i < $count; $i++) {
  71. $a += 1;
  72. if ($a > 9) :
  73. $html .= <<<a
  74. <a href="$path?p=$i" >...</a>
  75. a;
  76. break;
  77. endif;
  78. $class = ($i == ($p_a + 1)) ? 'active' : '';
  79. $html .= <<<a
  80. <a href="$path?p=$i" class='$class'>$i</a>
  81. a;
  82. }
  83. $p_b = $page + 1;
  84. if ($p_b <= $count) $html .= "<a href='$path?p=$p_b'>下一页</a>";
  85. $html .= '</p>';
  86. return $html;
  87. }
  88. }

index.php

  1. <?php
  2. $p=$_GET['p'];
  3. if($p===null||$p<1)$p='1';
  4. require('view.php');
  5. require('model.php');
  6. $view= new view();
  7. $mode=new Dbdata();
  8. $data=$mode->select(10,10*($p-1),$count);
  9. if(!$data)$data=$mode->select(10,10*($count-1),$count);
  10. if($p>$count)$p=$count;
  11. $view->index($data,$count,$p);

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. }

演示结果

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!