diff --git a/Pagination.class.php b/Pagination.class.php index 3810932..79040d4 100644 --- a/Pagination.class.php +++ b/Pagination.class.php @@ -219,12 +219,7 @@ public function getRelPrevNextLinkTags() // Pages $currentPage = (int) $this->_variables['current']; - $numberOfPages = ( - (int) ceil( - $this->_variables['total'] / - $this->_variables['rpp'] - ) - ); + $numberOfPages = $this->getNumberOfPages(); // On first page if ($currentPage === 1) { @@ -261,7 +256,7 @@ public function getRelPrevNextLinkTags() * logic found in the render.inc.php file. * * @access public - * @return void + * @return string */ public function parse() { @@ -337,6 +332,14 @@ public function setCurrent($current) $this->_variables['current'] = $current; } + /** + * Get the current page number + * @return integer + */ + public function getCurrent() { + return $this->_variables['current']; + } + /** * setFull * @@ -408,6 +411,17 @@ public function setRPP($rpp) $this->_variables['rpp'] = $rpp; } + /** + * Gets the number of records per page + * @access public + * @return integer + */ + public function getRPP() + { + return $this->_variables['rpp']; + } + + /** * setTarget * @@ -435,4 +449,63 @@ public function setTotal($total) { $this->_variables['total'] = $total; } + + /** + * Get the total number of records available for pagination + * . + * @return int + */ + public function getTotal() + { + return $this->_variables['total']; + } + + /** + * Get the number of the first item on the current page. + * + * @return int + */ + public function firstItem() + { + return ($this->getCurrent() - 1) * $this->getRPP() + 1; + } + + + + /** + * Get the number of items on the current page + */ + public function countCurrentItems() { + // on the last page there could be less records than RPP + if ($this->getCurrent()==$this->getNumberOfPages()) { + return ( $this->getTotal() % $this->getRPP() ); + } + else return $this->getRPP(); + } + + + /** + * Get the number of the last item on the current page. + * + * @return int + */ + public function lastItem() + { + return $this->firstItem() + $this->countCurrentItems() - 1; + } + + /** + * Get the whole number of pages in this pagination + * @return int + */ + public function getNumberOfPages() + { + return ( + (int) ceil( + $this->_variables['total'] / + $this->_variables['rpp'] + ) + ); + } + } diff --git a/test/PaginationTest.php b/test/PaginationTest.php new file mode 100644 index 0000000..fa78d28 --- /dev/null +++ b/test/PaginationTest.php @@ -0,0 +1,25 @@ +setCurrent(10); + $pagination->setTotal(95); + $pagination->setRPP(10); + $this->assertEquals(10, $pagination->getNumberOfPages()); + $this->assertEquals(5, $pagination->countCurrentItems()); + $this->assertEquals(91, $pagination->firstItem()); + $this->assertEquals(95, $pagination->lastItem()); + + //change to another page + $pagination->setCurrent(5); + $this->assertEquals(10, $pagination->countCurrentItems()); + $this->assertEquals(41, $pagination->firstItem()); + $this->assertEquals(50, $pagination->lastItem()); + } + +} \ No newline at end of file