Name | Modified | Size | Downloads / Week |
---|---|---|---|
zebra_pagination.1.2.1.zip | 2012-01-07 | 22.6 kB | |
readme.md | 2012-01-07 | 4.6 kB | |
zebra_pagination.1.1.zip | 2011-03-20 | 22.5 kB | |
zebra_pagination.1.0.1.zip | 2011-01-08 | 21.6 kB | |
Totals: 4 Items | 71.3 kB | 0 |
Zebra_Pagination, a generic pagination class written in PHP
A generic pagination class that automatically generates navigation links given the total number of items and the number of items per page.
Please note that this is a generic pagination class, 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.
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">
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();
?>