Home >Backend Development >PHP Tutorial >A simple universal paging class written by myself

A simple universal paging class written by myself

WBOY
WBOYOriginal
2016-07-25 08:47:57959browse
I personally learned a simple paging class written in PHP, which has strong versatility.
  1. /**
  2. * Simple paging class
  3. * @author:phpprince
  4. * @QQ: 8923052
  5. * @date: 2014-04-22 21:08:35
  6. */
  7. class Page{
  8. protected $url; //Address
  9. protected $allnum; //Total number of records
  10. protected $current; //Current page number
  11. protected $pagesize; //How many records are displayed on each page
  12. protected $postfix; //Suffix
  13. protected $style; //There are 3 display styles, 1 2 3 respectively means the first 5 and the last 4, the first 3 and the last 3, and the first 4 Post 4
  14. public function __construct($url,$allnum,$current,$pagesize=10,$postfix='',$style=1){
  15. $this->url=$url;
  16. $this-> ;allnum=$allnum;
  17. $this->current=$current;
  18. $this->pagesize=$pagesize;
  19. $this->postfix=$postfix;
  20. $this->style=$style;
  21. }
  22. //Get the total number of pages
  23. protected function maxPageNum(){
  24. $max=ceil($this->allnum/$this->pagesize);
  25. //Page number over limit correction
  26. if($this ->current>$max){
  27. $this->current=$max;
  28. }
  29. if($this->current<1){
  30. $this->current=1;
  31. }
  32. return $ max;
  33. }
  34. //Get the complete html of the first page link str
  35. protected function firstUrl(){
  36. if($this->current!=1)
  37. {
  38. return 'Homepage';
  39. }else{
  40. return 'Homepage';
  41. }
  42. }
  43. //Get the complete html of the previous page link str
  44. protected function prevUrl() {
  45. if($this->current<=1){
  46. $fullurl='previous page';
  47. }else{
  48. $fullurl=$this->url.($this->current-1).$this->postfix;
  49. $fullurl='Previous page';
  50. }
  51. return $fullurl;
  52. }
  53. //Get next One page link complete html str
  54. protected function nextUrl(){
  55. if($this->current>=$this->maxPageNum()){
  56. $fullurl='< ;a>Next page';
  57. }else{
  58. $fullurl=$this->url.($this->current+1).$this->postfix ;
  59. $fullurl='Next page';
  60. }
  61. return $fullurl;
  62. }
  63. //Get the complete html of the last page link str
  64. protected function lastUrl(){
  65. if($this->current>=$this->maxPageNum( )){
  66. $fullurl='lastpage';
  67. }else{
  68. $fullurl=$this->url .$this->maxPageNum().$this->postfix;
  69. $fullurl='Last page';
  70. }
  71. return $fullurl;
  72. }
  73. //Get the complete url of the specified page number
  74. protected function getUrl($pageno){
  75. return $this- >url.$pageno.$this->postfix;
  76. }
  77. //Specify the display style
  78. protected function getStyle(){
  79. switch($this->style){
  80. case 1:
  81. $before=5 ;
  82. $after=4;
  83. break;
  84. case 2:
  85. $before=3;
  86. $after=3;
  87. break;
  88. case 3:
  89. $before=4;
  90. $after=4;
  91. break;
  92. default :
  93. $before=5;
  94. $after=4;
  95. }
  96. return array($before,$after);
  97. }
  98. //Get the intermediate URL 1 2 3 4 5 ⑥ 7 8 9 10
  99. protected function getMiddelUrl (){
  100. $max=$this->maxPageNum(); //Get the total page first to correct the current page overrun problem
  101. $current=$this->current; //The current page number must be legal to ensure The following page number range must be correct
  102. //Get the current style
  103. list($before,$after)=$this->getStyle();
  104. $startno=$current-$before; //Start page number
  105. $endno= $current+$after; //End page number
  106. //To ensure that the output always meets the requirements, if the starting page increases, the end page must increase, and vice versa.
  107. while($endno>$ max||$startno<1){
  108. if($endno>$max){ //The end page number is out of bounds
  109. $endno--;
  110. if($startno>1){
  111. $startno--;
  112. }
  113. }
  114. if($startno<1){ //The starting page number is out of bounds
  115. $startno++;
  116. if($endno<$max){
  117. $endno++;
  118. }
  119. }
  120. }
  121. $str=''; //Use To save the entire html str
  122. for($i=$startno;$i<=$endno;$i++){
  123. $currenturl=$this->getUrl($i);
  124. if($i!=$current) {
  125. $str.="{$i}";
  126. }else{
  127. $str.=''.$i.'';
  128. }
  129. }
  130. return $str;
  131. }
  132. //Return the complete paging html string
  133. public function getPageStr() {
  134. $str='
    '.$this->firstUrl().$this->prevUrl();
  135. $str.=$this->getMiddelUrl();
  136. $str.=$this->nextUrl().$this->lastUrl().'total'.$this->maxPageNum().'page'. $this->allnum.'bar
';
  • return $str;
  • }
  • }
  • ?>
  • Copy code A simple universal paging class written by myself


    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn