Yet another MYSQL Pagination Class
Posted: Wed Nov 01, 2006 12:06 pm
Well, here is my attempt at a pagination class. It is very loosely based on the class by JCart here but I found it to be a little too buggy (no offense JCart).
Simple, but versatile is what i was going for here. See what you think.
Example 1
Example 1 returns
As you can see, all the navigation pieces are returned separately so you can place them where suits you best.
Example 2
Say you have a product catalog and the user has clicked into a certain category, and you display this by using GET values from the query string i.e. http://www.someonlinestore.com/catalog.php?categoryID=5
By default, the pagination class will maintain "categoryID=5" on all the pagination links it generates. This can be turned off by setting $maintainQueryString to FALSE
Example 3
Each link has its own CSS class definition e.g. the default class name is 'paginate', therefore:
The page number links are simply 'paginate'.
The default class name can be changed by changing $linkClass.[/list]
Example 4
You can specify the number of links that should appear each side of the current page. The default it 3.
Example output is as follows (current page in bold)
1 | 2 | 3 | 4 ....
... 4 | 5 | 6 | 7 | 8 | 9 | 10....
.. 10 | 11 | 12 | 13.
Simple, but versatile is what i was going for here. See what you think.
Code: Select all
<?php
/**
* A pagination class
*
* @version 0.1
* @author Mark Beech
* @date 1st November 2006
* @license http://www.gnu.org/licenses/lgpl.txt Lesser GNU Public License
*
* @copyright Copyright © 2006 Mark Beech - All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to
*
* The Free Software Foundation, Inc.,
* 51 Franklin Street,
* Fifth Floor,
* Boston,
* MA 02110-1301 USA
*
* "Mark Beech" <mbeech@mark-beech.co.uk>
*
*/
/*
USAGE:
$paginate = new paginate($sql, 3, (isset($_GET['start']) ? $_GET['start'] : 1));
$pagination = $paginate->getOutput();
OUTPUT:
[next] => Next >>
[prev] => << Prev
[first] => First
[last] => Last
[pageLinks] => 1 | 2 | 3 | 4 ...
[query] => SELECT * FROM `products` LIMIT 0,3
[viewing] => 1 to 3 of 13
OTHER POINTS OF INTEREST:
Each link has its own CSS class definition e.g. the default class name is 'paginate', therefore:
'Next >>' link will have the CSS class name 'paginateNext'.
'<< Prev' link will have the CSS class name 'paginatePrev'.
'<< First' link will have the CSS class name 'paginateFirst'.
'<< Last' link will have the CSS class name 'paginateLast'.
The page number links are simple 'paginate'
*/
/**
* A pagination class that returns various usuful pieces of data
*/
class paginate
{
var $query; // modified query
var $perPage; // number of results to show per page
var $maxPage; // the last page of the query result
var $currentPage; // the current select page
var $totalResults; // total number of results
var $result; // the result resource
var $pageLinksBefore = 3; // the number of page link we want to display after the current page
var $pageLinksAfter = 3; // the number of page link we want to display before the current page
var $pageNumDelimeter = " | "; // the elimeter we want to use to glue the page numbers together
var $linkClass = "pagination"; // the CSS style used for ALL links generated by this class
var $currentQueryString; // if there are an values in the query strign, we will be able to keep them on the page links
/**
* Sets required vars upon generation of the paginate class
* @param string sql query
* @param int number of results per to show per page
* @param int page we are currently viewing
* @param bool TRUE or FALSE - keep any values from the current query string
* @return void
*/
function paginate($query, $perPage, $currentPage, $maintainQueryString=TRUE)
{
$this->query = $query;
$this->perPage = $perPage;
$this->currentPage = $currentPage;
$this->maintainQueryString = $maintainQueryString;
if($this->maintainQueryString)
$this->getQueryString();
$this->totalResults();
$this->createNav();
$this->alterQuery();
$this->otherNavElements();
}
/**
* Stores the current query string (minus the start value) so we can insert it into the pagination links
* @return void
*/
function getQueryString()
{
if(!empty($_GET))
foreach($_GET as $k=>$v)
if($k != "start")
$this->currentQueryString .= $k."=".$v."&";
}
/**
* Gets the total number of results from the database
* @return void
*/
function totalResults()
{
$this->result = mysql_query($this->query);
$this->totalResults = mysql_num_rows($this->result);
if($this->totalResults != 0)
{
$this->maxPage = ceil(($this->totalResults / $this->perPage));
}
}
/**
* Creates the page link numbers
* @return void
*/
function createNav()
{
if($this->totalResults != 0)
{
if(($this->currentPage - $this->pageLinksBefore) < 1)
$this->lowerBoundary = 1;
else
$this->lowerBoundary = $this->currentPage - $this->pageLinksBefore;
if(($this->currentPage + $this->pageLinksAfter) > $this->maxPage)
$this->upperBoundary = $this->maxPage;
else
$this->upperBoundary = $this->currentPage + $this->pageLinksAfter;
for($x = $this->lowerBoundary; $x <= $this->upperBoundary; $x++)
{
if($this->currentPage == $x)
$this->pageNums[] = '<strong><a href="?'.$this->currentQueryString.'start='.$x.'" class="'.$this->linkClass.'">'.$x.'</a></strong>';
else
$this->pageNums[] = '<a href="?'.$this->currentQueryString.'start='.$x.'" class="'.$this->linkClass.'">'.$x.'</a>';
}
if(($this->currentPage - $this->pageLinksBefore) > 1)
$pageLinks = "... ";
$pageLinks .= implode($this->pageNumDelimeter, $this->pageNums);
if(($this->currentPage + $this->pageLinksAfter) < $this->maxPage)
$pageLinks .= " ...";
$this->pageLinks = $pageLinks;
}
}
/**
* Alters the query to limit the results according to the page the user is viewing
* @return void
*/
function alterQuery()
{
if($this->currentPage != 1)
$this->query .= ' LIMIT '.(($this->currentPage-1) * $this->perPage).','.$this->perPage;
else
$this->query .= ' LIMIT '.(($this->current) * $this->perPage).','.$this->perPage;
}
/**
* Create out other navigation elements, Prev, Next, First, Last etc.
* @return void
*/
function otherNavElements()
{
if($this->currentPage != 1)
{
$this->prevLink = '<a href="?'.$this->currentQueryString.'start='.($this->currentPage - 1).'" class="'.$this->linkClass.'Prev"><< Prev</a>';
}
if($this->currentPage != $this->maxPage)
{
$this->nextLink = '<a href="?'.$this->currentQueryString.'start='.($this->currentPage + 1).'" class="'.$this->linkClass.'Next">Next >></a>';
}
$this->firstLink = '<a href="?'.$this->currentQueryString.'start=1" class="'.$this->linkClass.'First">First</a>';
$this->lastLink = '<a href="?'.$this->currentQueryString.'start='.$this->maxPage.'" class="'.$this->linkClass.'Last">Last</a>';
if($this->currentPage == 1)
if($this->maxPage == 1)
$this->viewing = '1 to '.$this->totalResults.' of '.$this->totalResults;
else
$this->viewing = '1 to '.($this->currentPage * $this->perPage).' of '.$this->totalResults;
elseif($this->currentPage == $this->maxPage)
$this->viewing = ((($this->currentPage * $this->perPage) - $this->perPage) + 1).' to '.$this->totalResults.' of '.$this->totalResults;
else
$this->viewing = ((($this->currentPage * $this->perPage) - $this->perPage) + 1).' to '.($this->currentPage * $this->perPage).' of '.$this->totalResults;
}
/**
* Package all the data into a nice array
* @return array Array of the generated links etc.
*/
function getOutput()
{
$elements['next'] = $this->nextLink;
$elements['prev'] = $this->prevLink;
$elements['first'] = $this->firstLink;
$elements['last'] = $this->lastLink;
$elements['pageLinks'] = $this->pageLinks;
$elements['query'] = $this->query;
$elements['viewing'] = $this->viewing;
return $elements;
}
}
?>Code: Select all
<?php
$sql = "SELECT * FROM `products`";
/* instatiate a new instance of the pagination class */
$paginate = new paginate($sql, 9, (isset($_GET['start']) ? $_GET['start'] : 1));
$pagination = $paginate->getOutput();
?>Code: Select all
Array
(
[next] => Next >>
[prev] =>
[first] => First
[last] => Last
[pageLinks] => 1 | 2
[query] => SELECT * FROM `products` LIMIT 0,9
[viewing] => 1 to 9 of 13
)Example 2
Say you have a product catalog and the user has clicked into a certain category, and you display this by using GET values from the query string i.e. http://www.someonlinestore.com/catalog.php?categoryID=5
By default, the pagination class will maintain "categoryID=5" on all the pagination links it generates. This can be turned off by setting $maintainQueryString to FALSE
Example 3
Each link has its own CSS class definition e.g. the default class name is 'paginate', therefore:
- 'Next >>' link will have the CSS class name 'paginateNext'.
- '<< Prev' link will have the CSS class name 'paginatePrev'.
- 'First' link will have the CSS class name 'paginateFirst'.
- 'Last' link will have the CSS class name 'paginateLast'.
The page number links are simply 'paginate'.
The default class name can be changed by changing $linkClass.[/list]
Example 4
You can specify the number of links that should appear each side of the current page. The default it 3.
Example output is as follows (current page in bold)
1 | 2 | 3 | 4 ....
... 4 | 5 | 6 | 7 | 8 | 9 | 10....
.. 10 | 11 | 12 | 13.