Blogger Information
Blog 38
fans 0
comment 0
visits 22644
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
动态分页与redis静态分页
一个好人
Original
843 people have browsed it

动态分页

获取数据,并预处理

  1. // 获取页码 $_GET
  2. $page = $_GET['p'] ?? 1;
  3. // 越界检测
  4. $page = $page < 1 ? 1 : $page;
  5. // 每页显示条数
  6. $pageSize = 10;
  7. // $pageSize = $_GET['pageSize'];
  8. // 偏移量
  9. $offset = ($page - 1) * $pageSize;
  10. $db = new PDO('mysql:dbname=laravel', 'root', 'root');
  11. $stmt = $db->prepare('select * from cates limit ?,? ');
  12. $stmt->bindParam(1, $offset, PDO::PARAM_INT);//参数序号,参数值,参数类型
  13. $stmt->bindParam(2, $pageSize, PDO::PARAM_INT);
  14. $stmt->execute();
  15. $lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
  16. // var_dump($lists);
  17. // 计算总记录数
  18. $sql = 'SELECT COUNT(id) AS TOTAL FROM cates';
  19. $stmt = $db->prepare($sql);
  20. $stmt->execute();
  21. // print_r($stmt->fetch(PDO::FETCH_ASSOC));
  22. $total = $stmt->fetch(PDO::FETCH_ASSOC)['TOTAL'];
  23. // 总页数= 数据表的总行数/每页显示条数
  24. $pages = ceil($total / $pageSize);//向上取整

网页展示:

  1. <?php foreach ($lists as $list) : extract($list) ?>
  2. <tr>
  3. <td><?= $id ?></td>
  4. <td><?= $name ?></td>
  5. <td><?= $status == 1 ? '正常' : '禁用' ?></td>
  6. <td><?= date('Y-m-d H:i:s', $create_time) ?></td>
  7. <td><?= $update_time ?></td>
  8. <td><button>删除</button><button>编辑 </button></td>
  9. </tr>
  10. <?php endforeach ?>

添加分页符:

  1. <?php
  2. $prev = $page - 1;
  3. if ($page == 1) $prev = 1;
  4. if ($page != 1) :
  5. ?>
  6. <a href="?p=<?= $prev ?>">上一页</a>
  7. <?php endif;
  8. //动态生成分页
  9. for ($i = 1; $i <= $pages; $i++) :
  10. $active = ($page == $i) ? 'active' : null;
  11. ?>
  12. <a class="<?= $active ?>" href="?p=<?= $i ?>"><?= $i ?></a>
  13. <?php endfor ?>
  14. <?php
  15. $next = $page + 1;
  16. if ($next == $pages) $next = $pages;
  17. if ($page != $pages) : ?>
  18. <a href="">下一页</a>
  19. <?php endif ?>

redis静态分页

安装Redis,并从数据库获取数据:

  1. if (extension_loaded('redis')) {
  2. $redis = new Redis();
  3. $redis->connect('127.0.0.1', '6379');
  4. $lists = (new PDO('mysql:dbname=laravel', 'root', 'root'))->query('select * from cates')->fetchAll(PDO::FETCH_ASSOC);
  5. $lists = json_encode(getTree($lists), 320);
  6. $redis->set('cates', $lists);
  7. }

Redis数据分类:

  1. function getTree($list, $pid = 0, $level = 0)
  2. {
  3. static $tree = [];
  4. foreach ($list as $row) {
  5. if ($row['pid'] == $pid) {
  6. $row['level'] = $level;
  7. $tree[] = $row;
  8. getTree($list, $row['id'], $level++);
  9. }
  10. }
  11. return $tree;
  12. }

从Redis获取数据并预处理:

  1. $redis = new Redis();
  2. $redis->connect('127.0.0.1', '6379');
  3. $lists = json_decode($redis->get('cates'), true);
  4. $page = $_GET['p'] ?? 1;
  5. $page = $page < 1 ? 1 : $page;
  6. $pageSize = 20;
  7. $offset = ($page - 1) * $pageSize;
  8. $pages = ceil(count($lists) / $pageSize);
  9. $lists = array_splice($lists, $offset, $pageSize);

数据网页展示:

  1. <?php foreach ($lists as $list) : extract($list) ?>
  2. <tr>
  3. <td><?= $id ?></td>
  4. <td style="padding-left:<?= $level ?>cm">|<?= $name ?></td>
  5. <td><?= $status == 1 ? '正常' : '禁用' ?></td>
  6. <td><?= date('Y-m-d H:i:s', $create_time) ?></td>
  7. <td><?= $update_time ?></td>
  8. <td><button>删除</button><button>编辑 </button></td>
  9. </tr>
  10. <?php endforeach ?>

分页符:

  1. <?php
  2. $prev = $page - 1;
  3. if ($page == 1) $prev = 1;
  4. if ($page != 1) :
  5. ?>
  6. <a href="?p=<?= $prev ?>">上一页</a>
  7. <?php endif;
  8. //动态生成分页
  9. for ($i = 1; $i <= $pages; $i++) :
  10. $active = ($page == $i) ? 'active' : null;
  11. ?>
  12. <a class="<?= $active ?>" href="?p=<?= $i ?>"><?= $i ?></a>
  13. <?php endfor ?>
  14. <?php
  15. $next = $page + 1;
  16. if ($next == $pages) $next = $pages;
  17. if ($page != $pages) : ?>
  18. <a href="">下一页</a>
  19. <?php endif ?>

总结:

这节课内容比较多,刚捋完一遍,Redis挺有用,还没来得及优化。

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