Blogger Information
Blog 31
fans 0
comment 0
visits 14253
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
原生分页,文件上传后端要做哪些拦截,.事件委托实战:完成链式数据库查询构造器。
木子木杉
Original
664 people have browsed it

原生分页

前端

  1. <?php
  2. require 'pageData.php';
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="zh-CN">
  6. <head>
  7. <meta charset="UTF-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <link rel="stylesheet" href="static/css/style.css">
  11. <title>Document</title>
  12. </head>
  13. <body>
  14. <table>
  15. <caption>课程信息表</caption>
  16. <thead>
  17. <tr>
  18. <th>编号</th>
  19. <th>名称</th>
  20. <th>封面</th>
  21. <th>课程简介</th>
  22. <th>创建时间</th>
  23. <th>操作</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <?php foreach ($lists as $list) : ?>
  28. <tr>
  29. <td><?= $list['cou_id'] ?></td>
  30. <td><?= $list['title'] ?></td>
  31. <td><img style="width:100px" src="<?= $list['pic'] ?>" alt="课程封面"></td>
  32. <td><?= $list['info'] ?></td>
  33. <td><?= date("Y-m-d H-m-s", $list['add_time']) ?></td>
  34. <td><button>删除</button><button>编辑</button></td>
  35. </tr>
  36. <?php endforeach; ?>
  37. </tbody>
  38. </table>
  39. <!-- 生成分页条 -->
  40. <p>
  41. <? for ($i = 1; $i <= $pages; $i++) : ?>
  42. <? $jump = sprintf('?page=%d', $i) ?>
  43. <? $active = ($i == $page) ? 'active' : '' ?>
  44. <a class="<?= $active ?>" href="<?= $jump ?>"><?= $i ?></a>
  45. <? endfor ?>
  46. </p>
  47. </body>
  48. </html>

后端

  1. <?php
  2. $pdo = new PDO(
  3. 'mysql:host=localhost;charset=utf8;port=3306;dbname=phpcn',
  4. 'root',
  5. ''
  6. );
  7. //每页的数据量
  8. $pageSize = 8;
  9. //当前访问的是第几页
  10. $page = $_GET['page'] ?? 1;
  11. //偏移量
  12. $offset = ($page - 1) * $pageSize;
  13. $sql = "SELECT `cou_id`,`title`,`pic`,`info`,`add_time` FROM `mj_course_lists` ORDER BY `cou_id` DESC LIMIT {$offset},{$pageSize}";
  14. $lists = $pdo->query($sql)->fetchAll();
  15. //获取总页数
  16. $sql1 = "SELECT COUNT(`cou_id`) AS `sum` FROM `mj_course_lists`";
  17. $total = $pdo->query($sql1)->fetch()['sum'];
  18. // var_dump($total);
  19. $pages = ceil($total / $pageSize);
  20. // var_dump($pages);

文件上传后端要做的拦截

1.检查文件格式,是否在规定的格式内
2.用’is_uploaded_file()’来检测文件是否是通过http post方法上传的,而不是系统上的一个文件
3.检查文件大小

事件委托实战:完成链式数据库查询构造器

  1. <?php
  2. /**
  3. * 事件委托 请求委托 访问类中不存在的成员方法,会用魔术方法拦截,将请求重定向给或委托给别的对象成员方法
  4. * 类处理
  5. * 委托是指一个对象转发或者委托一个请求给另一个对象,被委托的一方替原先对象处理请求
  6. * 委托比继承更加灵活 父类与子类的关系是固定的,只能单继承,但是请求可以委托给多个对象
  7. */
  8. //被委托类 数据库查询构造器
  9. class Query
  10. {
  11. //创建pdo对象的唯一实例
  12. private static $db;
  13. protected $table;
  14. protected $field;
  15. protected $limit;
  16. //private 私有的 阻止此类在外部进行实例化
  17. private function __construct()
  18. {
  19. }
  20. static function connect($dsn, $user, $pwd)
  21. {
  22. if (is_null(static::$db)) {
  23. static::$db = new pdo($dsn, $user, $pwd);
  24. }
  25. // return static::$db;
  26. return new static; //返回query实例
  27. }
  28. public function table($table)
  29. {
  30. $this->table = $table;
  31. return $this;
  32. }
  33. public function field($field)
  34. {
  35. $this->field = $field;
  36. return $this;
  37. }
  38. public function limit($limit)
  39. {
  40. $this->limit = $limit;
  41. return $this;
  42. }
  43. public function getSql()
  44. {
  45. return sprintf('SELECT %s FROM %s LIMIT %d', $this->field, $this->table, $this->limit);
  46. }
  47. public function select()
  48. {
  49. return static::$db->query($this->getSql())->fetchAll(PDO::FETCH_ASSOC);
  50. }
  51. }
  52. //委托类
  53. class Db
  54. {
  55. static function __callStatic($name, $arguments)
  56. {
  57. $dsn = 'mysql:host=localhost;charset=utf8;port=3306;dbname=phpcn';
  58. $user = 'root';
  59. $pwd = '';
  60. //获取到被委托的类query实例
  61. $q = Query::connect($dsn, $user, $pwd);
  62. return call_user_func([$q, $name], ...$arguments);
  63. }
  64. }
  65. //客户端
  66. $res = Db::table('mj_course_cat')->field('cat_id,name')->limit(20)->select();
  67. var_dump($res);
Correcting teacher:PHPzPHPz

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