Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Pagination
- {
- protected $numItems;
- protected $page;
- protected $limit;
- protected $numPages;
- protected $offset;
- protected $backPage;
- protected $nextPage;
- public function getNumPages() { return $this->numPages; }
- public function getOffset() { return $this->offset; }
- public function getPage() { return $this->page; }
- public function setPage($_val)
- {
- $this->page = intval($_val);
- $this->recalculate();
- return $this;
- }
- public function getFirstPage() { return 1; }
- public function getLastPage() { return $this->numPages; }
- public function getLimit() { return $this->limit; }
- public function setLimit($_val)
- {
- $this->limit = $_val;
- $this->recalculate();
- return $this;
- }
- public function getPreviousPage() { return $this->backPage; }
- public function getNextPage() { return $this->nextPage; }
- public function getLastIndice()
- {
- if ($this->numPages < 1)
- {
- return 1;
- }
- else
- {
- $lastIndice = $this->page * $this->limit;
- $theLastIndice = ($lastIndice > $this->numItems) ? $this->numItems : $lastIndice;
- return $theLastIndice;
- }
- }
- public function getFirstIndice()
- {
- if ($this->numPages < 1)
- {
- return 1;
- }
- else
- {
- return (($this->page * $this->getLimit()) - ($this->getLimit() - 1));
- }
- }
- protected function recalculate()
- {
- // calculate number of pages
- print_r(array($this->numItems, $this->limit));
- $this->numPages = $this->numItems / $this->limit;
- // set previous page
- if ($this->page <= 1)
- {
- $this->backPage = 1;
- }
- else
- {
- $this->backPage = $this->page - 1;
- }
- // set next page
- if ($this->page >= $this->getLastPage())
- {
- $this->nextPage = $this->getLastPage();
- }
- else
- {
- $this->nextPage = $this->page + 1;
- }
- $this->offset = ($this->page - 1) * $this->limit;
- return $this;
- }
- public function __construct($_genericObjAry, $_limit)
- {
- $this->setPage(FusionboxUtilities::getDefault(Delegator::$ID, 1));
- $this->numItems = count($_genericObjAry);
- $this->limit = $_limit;
- $this->recalculate();
- }
- public function __destruct()
- {
- }
- }
Add Comment
Please, Sign In to add comment