Guest User

Untitled

a guest
May 20th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Pagination
  5. {
  6. protected $numItems;
  7. protected $page;
  8. protected $limit;
  9. protected $numPages;
  10. protected $offset;
  11. protected $backPage;
  12. protected $nextPage;
  13.  
  14.  
  15. public function getNumPages() { return $this->numPages; }
  16. public function getOffset() { return $this->offset; }
  17.  
  18.  
  19. public function getPage() { return $this->page; }
  20. public function setPage($_val)
  21. {
  22. $this->page = intval($_val);
  23. $this->recalculate();
  24. return $this;
  25. }
  26.  
  27.  
  28. public function getFirstPage() { return 1; }
  29. public function getLastPage() { return $this->numPages; }
  30.  
  31.  
  32. public function getLimit() { return $this->limit; }
  33. public function setLimit($_val)
  34. {
  35. $this->limit = $_val;
  36. $this->recalculate();
  37. return $this;
  38. }
  39.  
  40.  
  41. public function getPreviousPage() { return $this->backPage; }
  42. public function getNextPage() { return $this->nextPage; }
  43.  
  44.  
  45. public function getLastIndice()
  46. {
  47. if ($this->numPages < 1)
  48. {
  49. return 1;
  50. }
  51. else
  52. {
  53. $lastIndice = $this->page * $this->limit;
  54. $theLastIndice = ($lastIndice > $this->numItems) ? $this->numItems : $lastIndice;
  55. return $theLastIndice;
  56. }
  57. }
  58.  
  59.  
  60. public function getFirstIndice()
  61. {
  62. if ($this->numPages < 1)
  63. {
  64. return 1;
  65. }
  66. else
  67. {
  68. return (($this->page * $this->getLimit()) - ($this->getLimit() - 1));
  69. }
  70. }
  71.  
  72.  
  73. protected function recalculate()
  74. {
  75. // calculate number of pages
  76. print_r(array($this->numItems, $this->limit));
  77. $this->numPages = $this->numItems / $this->limit;
  78.  
  79. // set previous page
  80. if ($this->page <= 1)
  81. {
  82. $this->backPage = 1;
  83. }
  84. else
  85. {
  86. $this->backPage = $this->page - 1;
  87. }
  88.  
  89. // set next page
  90. if ($this->page >= $this->getLastPage())
  91. {
  92. $this->nextPage = $this->getLastPage();
  93. }
  94. else
  95. {
  96. $this->nextPage = $this->page + 1;
  97. }
  98.  
  99. $this->offset = ($this->page - 1) * $this->limit;
  100. return $this;
  101. }
  102.  
  103.  
  104. public function __construct($_genericObjAry, $_limit)
  105. {
  106. $this->setPage(FusionboxUtilities::getDefault(Delegator::$ID, 1));
  107. $this->numItems = count($_genericObjAry);
  108. $this->limit = $_limit;
  109. $this->recalculate();
  110. }
  111.  
  112.  
  113. public function __destruct()
  114. {
  115. }
  116.  
  117.  
  118. }
Add Comment
Please, Sign In to add comment