A simple universal paging class written by a person

Source: Internet
Author: User
Individual learning PHP written simple paging class, more versatile
  1. /**
  2. * Easy Paging class
  3. * @author:p Hpprince
  4. * @QQ: 8923052
  5. * @date: 2014-04-22 21:08:35
  6. */
  7. Class page{
  8. protected $url;//Address
  9. protected $allnum;//Total Record Count
  10. protected $current;//Current page
  11. protected $pagesize;//How many records are displayed per page
  12. protected $postfix;//suffix
  13. protected $style;//display style A total of 3 styles, 1 2 3 for the first 5 after 4, the first 3 after 3, the first 4 after 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 Total Pages
  23. protected function Maxpagenum () {
  24. $max =ceil ($this->allnum/$this->pagesize);
  25. Page 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 first page link full HTML str
  35. protected function Firsturl () {
  36. if ($this->current!=1)
  37. {
  38. Return 'getUrl (1). ' title= ' View the first page "> Home ";
  39. }else{
  40. Return ' home ';
  41. }
  42. }
  43. Get previous page link full HTML 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 page link full HTML str
  54. protected function Nexturl () {
  55. if ($this->current>= $this->maxpagenum ()) {
  56. $fullurl = ' next page ';
  57. }else{
  58. $fullurl = $this->url. ($this->current+1). $this->postfix;
  59. $fullurl = ' next page ';
  60. }
  61. return $fullurl;
  62. }
  63. Get last page link full HTML str
  64. protected function Lasturl () {
  65. if ($this->current>= $this->maxpagenum ()) {
  66. $fullurl = ' last ';
  67. }else{
  68. $fullurl = $this->url. $this->maxpagenum (). $this->postfix;
  69. $fullurl = ' last ';
  70. }
  71. return $fullurl;
  72. }
  73. Gets the full URL of the specified page number
  74. protected function GetUrl ($pageno) {
  75. Return $this->url. $pageno. $this->postfix;
  76. }
  77. Specify 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 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 that the following page range must be correct
  102. Get current style
  103. List ($before, $after) = $this->getstyle ();
  104. $startno = $current-$before;//Start Page
  105. $endno = $current + $after;//End page
  106. To ensure that the output always meet the requirements, the page is not overrun, the start page increases, the end page must be increased, vice versa.
  107. while ($endno > $max | | $startno <1) {
  108. if ($endno > $max) {//End page number Hyper-bounds
  109. $endno--;
  110. if ($startno >1) {
  111. $startno--;
  112. }
  113. }
  114. if ($startno <1) {//Start page number over bounds
  115. $startno + +;
  116. if ($endno < $max) {
  117. $endno + +;
  118. }
  119. }
  120. }
  121. $STR = ";//for saving 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. Returns the full page-paged 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. ';
  137. return $str;
  138. }
  139. }
  140. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.