0

0

php mysql分页类(php新手入门)

php中文网

php中文网

发布时间:2016-07-25 09:00:54

|

1720人浏览过

|

来源于php中文网

原创

  1. /***

  2. * php mysql 分页类
  3. * 整理 http://bbs.it-home.org
  4. */
  5. class pagination{
  6. private $_result;
  7. private $_count; //记录数
  8. private $_pagemax; //最大页
  9. private $_page; //当前页
  10. private $_url;
  11. private $_startpage;//分页条起码
  12. private $_endpage; // 分页条止码
  13. private $_nextpage; //上一页
  14. private $_prepage; //下一页
  15. function __construct($table, $pageSize, $getPage){

  16. $this->_result = $GLOBALS['db']->query('SELECT * FROM '.$table);
  17. $this->_count = $GLOBALS['db']->getRecordNum();
  18. $this->_pageMax = ceil($this->_count/$pageSize);
  19. $this->_result = '';
  20. if($_GET[$getPage] == '')

  21. $this->_page = 1;
  22. else
  23. $this->_page= max(intval($_GET[$getPage]), 1);
  24. $this->_page = min($this->_page, $this->_pageMax);
  25. $offset = ($this->_page - 1) * $pageSize;

  26. $sql = 'SELECT * FROM '.$table.' LIMIT '. $offset .','. $pageSize;
  27. $this->_result = $GLOBALS['db']->query($sql);
  28. }
  29. function getRecord(){
  30. return $GLOBALS['db']->getRecord();
  31. }
  32. function getPageBar($url = '?', $barLn = 10, $style = 1){
  33. if($style == 1){
  34. if($barLn % 2 != 0 ){
  35. $midder = ceil($barLn / 2);
  36. $big_repair = $midder - 1 ;//当上面以进一法取整,则这里为减1,反之为加1
  37. }else{
  38. $midder = $big_repair = $barLn / 2;
  39. }
  40. $sml_repair = $midder- 1;
  41. $this->_startPage = ($this->_page + $midder) > $this->_pageMax ? $this->_pageMax - $barLn : $this->_page - $sml_repair;
  42. $this->_endPage = $this->_page _page + $big_repair;
  43. }elseif($style == 2){
  44. if($this->_page % $barLn == 0){
  45. $this->_startPage = $this->_page;
  46. }else{
  47. $this->_startPage = ($this->_page > $barLn)? $this->_page - ($this->_page % $barLn ) : 1;
  48. }
  49. $this->_endPage = $this->_startPage + $barLn - 1;
  50. }
  51. $this->_url = $url;
  52. $this->_nextPage = $this->_page + 1;
  53. $this->_prePage = $this->_page - 1;
  54. $this->_startPage = max($this->_startPage, 1);//至少从第一页开始
  55. $this->_endPage = min($this->_endPage, $this->_pageMax);//最多只到末页
  56. $this->_result = '当前是:'.$this->_page.'/'.$this->_pageMax.'页,共'.$this->_count.'条记录';
  57. if ($this->_page > 1)
  58. $this->_result .= '
  59. 9
  60. 3
  61. ';
  62. else
  63. $this->_result .= '9
  64. 3';
  65. for($i = $this->_startPage; $i _endPage; $i++) {
  66. if ($this->_page == $i)
  67. $this->_result .= ''.$i.'';
  68. else
  69. $this->_result.= ''.$i.'';
  70. }
  71. if ($this->_page != $this->_pageMax) {
  72. $this->_result .= '4';
  73. $this->_result .= ':';
  74. } else {
  75. $this->_result.= '4:';
  76. }
  77. $this->_result.= '
  78. ';
  79. return $this->_result;
  80. }
  81. }
  82. ?>

    立即学习PHP免费学习笔记(深入)”;

复制代码

2、调用示例

  1. $page = new Pagination($table, 5, 'page');
  2. while($row = $page->getRecord()){
  3. echo $row[0],'
    ';
  4. }
  5. echo $page->getPageBar('?',8, 1);
  6. ?>
复制代码


相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Golang 分布式缓存与高可用架构
Golang 分布式缓存与高可用架构

本专题系统讲解 Golang 在分布式缓存与高可用系统中的应用,涵盖缓存设计原理、Redis/Etcd集成、数据一致性与过期策略、分布式锁、缓存穿透/雪崩/击穿解决方案,以及高可用架构设计。通过实战案例,帮助开发者掌握 如何使用 Go 构建稳定、高性能的分布式缓存系统,提升大型系统的响应速度与可靠性。

26

2026.01.09

java学习网站推荐汇总
java学习网站推荐汇总

本专题整合了java学习网站相关内容,阅读专题下面的文章了解更多详细内容。

38

2026.01.08

java学习网站汇总
java学习网站汇总

本专题整合了java学习网站相关内容,阅读专题下面的文章了解更多详细内容。

0

2026.01.08

正则表达式 删除
正则表达式 删除

本专题整合了正则表达式删除教程大全,阅读专题下面的文章了解更多详细教程。

49

2026.01.08

java 元空间 永久代
java 元空间 永久代

本专题整合了java中元空间和永久代的区别,阅读专题下面的文章了解更多详细内容。

4

2026.01.08

java 永久代和元空间
java 永久代和元空间

本专题整合了java中元空间和永久代的区别,阅读专题下面的文章了解更多详细内容。

0

2026.01.08

java成品网站源码资源大全
java成品网站源码资源大全

本专题整合了java成品网站源码相关内容,阅读专题下面的文章了解更多详细内容。

20

2026.01.08

java过滤器教程大全
java过滤器教程大全

本专题整合了java过滤器相关教程,阅读专题下面的文章了解更多详细内容。

6

2026.01.08

作业帮网页版入口地址大全
作业帮网页版入口地址大全

本专题整合了作业帮网页版地址整理,阅读专题下面的文章了解更多详细内容。

7

2026.01.08

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
php ajax快速入门视频教程
php ajax快速入门视频教程

共6课时 | 1.3万人学习

PHP 新手入门教程
PHP 新手入门教程

共54课时 | 29.4万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号