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

php实现google样式的分页

程序员文章站 2022-06-12 11:07:55
...

废话不多说,直接看代码吧 Pager.class.php 代码如下 class Pager{ /** *int总页数 **/ protected $pageTotal ; /** **/ protected $previous ; /** **/ protected $next ; /** *int中间页起始序号 **/ protected $startPage ; /** *int中间页终止序号 **/ pr

废话不多说,直接看代码吧

Pager.class.php 代码如下

  1. class Pager {
  2. /**
  3. *int 总页数
  4. * */
  5. protected $pageTotal;
  6. /**
  7. * */
  8. protected $previous;
  9. /**
  10. * */
  11. protected $next;
  12. /**
  13. *int 中间页起始序号
  14. * */
  15. protected $startPage;
  16. /**
  17. *int 中间页终止序号
  18. * */
  19. protected $endPage;
  20. /**
  21. *int 记录总数
  22. * */
  23. protected $recorbTotal;
  24. /**
  25. *int 每页显示记录数
  26. * */
  27. protected $pageSize;
  28. /**
  29. *int 当前显示页
  30. * */
  31. protected $currentPage;
  32. /**
  33. *string 基url地址
  34. * */
  35. protected $baseUri;
  36. /**
  37. * @return string 获取基url地址
  38. */
  39. public function getBaseUri() {
  40. return $this->baseUri;
  41. }
  42. /**
  43. * @return int 获取当前显示页
  44. */
  45. public function getCurrentPage() {
  46. return $this->currentPage;
  47. }
  48. /**
  49. * @return int 获取每页显示记录数
  50. */
  51. public function getPageSize() {
  52. return $this->pageSize;
  53. }
  54. /**
  55. * @return int 获取记录总数
  56. */
  57. public function getRecorbTotal() {
  58. return $this->recorbTotal;
  59. }
  60. /**
  61. * @param string $baseUri 设置基url地址
  62. */
  63. public function setBaseUri($baseUri) {
  64. $this->baseUri = $baseUri;
  65. }
  66. /**
  67. * @param int $currentPage 设置当前显示页
  68. */
  69. public function setCurrentPage($currentPage) {
  70. $this->currentPage=$currentPage;
  71. }
  72. /**
  73. * @param int $pageSize 设置每页显示记录数
  74. */
  75. public function setPageSize($pageSize) {
  76. $this->pageSize = $pageSize;
  77. }
  78. /**
  79. * @param int $recorbTotal 设置获取记录总数
  80. */
  81. public function setRecorbTotal($recorbTotal) {
  82. $this->recorbTotal = $recorbTotal;
  83. }
  84. /**
  85. *构造函数
  86. * */
  87. public function __construct()
  88. {
  89. $this->pageTotal = 0;
  90. $this->previous = 0;
  91. $this->next = 0;
  92. $this->startPage = 0;
  93. $this->endPage = 0;
  94. $this->pageSize = 20;
  95. $this->currentPage = 0;
  96. }
  97. /**
  98. *分页算法
  99. * */
  100. private function arithmetic() {
  101. if ($this->currentPage
  102. $this->currentPage = 1;
  103. $this->pageTotal = floor ( $this->recorbTotal / $this->pageSize ) + ($this->recorbTotal % $this->pageSize > 0 ? 1 : 0);
  104. if ($this->currentPage > 1 && $this->currentPage > $this->pageTotal)
  105. header ( 'location:' . $this->baseUri . 'page=' . $this->pageTotal );
  106. $this->next = $this->currentPage + 1;
  107. $this->previous = $this->currentPage - 1;
  108. $this->startPage = ($this->currentPage + 5) > $this->pageTotal ? $this->pageTotal - 10 : $this->currentPage - 5;
  109. $this->endPage = $this->currentPage $this->currentPage + 5;
  110. if ($this->startPage
  111. $this->startPage = 1;
  112. if ($this->pageTotal $this->endPage)
  113. $this->endPage = $this->pageTotal;
  114. }
  115. /**
  116. *分页样式
  117. * */
  118. protected function pageStyle() {
  119. $result = "共" . $this->pageTotal . "页 ";
  120. if ($this->currentPage > 1)
  121. $result .= " . $this->baseUri . "page=1/">9 . $this->baseUri . "page=$this->previous/">3";
  122. else
  123. $result .= "9 3";
  124. for($i = $this->startPage; $i $this->endPage; $i ++) {
  125. if ($this->currentPage == $i)
  126. $result .= " $i";
  127. else
  128. $result .= " . $this->baseUri . "page=$i/">$i";
  129. }
  130. if ($this->currentPage != $this->pageTotal) {
  131. $result .= " . $this->baseUri . "page=$this->next/">4";
  132. $result .= " . $this->baseUri . "page=$this->pageTotal/">:";
  133. } else {
  134. $result .= " 4 :";
  135. }
  136. return $result;
  137. }
  138. /**
  139. *执行分页
  140. * */
  141. public function execute() {
  142. if ($this->baseUri != "" && $this->recorbTotal == 0)
  143. return "";
  144. $this->arithmetic();
  145. return $this->pageStyle ();
  146. }
  147. }