Introduction

Introduction -- Usage of Pager_Sliding

What is Pager_Sliding?

Pager_Sliding is a class to page an array of data. It is taken as input and it is paged according to various parameters. Pager_Sliding also builds links within a specified range, and allows complete customization of the output (it even works with mod_rewrite). It is compatible with PEAR::Pager's API

Example 1

This simple example will page the array of alphabetical letters, giving back pages with 3 letters per page, and links to the previous two / next two pages:
require_once 'Pager/Sliding.php';
$params = array(
            'perPage'    => 3,
            'delta'      => 2,
            'itemData'   => array('a','b','c','d','e',[...omissis...],'z')
            );
$pager = & new Pager_Sliding($params);
$data  = $pager->getPageData();
$links = $pager->getLinks();
//$links is an ordered+associative array with 'back'/'pages'/'next'/'first'/'last'/'all' links.
//NB: $links['all'] is the same as $pager->links;

//echo links to other pages:
echo $links['all']

//Show data for current page:
echo 'PAGED DATA: ' ; print_r($data);

//Results from methods:
echo 'getCurrentPageID()...: '; var_dump($pager->getCurrentPageID());
echo 'getNextPageID()......: '; var_dump($pager->getNextPageID());
echo 'getPreviousPageID()..: '; var_dump($pager->getPreviousPageID());
echo 'numItems()...........: '; var_dump($pager->numItems());
echo 'numPages()...........: '; var_dump($pager->numPages());
echo 'isFirstPage()........: '; var_dump($pager->isFirstPage());
echo 'isLastPage().........: '; var_dump($pager->isLastPage());
echo 'isLastPageComplete().: '; var_dump($pager->isLastPageComplete());
echo '$pager->range........: '; var_dump($pager->range);

Example 2

This example shows how you can use this class with mod_rewite. Let's suppose we have a .htaccess like this:
---------
RewriteEngine on
#Options FollowSymlinks

RewriteBase /
RewriteRule ^articles/([a-z]{1,12})/art([0-9]{1,4})\.html$ /article.php?num=$2&month=$1 [L]
---------
It should transform an url like "/articles/march/art15.html" into "/article.php?num=15&month=march"

require_once 'Pager/Sliding.php';

$month = 'september';
$params = array(
            'append'    => false,
            'urlVar'    => 'num',
            'path'      => 'http://myserver.com/articles/' . $month,
            'fileName'  => 'art%d.html',  //Pager replaces "%d" with page number...
            'itemData'  => array('a','b','c',[...omissis...],'z'),
            'perPage'   => 3
            );
$pager = & new Pager_Sliding($params);

$data  = $pager->getPageData();
echo $pager->links;
echo 'Data for current page: '; print_r($data);