No problem.. thanks for your help!
Below is an updated version of the pagination class and an example of its implementation...
Instantiation of pagination class in a controller helper class...
Code: Select all
class NotesActions extends ActionController {
...SNIP...
public function doNotes() {
...SNIP...
/* Instantiates new Notes class, then gathers all entries from the
* database, places that data into an array, and then passes the object
* to the registry
*/
$notes = new Notes($this->mysqli);
$notes->findEntries();
$this->registry->set('notesObject', $notes);
/* Instantiates new Pagination class, passes all the required data for
* generating pagination, and then passes the object to the registry
*/
$pagination = new Pagination('notes');
$pagination->setTotalItems($notes->getDataCount());
$pagination->setDefaultDisplayNumber(4);
$pagination->setCurrentPageNumber($this->page);
$this->registry->set('paginationObject', $pagination);
}
}
Example of implementation of the pagination object...
Code: Select all
<?php
//Sets note and pagination objects from the registry
$notes = $this->registry->get('notesObject');
$pagination = $this->registry->get('paginationObject');
//Ouputs HTML header and openning div
echo "\t\t<div class=\"notesContainer\">\n";
echo "\t\t\t<h2 class=\"notes\">Notes:: More from me..</h2>\n";
echo "\t\t\tI use this area of my site to share random thoughts, experiences, etc.\n\t\t\t<br />\n\n";
//Ouputs navigation div
echo "\t\t\t<div class=\"notesNavigationContainer\">\n";
echo "\t\t\t\t<div class=\"notesNavigationRight\">\n";
//If there are more pages beyond the current one, a "Next" link is provided
if ($pagination->getTotalItems() > $pagination->getLastPageItemPosition()) {
echo "\t\t\t\t\t<a href=\"http://" . $_SERVER['SERVER_NAME'] . '/notes/' . ($pagination->getCurrentPageNumber() + 1) . "\">Next</a>\n";
$addNextLink = true;
} else {
$addNextLink = false;
}
echo "\t\t\t\t</div>\n";
echo "\t\t\t\t<div class=\"notesNavigationLeft\">\n";
//If the user is beyond the first page, a "Previous" link is provided
if ($pagination->getCurrentPageNumber() > 1) {
echo "\t\t\t\t\t<a href=\"http://" . $_SERVER['SERVER_NAME'] . '/notes/' . ($pagination->getCurrentPageNumber() - 1) . "\">Previous</a>\n";
} else {
echo "\t\t\t\t\t<span style=\"color:#ffffff;\">_</span>\n";
}
echo "\t\t\t\t</div>\n";
echo "\t\t\t\t<div class=\"notesNavigationCenter\">\n\t\t\t\t\t";
//Calculating the starting pagination number
$startingPaginationNumber = $pagination->getCurrentPageNumber() - 2;
//If the user is on page four or beyond, a link to page one is provided
if ($startingPaginationNumber <= 1) {
$startingPaginationNumber = 1;
} else {
echo '<a href="http://' . $_SERVER['SERVER_NAME'] . '/notes/1" title="First Page">«</a> ';
}
//Calculating the ending pagination number
$endingPaginationNumber = $pagination->getCurrentPageNumber() + 2;
if ($endingPaginationNumber > $pagination->getTotalPages()) {
$endingPaginationNumber = $pagination->getTotalPages();
}
/* Navigation via a numerical index will be provided with links directly to the
* two most adjacent pages on either side of the current page number, as well as
* the additional links to the starting and ending pages, all such that the
* output will resemble "<< 3 4 5 6 7 >>" where 5 is the current page
*/
for ($i = $startingPaginationNumber; $i <= $endingPaginationNumber; $i++) {
if ($i == $pagination->getCurrentPageNumber()) {
echo '<span style="font-weight:800;text-decoration:underline;">' . $i . '</span> ';
} else {
echo '<a href="http://' . $_SERVER['SERVER_NAME'] . "/notes/$i\">$i</a> ";
}
}
/* If the user is three or more pages from the end, a link to the last page is
* provided
*/
if ($endingPaginationNumber < $pagination->getTotalPages()) {
echo '<a href="http://' . $_SERVER['SERVER_NAME'] . '/notes/' . $pagination->getTotalPages() . '" title="Last Page">»</a>';
}
echo "\n\t\t\t\t</div>\n";
echo "\t\t\t</div>\n\n";
//Outputting entry data
$noteData = $notes->getSlicedData($pagination->getFirstPageItemPosition(), $pagination->getDisplayNumber());
foreach($noteData as $note) {
echo "\t\t\t<div class=\"noteContainer\">\n";
echo "\t\t\t\t<h2 class=\"notes\">" . $note['title'] . "</h2>\n";
echo "\t\t\t\t" . $note['date'] . " | <a href=\"http://" . $_SERVER['SERVER_NAME'] . '/notes/entry/' . $note['id'] . "\">Permalink</a>\n";
echo "\t\t\t\t<div class=\"noteBody\">\n\t\t\t\t\t" . stripslashes(str_replace("\n" , "\t\t\t\t\t<br />\n\t\t\t\t\t", $note['note'])) . "\n\t\t\t\t</div>";
echo "\n\t\t\t</div>\n";
}
echo "\t\t</div>";
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>" class="notesNumberDisplayed">
<div class="notesNumberDisplayed">
Display
<select name="displayNumber" size="1" class="notesNumberDisplayed" onchange="form.submit()">
<option value="2" class="notesNumberDisplayed" <?php if ($pagination->getDisplayNumber() == 2) {echo "selected=\"selected\"";} ?>>2</option>
<option value="4" class="notesNumberDisplayed" <?php if ($pagination->getDisplayNumber() == 4) {echo "selected=\"selected\"";} ?>>4</option>
<option value="6" class="notesNumberDisplayed" <?php if ($pagination->getDisplayNumber() == 6) {echo "selected=\"selected\"";} ?>>6</option>
<option value="8" class="notesNumberDisplayed" <?php if ($pagination->getDisplayNumber() == <!-- s8) --><img src=\"{SMILIES_PATH}/icon_cool.gif\" alt=\"8)\" title=\"Cool\" /><!-- s8) --> {echo "selected=\"selected\"";} ?>>8</option>
<option value="10" class="notesNumberDisplayed" <?php if ($pagination->getDisplayNumber() == 10) {echo "selected=\"selected\"";} ?>>10</option>
<option value="20" class="notesNumberDisplayed" <?php if ($pagination->getDisplayNumber() == 20) {echo "selected=\"selected\"";} ?>>20</option>
<option value="40" class="notesNumberDisplayed" <?php if ($pagination->getDisplayNumber() == 40) {echo "selected=\"selected\"";} ?>>40</option>
</select>
<?php
if ($addNextLink) {
echo "\n\t\t\t<a href=\"http://" . $_SERVER['SERVER_NAME'] . '/notes/' . ($pagination->getCurrentPageNumber() + 1) . "\">Next</a>\n";
}
?>
<noscript><p class="no_style">JavaScript required to change the display number!</p></noscript>
</div>
</form>
Pagination class (updated)
Code: Select all
<?php
class Pagination {
//Declaring variables
protected $paginationFor;
protected $totalItems;
protected $totalPages;
protected $currentPage;
protected $defaultDisplayNumber;
protected $displayNumber;
//Sets variable specifying what application/module the pagination is for
public function __construct($paginationFor) {
//Sets the paginationFor variable
$this->paginationFor = $paginationFor;
}
//Sets total number of items undergoing pagination
public function setTotalItems($totalItems) {
$this->totalItems = $totalItems;
}
//Returns total number of items
public function getTotalItems() {
return $this->totalItems;
}
//Sets current page number
public function setCurrentPageNumber($page) {
//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->currentPage = ($page > $this->getTotalPages()) ? $this->getTotalPages() : $page;
/* 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();
}
//Returns the current page number
public function getCurrentPageNumber() {
return $this->currentPage;
}
//Returns the page number for any particular item
public function getItemsPageNumber($itemPosition) {
return ceil(($itemPosition)/($this->getDisplayNumber() - 0.0000001));
}
/* Sets default number of items displayed per page and sets it to a session
* variable
*/
public function setDefaultDisplayNumber($defaultDisplayNumber) {
//Sets default number of items displayed
$this->defaultDisplayNumber = $defaultDisplayNumber;
/* Creates displayNumber array session variable, if not already
* present, containing the default number of items to display
*/
if (empty($_SESSION['displayNumber'][$this->paginationFor])) {
$_SESSION['displayNumber'][$this->paginationFor] = $this->defaultDisplayNumber;
}
/* If user has provided a new number to display from an HTML form, that
* value will be added/updated in the displayNumber array session
* variable, followed by an update of the current page number.
*/
if (isset($_POST['displayNumber'])) {
$this->setDisplayNumber($_POST['displayNumber']);
$this->setCurrentPageNumber($this->currentPage);
}
}
//Sets a user defined number of items to be displayed per page
public function setDisplayNumber($displayNumber) {
//Checks that the $displayNumber variable only contains numbers
$regex = '/[^0-9]++/'; //Only numbers allowed
$this->displayNumber = preg_replace($regex, '', $displayNumber);
//Adds key/value pair to session array
$_SESSION['displayNumber'][$this->paginationFor] = $this->displayNumber;
}
//Returns the number of items to be displayed per page
public function getDisplayNumber() {
//Either returns the key's value or returns false
if (empty($_SESSION['displayNumber'][$this->paginationFor])) {
return false;
} else {
return $_SESSION['displayNumber'][$this->paginationFor];
}
}
//Sets total number of pages
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->getDisplayNumber());
}
//Returns total number of pages
public function getTotalPages() {
return $this->totalPages;
}
//Sets numerical position of first/last items on a given page
public function setFirstLastPosition() {
/* Calculates the numerical position of the first and last items on any
* particular page
*/
$this->firstPageItemPosition = ($this->getCurrentPageNumber() * $this->getDisplayNumber()) - $this->getDisplayNumber();
$this->lastPageItemPosition = $this->firstPageItemPosition + $this->getDisplayNumber();
}
//Returns position of the first item on a given page
public function getFirstPageItemPosition() {
return $this->firstPageItemPosition;
}
//Returns position of the last item on a given page
public function getLastPageItemPosition() {
return $this->lastPageItemPosition;
}
}
?>