Pager class documentation

Overview

Pager is a PHP class which breaks down large data sets into different pages and generates HTML links to navigate between them.
Data set can be in different formats (array, MySQL recordset etc).

Features

Usage

By default Pager can be initialized in the following way:

$total = count($data);

or

// Using PDO MySQL extension
$stmt = 'SELECT count(*) as cnt FROM data';
$sth = $dbh->prepare($stmt);
$sth->execute();
$res = $sth->fetch();
$total = $res['cnt'];

$currentPage = (isset($_GET['p']) && $_GET['p'] != '') ? (int)$_GET['p'] : 1;
$pager = new Pager($total, $current);

Total number of items could be determined using count() function for PHP array or COUNT() function for MySQL recordset.
Current page value is passed via 'p' GET parameter.

getOffset() method is used to retrieve current page offset:

$offset = $pager->getOffset();

getOffset() returns -1 if offset is out of bounds and 0 or more otherwise.

To retrieve data to be displayed on particular page:

$pageData = array_slice($data, $offset, $itemsPerPage);

or

$pageStmt = 'SELECT country, cc_alpha2, cc_alpha3, cc_num FROM data LIMIT '
. $offset . ', ' . $itemsPerPage;

$sth = $dbh->prepare($pageStmt);
$sth->execute();

$pageData = $sth->fetchAll(PDO::FETCH_ASSOC);

To display navigation links it's needed to echo Pager object:

echo $pager;

Please check array.php and mysql.php examples for complete code

Advanced usage

Pager allows customization of number of items displayed per page, number of navigation links (links range), quick navigation (first, previous, next and last) links

$pager = new Pager(
$total,                         // Total number of items in dataset
$current,                       // Current page number
$itemsPerPage,                  // Number of items displayed per page (10 by default)
$range,                         // Number of navigation links to display (10 by default)
$options = array(               // Options to customize quick navigation links (empty by default)
    'first' => false,           // Always hide link to first page
    'prev' => true,             // Always show link to previous page
    'next' => true,             // Always show link to next page
    'last' => false,            // Always hide link to last page
    'firstLabel' => '<',     // Label of first link
    'prevLabel' => '<<',  // Label of previous link
    'nextLabel' => '>',      // Label of next link
    'lastLabel' => '>>'   // Label of last link
    )
);

By default (if omitted in $options array) first and previous links will be hidden on the first page, and next and last links will be hidden on the last page

getNavigation() method allows customization of navigation links HTML markup:

$navData = $pager->getNavigation();

$result = '<ul class="pager custom">';

foreach ($navData as $link) {
    $result .= '<li>
               <a href="?p=' . $link['page'] . '" class="' . $link['class'] . '">'
               . $link['label']
               . '</a></li>';
}

$result .= '</ul>';

echo $result;

$low = $offset + 1;

if ($currentPage >= $pager->pages) {
    $high = $pager->total;
} else {
    $high = $offset + $itemsPerPage;
}

if ($offset != -1) {
echo '<p>Displaying ' . $low . ' - ' . $high . ' of ' . $pager->total . '</p>';
}

Please check custom.php example for complete code