Blogger Information
Blog 40
fans 0
comment 0
visits 16097
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实现动态分页以及redis静态分页
飞天001
Original
538 people have browsed it

实现动态分页,以及redis静态分页

分页自定义函数

  1. <?php
  2. /***
  3. * $page 当前页
  4. * $pageSize 每页显示条数
  5. * $pages 总页数
  6. */
  7. function page($page,$pageSize,$pages){
  8. $span=''; //定义空字符串
  9. $preStr =''; //上一页
  10. $nextStr= ''; //下一页
  11. for($i=1;$i<=$pages;$i++){
  12. $active=($page==$i)?'active':null;
  13. $span .= '<a class="'.$active.'" href="?p='.$i.'">'.$i.'</a>';
  14. }
  15. $prev = $page - 1;
  16. if($page<=1) $prev==1;
  17. if($page>1){
  18. $preStr = '<a href="?p='.$prev.'">上一页</a>';
  19. }
  20. $next = $page + 1;
  21. if($page>=$pages) $next=$pages;
  22. if($page<$pages){
  23. $nextStr ='<a href="?p='.$next.'">下一页</a>';
  24. }
  25. $span = $preStr.$span.$nextStr;
  26. return $span;
  27. }

实现动态分页

获取数据

  1. $page=$_GET['p']??1;
  2. $page = $page<1?1:$page;
  3. //每页显示条数
  4. $pageSize=10;
  5. //偏移量
  6. $offset = ($page - 1) * $pageSize;
  7. $pdo = new PDO('mysql:dbname=guer', 'root', 'root',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_WARNING]);
  8. $sql = 'select * from auth_rule limit ?,? ';
  9. $stmt = $pdo->prepare($sql);
  10. $stmt->bindParam('1',$offset,PDO::PARAM_INT);
  11. $stmt->bindParam('2',$pageSize,PDO::PARAM_INT);
  12. $stmt->execute();
  13. $lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
  14. // 计算总记录数 $total
  15. $sql = 'SELECT count(id) AS TOTAL FROM auth_rule';
  16. $stmt = $pdo->prepare($sql);
  17. $stmt->execute();
  18. $total = $stmt->fetch(PDO::FETCH_ASSOC)['TOTAL'];
  19. //总页数= 总行数/每页显示行数
  20. $pages = ceil($total / $pageSize); //取整数,ceil()进一法取整

实现分页

  1. <?=page($page,$pageSize,$pages);?>

以及redis静态分页

获取数据

  1. namespace redis_demo;
  2. use Redis, PDO;
  3. if (extension_loaded('redis')) {
  4. $redis = new Redis();
  5. $redis->connect('127.0.0.1', '6379');
  6. // 对于不经常更新的且数据量大的sql查询结果做存储、
  7. $lists = (new PDO('mysql:dbname=guer', 'root', 'root'))->query('select * from auth_rule')->fetchAll(PDO::FETCH_ASSOC);
  8. $lists = json_encode(getTree($lists), 320);
  9. $redis->set('cates', $lists);
  10. }
  11. // 返回平面化且有关系的无限极分类
  12. function getTree($list, $pid = 0, $level = 0)
  13. {
  14. static $tree = [];
  15. foreach ($list as $row) {
  16. if ($row['pid'] == $pid) {
  17. $row['level'] = $level;
  18. $tree[] = $row;
  19. getTree($list, $row['id'], $level++);
  20. }
  21. }
  22. return $tree;
  23. }

获取分页需要的基本参数

  1. $redis = new Redis(); // 创建对象
  2. $redis->connect('127.0.0.1','6379');
  3. $lists = json_decode($redis->get('cates'),true);
  4. //提供分页数据
  5. //当前页
  6. $page = $_GET['p']??1;
  7. //越界检测
  8. $page = $page<1?1:$page;
  9. //每页显示行数
  10. $pageSize = 20;
  11. //偏移量
  12. $offset = ($page - 1)*$pageSize; // (当前页-1)*每页显示数
  13. // 总页数
  14. $pages = ceil(count($lists)/$pageSize);
  15. $lists = array_splice($lists, $offset, $pageSize);

实现分页

  1. <?=page($page,$pageSize,$pages);?>
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