Stefan Gabos web developer extraordinaire
Zebra_Pagination, a generic pagination class written in PHP
- 1. Overview
- 2. Features
- 3. Requirements
- 4. Installation
- 5. How to use
- 6. Download
- 7. Documentation
- 8. Changelog
- 9. Comments
A generic pagination PHP library that automatically generates the HTML navigation links (next, previous, specific pages), given the total number of records and the number of records to be displayed per page.
Please note that this is a *generic* pagination PHP library, meaning that it does not display any records! It is up to developer to fetch the actual data and displaying it based on the information returned by this class. The advantage is that it can be used to paginate over records coming from any source (arrays, database, etc).
The appearance is customizable through CSS.
Zebra_Pagination‘s code is heavily commented and generates no warnings/errors/notices when PHP’s error reporting level is set to E_ALL.
Features review
- it is a generic library: can be used to paginate records from any source (arrays, databases, etc)
- it automatically generates navigation links, given the total number of items and the number of items per page (examples of best practices are also included)
- appearance is easily customizable through CSS
- code is heavily commented and generates no warnings/errors/notices when PHP’s error reporting level is set to E_ALL
- has comprehensive documentation
Requirements
PHP 4.4.9+
Installation
Download the latest version, unpack it, and put it in a place accessible to your scripts.
How to use
Make sure that in the <head> of your page you have
<link rel="stylesheet" href="path/to/Zebra_Pagination/public/css/zebra_pagination.css" type="text/css">
If you want to preserve hashes in the URL, also include the JavaScript file – simply including it will suffice;
(jQuery needs to also be loaded before loading this file)
<script type="text/javascript" src="path/to/Zebra_Pagination/public/javascript/zebra_pagination.js"></script>
Paginate data from an array:
<?php
// let's paginate data from an array...
$countries = array(
// array of countries
);
// how many records should be displayed on a page?
$records_per_page = 10;
// include the pagination class
require 'path/to/Zebra_Pagination.php';
// instantiate the pagination object
$pagination = new Zebra_Pagination();
// the number of total records is the number of records in the array
$pagination->records(count($countries));
// records per page
$pagination->records_per_page($records_per_page);
// here's the magick: we need to display *only* the records for the current page
$countries = array_slice(
$countries,
(($pagination->get_page() - 1) * $records_per_page),
$records_per_page
);
?>
<table>
<tr><th>Country</th></tr>
<?php foreach ($countries as $index => $country):?>
<tr<?php echo $index % 2 ? ' class="even"' : '')?>>
<td><?php echo $country?></td>
</tr>
<?php endforeach?>
</table>
<?php
// render the pagination links
$pagination->render();
?>
Paginate data from MySQL:
<?php
// how many records should be displayed on a page?
$records_per_page = 10;
// include the pagination class
require 'path/to/Zebra_Pagination.php';
// instantiate the pagination object
$pagination = new Zebra_Pagination();
// the MySQL statement to fetch the rows
// note how we build the LIMIT
// also, note the "SQL_CALC_FOUND_ROWS"
// this is to get the number of rows that would've been returned if there was no LIMIT
// see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
$MySQL = '
SELECT
SQL_CALC_FOUND_ROWS
country
FROM
countries
LIMIT
' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '
';
// if query could not be executed
if (!($result = @mysql_query($MySQL))) {
// stop execution and display error message
die(mysql_error());
}
// fetch the total number of records in the table
$rows = mysql_fetch_assoc(mysql_query('SELECT FOUND_ROWS() AS rows'));
// pass the total number of records to the pagination class
$pagination->records($rows['rows']);
// records per page
$pagination->records_per_page($records_per_page);
?>
<table class="countries" border="1">
<tr><th>Country</th></tr>
<?php $index = 0?>
<?php while ($row = mysql_fetch_assoc($result)):?>
<tr<?php echo $index++ % 2 ? ' class="even"' : ''?>>
<td><?php echo $row['country']?></td>
</tr>
<?php endwhile?>
</table>
<?php
// render the pagination links
$pagination->render();
?>
Download
Zebra_Pagination is distributed under the LGPL.
In plain English, this means that you have the right to view and to modify the source code of this software, but if you modify and distribute it, you are required to license your copy under a LGPL-compatible license, and to make the entire source code of your derivation available to anybody you distribute the software to.
You also have the right to use this software together with software thas has different licensing terms (including, but not limited to, commercial and closed-source software), and distribute the combined software, as long as state that your software contains portions licensed under the LGPL license, and provide information about where the LGPL licensed software can be downloaded.
If you distribute copies of this software you may not change the copyright or license of this software.
You may also like:
- Zebra_Database, a MySQL database wrapper written in PHP
- Zebra_Form, a jQuery augmented PHP library for creating and validating forms
- Zebra_Image, a lightweight image manipulation library written in PHP
- Zebra_Mptt, a PHP implementation of the modified preorder tree traversal algorithm
- Zebra_Session, a wrapper for PHP's default session handling functions, using MySQL for storage
Documentation
|
Become a ninja. Read the comprehensive documentation. |
Changelog
Click on a version to expand/collapse information.
- version 1.3 (May 03, 2012)
-
- the URL specified through the “base_path” method can now contain query strings; previously query strings in this value got automatically removed; also, any query strings existing in the page’s URL were *always* preserved – now the “base_path” method accepts an additional argument to disable this behavior; thanks to Kesuma, Augusto Carvalho dos Santos, moregatest and to all the people reporting these things;
- by also including a newly added JavaScript file, hashes in the URL can now also be preserved; the simple inclusion of the JavaScript file will do the trick; not including it will mean that hashes will not be preserved; thanks to Alan for requesting this;
- version 1.2.2 (October 14, 2011)
-
- fixed a bug where query strings got deleted if URLs were SEO friendly; thanks to theyouyou;
- added a new method for setting whether the script should add a trailing slash to the URLs when generating SEO friendly URLs; read more on the subject on Official Google Webmaster Central Blog; thanks to theyouyou for suggesting.
- version 1.2.1 (September 25, 2011)
-
- fixed a bug that appeared in version 1.2 that would remove all query string parameters from the URL, except the page-related one; thanks to kumbing for reporting;
- when method is “url”, the link to the first page does not include the “page” parameter anymore, in order to avoid duplicate content; previously this was true only for when method was “get”;
- version 1.2 (September 18, 2011)
-
- the link to the first page does not include the “page” parameter anymore, in order to avoid duplicate content; thanks to Sebi Popa for suggesting;
- some optimizations were made in the code;
- version 1.1c (May 05, 2011)
-
- fixed a bug with the “next” link, when on first page; thanks to Jan for reporting.
- version 1.1b (April 30, 2011)
-
- fixed a bug where disabling the “next” and “previous” links, when on first or last page respectively, was not working properly; thanks to Javier for reporting.
- version 1.1 (March 20, 2011)
-
- fixed a bug where the “padding” method was not working (thanks to D. Koper for reporting);
- fixed a bug where the “set_page” method was not working correctly (thanks to D. Koper for reporting);
- when there is a single page available, the pagination links are not displayed anymore (thanks to Sebi P. for the suggestion);
- default style was tweaked a bit;
- version 1.0.1 (January 08, 2011)
-
- entire code was audited and improved;
- cleaner output;
- more complete examples were added;
- method names, method arguments and global properties were changed and therefore this version breaks compatibility with previous ones;
- version 1.0 (June 04, 2009)
-
- initial release;



I am a 32 year old web developer working from Bucharest, Romania. I am coding since I was 14 and I am extremely passionate about it. For the server side of things I use PHP/MySQL while on the front-end I write valid HTML 5, nice CSS and lots of JavaScript code using jQuery.
Stefan, yet another great library. Totally awesome.
Could I please request, you currently have a ‘base_url’ option to modify the pagination links but could you please add an optional ‘anchor’ option which would allow an anchor to be added to the end of the pagination links.
Why? I have pagination inside tabbed content and so when I click a page link although it works, it shows the default tab instead of the tab I need.
Thanks
Replyi’ll consider this. thanks!
How to use this script, when using an Oracle database
the request is the following
Select column FROM (SELECT column FROM table ROWNUM)
WHERE ROWNUM BETWEEN START AND END
Replysorry, I can’t help you as I’ve never used Oracle…
alfred,
Short answer would be “you can’t”
Long answer would be because this fantastic library is written specifically to connect and talk to a mysql database. In order to make use of this with another type of database would require the entire library to be rewritten.
Actually, this is a *generic* library for pagination – it’s not bound to MySQL or any other database for that matter. It just happens that I use MySQL and that’s why the examples are with MySQL
Sorry Stefan. I had my brain stuck in Zebra_Database.
See what happens when so many great libraries that I end up using all of them
Implemented in a few minutes and it works great! Thanks for posting this. You saved the day.
ReplyI just modified the code a bit to make it match Twitter bootstrap since I’ve seen it gain a lot of popularity and I use if for lots of projects.
You can find my version here https://github.com/javiervd/Zebra-Pagination-for-Twitter-Bootstrap feel free to fork/clone
ReplyHow do I remove my index routing? Let’s say my URL something like this.
On pagination URL will grab my index.php routing..
How do I remove the route=buyer%2Fhistory? & only keep the &page=..
ReplyThanks Stefan Gabos
Hmm.. here is my Zebra_Pagination Object and still showing
Zebra_Pagination Object
(
[page] => 3
[_selectable_pages] => 11
[_records_per_page] => 1
[_records] => 5
[_padding] => 1
[_variable_name] => page
[_method] => get
[_trailing_slash] => 1
[_base_url] => http://domain.com/buyer/history/
[page_set] => 1
[_total_pages] => 5
)
But still showing my index routing.
http://domain.com/buyer/history/?route=buyer%2Fhistory&page=3
this is fixed now in version 1.3 – thanks!
Is possible sending more variables via get method ? It´s because may “where” query clause recives a value.
ReplyWhen my total records in the database reaches 10 or more..the pagination link disappears..
Replysometime you need to send more variables via get method.
Replyso I fixed some code from 1.2.2
ex:
base_url(‘http://www.example.com/index.php?target=product’);
?>
and you will got
http://www.example.com/index.php?target=product&page=2
http://www.example.com/index.php?target=product&page=3
….
plz see fixed code here
http://pastebin.com/7BZgYPwV
version 1.3, fixing these issues, was just released. thanks!
Dear Stefan,
Good job and keep going
Hi, i used library, but i need ajax for the use. I know a php library for ajax:” xajax”. How can i do for library work with xajax?
Reply