Home > Backend Development > PHP Tutorial > PHP paging class example PHP and mysql simple paging class code

PHP paging class example PHP and mysql simple paging class code

WBOY
Release: 2016-07-25 08:51:30
Original
914 people have browsed it
  1. class PageModel {
  2. /**
  3. * Get the paging array
  4. * @param unknown $page The current number of pages
  5. * @param unknown $goodsCount The total number of products
  6. * @param unknown $pageLength The number of pages displayed on each page
  7. */
  8. public static function getPageArr($page, $goodsCount, $pageCountLength, $pageLength) {
  9. //Total number of 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. //The total number of pages is displayed in one page
  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, put it in 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. //If it is too big, only the edge will be fetched. If it exceeds, only the edge will be fetched
  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 number, take out the middle number
  40. for ($i = -$halfLength; $i < $pageCountLength - $halfLength; $i ++) {
  41. $arr[] = array('page' => $page + $i);
  42. }
  43. return $arr;
  44. }
  45. }
Copy code

2. PHP paging class code

Code:

  1. class Helper_Page{

  2. /**Total number of messages*/

  3. var $infoCount;
  4. /**total pages*/
  5. var $pageCount ;
  6. /**Number of items displayed per page*/
  7. var $items;
  8. /**Current page number*/
  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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template