PHP Page class instance PHP and MySQL simple pagination class code

Source: Internet
Author: User
  1. Class Pagemodel {
  2. /**
  3. * Get the number of page groups
  4. * @param unknown $page current page number
  5. * Total number of items @param unknown $goodsCount
  6. * @param unknown $pageLength The number of pages displayed per page
  7. */
  8. public static function Getpagearr ($page, $goodsCount, $pageCountLength, $pageLength) {
  9. Total pages
  10. $allPageCount = Ceil ($goodsCount/$pageLength);
  11. If the page is always shorter than the length, set the page length to the total number of pages
  12. if ($allPageCount <= $pageCountLength) {
  13. $allPageCount = Ceil ($goodsCount/$pageLength);
  14. }
  15. Total page Count one page to finish
  16. if ($allPageCount <= $pageCountLength) {
  17. for ($i = 0; $i < $allPageCount; $i + +) {
  18. $arr [] = Array (' page ' = + $i + 1);
  19. }
  20. return $arr;
  21. }
  22. The length before and after
  23. $halfLength = Floor ($pageCountLength/2);
  24. Because it is too small, so put the original position, left
  25. if ($page <= $halfLength) {
  26. $arr = Array ();
  27. for ($i = 0; $i < $pageCountLength; $i + +) {
  28. $arr [] = Array (' page ' = + $i + 1);
  29. }
  30. return $arr;
  31. }
  32. Too big, only take to the edge, beyond also only to the edge
  33. if ($page > $allPageCount-floor ($pageCountLength/2)) {
  34. for ($i =-$pageCountLength; $i < 0; $i + +) {
  35. $arr [] = Array (' page ' = + $allPageCount + $i + 1);
  36. }
  37. return $arr;
  38. }
  39. The middle of the number, the middle of the take out
  40. for ($i =-$halfLength; $i < $pageCountLength-$halfLength; $i + +) {
  41. $arr [] = Array (' page ' = = $page + $i);
  42. }
  43. return $arr;
  44. }
  45. }
Copy Code

Second, PHP page class code

Code:

  1. Class helper_page{

  2. /** total number of messages */

  3. var $infoCount;
  4. /** Total pages */
  5. var $pageCount;
  6. /** show number of bars per page */
  7. var $items;
  8. /** Current Page */
  9. var $pageNo;
  10. /** the starting position of the query */
  11. var $startPos;
  12. /** Next Page */
  13. var $nextPageNo;
  14. /** Previous Page */
  15. var $prevPageNo;

  16. function Helper_page ($infoCount, $items, $pageNo)

  17. {
  18. $this->infocount = $infoCount;
  19. $this->items = $items;
  20. $this->pageno = $pageNo;
  21. $this->pagecount = $this->getpagecount ();
  22. $this->adjustpageno ();
  23. $this->startpos = $this->getstartpos ();
  24. }
  25. function Adjustpageno ()
  26. {
  27. if ($this->pageno = = "| | $this->pageno < 1)
  28. $this->pageno = 1;
  29. if ($this->pageno > $this->pagecount)
  30. $this->pageno = $this->pagecount;
  31. }
  32. /**
  33. * Next Page
  34. */
  35. function Gotonextpage ()
  36. {
  37. $nextPageNo = $this->pageno + 1;
  38. if ($nextPageNo > $this->pagecount)
  39. {
  40. $this->nextpageno = $this->pagecount;
  41. return false;
  42. }
  43. $this->nextpageno = $nextPageNo;
  44. return true;
  45. }
  46. /**
  47. * Previous Page
  48. */
  49. function Gotoprevpage ()
  50. {
  51. $prevPageNo = $this->pageno-1;
  52. if ($prevPageNo < 1)
  53. {
  54. $this->prevpageno = 1;
  55. return false;
  56. }
  57. $this->prevpageno = $prevPageNo;
  58. return true;
  59. }
  60. function Getpagecount ()
  61. {
  62. Return Ceil ($this->infocount/$this->items);
  63. }
  64. function Getstartpos ()
  65. {
  66. Return ($this->pageno-1) * $this->items;
  67. }
  68. }

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.