New pagination class... looking for feedback...
Posted: Wed Aug 20, 2008 9:48 pm
I am new to OOP, and created a general pagination class to gather, calculate, etc, all the data I need to generate for several applications (blog, gallery, etc). It is working fine, but I wanted to get some feedback. What would you do differently? What could I improve on? Etc.
Thank you all in advance!
Thank you all in advance!
Code: Select all
<?php
class Pagination {
//Declaring variables
protected $paginationFor;
protected $totalItems;
protected $totalPages;
protected $page;
protected $defaultDisplayNumber;
protected $numberDisplayed;
public function __construct($paginationFor) {
//Sets the paginationFor variable
$this->paginationFor = $paginationFor;
}
public function setTotalItems($totalItems) {
$this->totalItems = $totalItems;
}
public function getTotalItems() {
return $this->totalItems;
}
public function setCurrentPageNumber($currentPage) {
//Sets total number of pages (required for following line of code)
$this->setTotalPages();
/* Resets page number to total number of pages if current page value is
* greater than total pages
*/
$this->page = ($currentPage > $this->getTotalPages()) ? $this->getTotalPages() : $currentPage;
/* Resets position values for the first and last items on a particular
* page in case the page number value was changed by the above line
*/
$this->setFirstLastPosition();
}
public function getCurrentPageNumber() {
return $this->page;
}
public function setDefaultDisplayNumber($defaultDisplayNumber) {
//Creates numberDisplayed array session variable if not already present
if (!isset($_SESSION['numberDisplayed'])) {
$_SESSION['numberDisplayed'] = array();
}
/* If user has provided a new number to display from an HTML form, that
* value will be added/updated in the numberDisplayed array session
* variable. If no new value has been provided, then, if the key is
* present, the existing session variable key/value pair will be used,
* or a new pair will be created based on a provided default value
*/
if (isset($_POST['numberDisplayed'])) {
$this->setNumberDisplayed($this->paginationFor, $_POST['numberDisplayed']);
$this->setCurrentPageNumber($this->page); //Resets current page
} elseif (!$this->getNumberDisplayed('notes')) {
$this->setNumberDisplayed($this->paginationFor, $defaultDisplayNumber);
}
}
public function setNumberDisplayed($paginationFor, $numberDisplayed) {
//Checks that the $numberDisplayed variable only contains numbers
$regex = '/[^0-9]++/'; //Only numbers allowed
$this->numberDisplayed = preg_replace($regex, '', $numberDisplayed);
//Adds key/value pair to session array
$_SESSION['numberDisplayed'][$paginationFor] = $this->numberDisplayed;
}
public function getNumberDisplayed() {
//Either returns the key's value or returns false
if (empty($_SESSION['numberDisplayed'][$this->paginationFor])) {
return false;
} else {
return $_SESSION['numberDisplayed'][$this->paginationFor];
}
}
public function setTotalPages() {
/* Calculates total number of pages based on total items and number of
* items displayed per page
*/
$this->totalPages = ceil($this->getTotalItems() / $this->getNumberDisplayed());
}
public function getTotalPages() {
return $this->totalPages;
}
public function setFirstLastPosition() {
/* Calculates the numerical position of the first and last items on any
* particular page
*/
$this->firstPageItemPosition = ($this->getCurrentPageNumber() * $this->getNumberDisplayed()) - $this->getNumberDisplayed();
$this->lastPageItemPosition = $this->firstPageItemPosition + $this->getNumberDisplayed();
}
public function getFirstPageItemPosition() {
return $this->firstPageItemPosition;
}
public function getLastPageItemPosition() {
return $this->lastPageItemPosition;
}
}
?>