PHP Classes
elePHPant
Icontem

File: limit/limit.pager.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Johan Barbier  >  SPL and Iterators  >  limit/limit.pager.php  >  Download  
File: limit/limit.pager.php
Role: Example script
Content type: text/plain
Description: Example on how to build a pager with oLimit package
Class: SPL and Iterators
Implements several iterators using SPL
Author: By
Last change: Added documentation
Date: 8 years ago
Size: 1,462 bytes
 

Contents

Class file image Download
<?php
/**
 * Basic pager example
 */
require_once 'package.oLimit.php';

/**
 * How many items are to be displayed on the page
 *
 */
DEFINE ('PER_PAGE', 20);
/**
 * Defining the array to be displayed
 */
$aTab = range (1, 638, 5);
/**
 * Counting how many pages will be needed
 */
$iNbPages = ceil (count ($aTab)/PER_PAGE);
/**
 * checking and overwriting if necessary the GET variable (page number p)
 */
if (!isset ($_GET['p']) || !is_numeric ($_GET['p']) || $_GET['p'] < 0 || $_GET['p'] >= $iNbPages) {
   
$_GET['p'] = 0;
}
/**
 * Displaying the pager
 */
$sHtml = '<div>';
for (
$i = 0; $i < $iNbPages; $i ++) {
   
$iDisplay = $i + 1;
   
$sColor=(isset($_GET['p']) && (int)$_GET['p'] === $i)?'background-color: rgb(195,195,195);':'';
   
$sHtml .= <<<HTML
<span style="border: 1px solid rgb(0,0,0); padding: 3px; {$sColor}"><a href="limit.pager.php?p={$i}">{$iDisplay}</a></span>
HTML;
}
$sHtml .= '</div><br /><br />';
/**
 * Determining starting offset from current page number
 */
$iOffset = (int)$_GET['p'] * PER_PAGE;
echo
$sHtml;

try {
   
/**
     * Creating the arrayLimit object, using the factory, and determined values (starting offset, items to be displayed on a page)
     */
   
$limit = LimitFactory::factory ('ARRAY', $aTab, $iOffset, PER_PAGE);
    while (
true === $limit -> valid()) {
        echo
$limit -> key (), ' => ', $limit -> current (), '<br />';
       
$limit -> next ();
    }
} catch (
Exception $e) {
    echo
$e -> getMessage (), ' on line ', $e -> getLine ();
}
?>