| Andrew |
12-17-2007 06:10 AM |
My Pagination Class
I've seen quite a bit of talk around here about Pagination, so I thought I'd quickly make my own. My class is able to work with any kind of result, as long as it's an array.
Here is the code for the actual page it is displayed on:
PHP Code:
<?php
include_once 'pagination.php';
$aData = array(
'1' => 'oranges',
'2' => 'lemons',
'3' => 'limes',
'4' => 'grapes',
'5' => 'grapefuit',
'6' => 'peaches',
'7' => 'potatoes',
'8' => 'cranberries',
'9' => 'something else',
'10' => 'apples');
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$page = new Pagination($aData, $current_page, 2);
for ($i = $page->firstResult; $i <= $page->lastResult; $i++) {
echo $aData[$i].'<br />';
}
echo '<ul>'."\n";
echo $page->prevLink('<li>', '</li>');
foreach ($page->display() AS $output) {
echo '<li>'.$output.'</li>'."\n";
}
echo $page->nextLink('<li>', '</li>');
echo '</ul>'."\n";
?>
And here is the code for the class:
PHP Code:
<?php
/**
* @name PHP5 Pagination Script
* @desc Usable with any sort of array
* @created 16 Dec 2007
* @copyright Andrew Ryno
**/
class Pagination {
/* These variables will just hold all the information
and values which are used by the class, no editing
needed here */
public $show_max;
public $current_page;
public $total_results;
public $total_pages;
public $prev_link;
public $next_link;
public $firstResult;
public $lastResult;
/* This function will construct all the variables which
are needed for the script, and get the values of the
variables passed through the initializaion */
function __construct($aData, $iCurrentPage, $iMax) {
/* Max number of results on a page */
$this->show_max = $iMax;
/* Current page, of course */
$this->current_page = $iCurrentPage;
/* Total number of results to show */
$this->total_results = count($aData);
/* Total number of pages */
$this->total_pages = ceil($this->total_results / $this->show_max);
/* This will output the number of the result
which will be shown first on the page */
$this->firstResult = ($this->show_max * ($this->current_page - 1)) + 1;
/* This will output the number of the result
which will be shown last on the page */
$this->lastResult = $this->show_max * $this->current_page;
}
/* This function, when called, will output the link
for the previous link, if needed. Using the arguments
szBefore and szAfter, you can append tags such as <li>
and </li> to the link, if needed */
function prevLink($szBefore, $szAfter) {
if ($this->current_page > 1) {
$iPrevNum = $this->current_page - 1;
$this->prev_link = $szBefore.'<a href="?page='.$iPrevNum.'">Previous</a>'.$szAfter."\n";
}
return $this->prev_link;
}
/* Displays the list of pages (all of them, for now),
as links so you can navigate through the pages.
However, the current page doesn't show up as a
link */
function display() {
for ($i = 1; $i <= $this->total_pages; ++$i) {
if ($i == $this->current_page) {
$aOutput[] = $i;
} else {
$aOutput[] = '<a href="?page='.$i.'">'.$i.'</a>';
}
}
return $aOutput;
}
/* This function, when called, will output the link
for the next link, if needed. Using the arguments
szBefore and szAfter, you can append tags such as <li>
and </li> to the link, if needed */
function nextLink($szBefore, $szAfter) {
if ($this->current_page < $this->total_pages) {
$iNextNum = $this->current_page + 1;
$this->next_link = $szBefore.'<a href="?page='.$iNextNum.'">Next</a>'.$szAfter."\n";
}
return $this->next_link;
}
}
?>
And comments would be welcome, as long as criticsm. Now, I'm not entirely sure how well it would work with raw MySQL results, as I outputted my results using $aData[$i] and I don't recall if MySQL results using mysql_fetch_array() use numbers as I did, so any tips on how to make that work the best would be great without much modification.
Anyway, thanks for looking. This is my first true helpful class I've made that can work alone, as well.
|