欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

分页类系列之三

程序员文章站 2022-06-11 08:08:53
...
分页类之三
  1. class Page {
  2. private $total; //查询所有的数据总记录数
  3. private $page; //当前第几页
  4. private $num; //每页显示记录的条数
  5. private $pageNum; //一共多少页
  6. private $offset; //从数据库中取记录的开始偏移数
  7. function __construct($total, $page=1, $num=5) {
  8. $this->total=$total;
  9. $this->page=$page;
  10. $this->num=$num;
  11. $this->pageNum=$this->getPageNum();
  12. $this->offset=$this->getOffset();
  13. }
  14. private function getPageNum(){
  15. return ceil($this->total/$this->num);
  16. }
  17. private function getNextPage() {
  18. if($this->page==$this->pageNum)
  19. return false;
  20. else
  21. return $this->page+1;
  22. }
  23. private function getPrevPage() {
  24. if($this->page==1)
  25. return false;
  26. else
  27. return $this->page-1;
  28. }
  29. //数据库查询的偏移量
  30. private function getOffset() {
  31. return ($this->page-1)*$this->num;
  32. }
  33. //当前页开始的记录数
  34. private function getStartNum() {
  35. if($this->total==0)
  36. return 0;
  37. else
  38. return $this->offset+1;
  39. }
  40. //当前页结束的记录数
  41. private function getEndNum() {
  42. return min($this->offset+$this->num,$this->total);
  43. }
  44. public function getPageInfo(){
  45. $pageInfo=array(
  46. "row_total" => $this->total,
  47. "row_num" => $this->num,
  48. "page_num" => $this->getPageNum(),
  49. "current_page" => $this->page,
  50. "row_offset" => $this->getOffset(),
  51. "next_page" => $this->getNextPage(),
  52. "prev_page" => $this->getPrevPage(),
  53. "page_start" => $this->getStartNum(),
  54. "page_end" => $this->getEndNum()
  55. );
  56. return $pageInfo;
  57. }
  58. }
  59. ?>
复制代码
相关标签: 分页类系列之三