PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » HTML » Pager_Sliding » Manual

Data paging class which also builds links to the pages.

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

Deprecated

This package is deprecated in favour of the new Pager v.2.x.

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:

<?php
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);
?>

In case you're wondering, $pager->range is a numeric array; its keys are the numbers of the pages in the current range, and the matching values are booleans (TRUE if its key represents currentPage, FALSE otherwise). This array can be useful to build the links manually, e.g. when using a template engine.

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&amp;month=$1 [L]
---------

It should transform an url like "/articles/march/art15.html" into "/article.php?num=15&month=march"

<?php
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);
?>

More pagers in a single page

Using more than one pager in a single page is as simple as using a different urlVar for each pager:

<?php
require_once 'Pager/Sliding.php';

//first pager
$params1 = array(
            'perPage'    => 3,
            'urlVar'     => pageID_articles,  //1st identifier
            'itemData'   => $someArray
            );
$pager1 = & new Pager_Sliding($params1);
$data1  = $pager1->getPageData();
$links1 = $pager1->getLinks();

//second pager
$params2 = array(
            'perPage'    => 8,
            'urlVar'     => pageID_news,      //2nd identifier
            'itemData'   => $someOtherArray
            );
$pager2 = & new Pager_Sliding($params2);
$data2  = $pager2->getPageData();
$links2 = $pager2->getLinks();
?>

Pager (v.1.x) vs. Pager_Sliding

Pager (v.1.x) vs. Pager_Sliding – Feature comparison of the two classes

What are the differences with Pager?

While Pager v.1.x has a "jumping window" style, Pager_Sliding has a "sliding window" style. What does that mean? Let's see an example:

Pager v.1.x logic

Let's suppose that the data spans on 15 pages, and the window width is 5 page links. The links are built on "frames" of 5 pages each: [1-5] [6-10] [11-15] Pager v.1.x always shows the same 5 page links while you are on one of these pages. Here's a temporal succession of the links, starting from page 1 and moving forward. There are brakets around current page number to highlight this:

<?php
a)    {1} 2  3  4  5  =>   // first frame: [1-5]
b) <=  1 {2} 3  4  5  =>
c) <=  1  2 {3} 4  5  =>
d) <=  1  2  3 {4} 5  =>
e) <=  1  2  3  4 {5} =>   // HERE IT JUMPS TO THE NEXT FRAME
f) <= {6} 7  8  9 10  =>   // second frame: [6-10]
g) <=  6 {7} 8  9 10  =>
h) <=  6  7 {8} 9 10  =>
?>

and so on. See what a "jumping window" frame is? When you reach a limit (in the example, you go from page 5 to page 6), it "jumps" to next frame (links from page 6 to 10).

Pager_Sliding logic

Instead of jumping from one frame to the other, with Pager_Sliding the change is done smoothly, and the current page is always shown at the center of the "window" (except of course for the first and the last pages):

<?php
a)       {1} 2  3  4  5  => [15]
b) [1] <= 1 {2} 3  4  5  => [15]
c) [1] <= 1  2 {3} 4  5  => [15]  // HERE IT's STARTING WORKING AS DESIGNED
d) [1] <= 2  3 {4} 5  6  => [15]  // see: current page number is at the center of the window
e) [1] <= 3  4 {5} 6  7  => [15]  // and it stays there...
f) [1] <= 4  5 {6} 7  8  => [15]
g) [1] <= 5  6 {7} 8  9  => [15]
h) [1] <= 6  7 {8} 9 10  => [15]
?>

and so on.

Other differences

Apart from the different "philosophy", Pager_Sliding is also designed to be highly customizable.

UPDATE: since Pager 2.x release, every option once only available in Pager_Sliding is now fully implemented in Pager too, and more. As stated in the Intro, Pager_Sliding is now deprecated in favour of the new Pager v.2.x. You can have both behaviours ("Jumping" and "Sliding") in Pager v.2.x, just change the mode. Please refer to Pager docs for more details.

Pager_Sliding::getCurrentPageID

Pager_Sliding::getCurrentPageID() – Returns current page number

Synopsis

require_once 'Pager/Sliding.php';

integer getCurrentPageID ( )

Return value

integer - Current page number.

Pager_Sliding::getNextPageID

Pager_Sliding::getNextPageID() – Returns next page number.

Synopsis

require_once 'Pager/Sliding.php';

mixed Pager_Sliding::getNextPageID ( )

Description

If current page is last page this function returns FALSE, otherwise returns next page number.

Return value

return Next page number or FALSE

Pager_Sliding::getOffsetByPageId

Pager_Sliding::getOffsetByPageId() – Returns offsets for given pageID.

Synopsis

require_once 'Pager/Sliding.php';

array Pager_Sliding::getOffsetByPageId ( integer $pageid = null )

Description

Eg, if you pass it pageID = 5 and your delta is 2 it will return you 3 and 7. PageID of 6 would give you 4 and 8

NB: The behaviour of this function could be misleading: it was left only for compatibility with PEAR::Pager. It could raise some confusion when pageID is within delta positions from an extreme: in fact this method returns also the extremes, while $this->_getPageLinks leaves them out. Pager_Sliding works this way: if pageID is NOT an extreme, show first and last page within brackets: [1] << 5 | _6_ | 7 >> [15] So when dealing with pageID within delta positions from an extreme, this method would return the extreme as well, while $this->_getPageLinks would return (for instance) 2 | _3_ | 4 | 5 even if pageID is 3 and delta is 2. Consider this method deprecated and/or subject to changes.

Parameter

Return value

return array with first and last offsets

Deprecated

deprecated

Pager_Sliding::getPageData

Pager_Sliding::getPageData() – Returns an array of current pages data

Synopsis

require_once 'Pager/Sliding.php';

array Pager_Sliding::getPageData ( integer $pageID = null )

Parameter

Return value

return array of data for this page.

Pager_Sliding::getPageIdByOffset

Pager_Sliding::getPageIdByOffset() – Overload PEAR::Pager method. VOID.

Synopsis

require_once 'Pager/Sliding.php';

array Pager_Sliding::getPageIdByOffset ( integer $index )

Parameter

Return value

return Page number for this offset

Deprecated

deprecated. It is only here for compatibility with PEAR::Pager.

Pager_Sliding::getPreviousPageID

Pager_Sliding::getPreviousPageID() – Returns previous page number.

Synopsis

require_once 'Pager/Sliding.php';

mixed Pager_Sliding::getPreviousPageID ( )

Description

If current page is first page this function returns FALSE, otherwise returns previous page number.

Return value

return Previous page number or FALSE.

Pager_Sliding::isFirstPage

Pager_Sliding::isFirstPage() – Returns whether current page is first page

Synopsis

require_once 'Pager/Sliding.php';

bool Pager_Sliding::isFirstPage ( )

Return value

return TRUE or FALSE, wrt it is the first page or not.

Pager_Sliding::isLastPage

Pager_Sliding::isLastPage() – Returns whether current page is last page

Synopsis

require_once 'Pager/Sliding.php';

bool Pager_Sliding::isLastPage ( )

Return value

return TRUE or FALSE, wrt it is the last page or not.

Pager_Sliding::isLastPageComplete

Pager_Sliding::isLastPageComplete() – Returns whether last page is complete

Synopsis

require_once 'Pager/Sliding.php';

bool Pager_Sliding::isLastPageComplete ( )

Return value

return TRUE or FALSE, wrt the last page is complete (i.e. it has perPage values in the array for the last page) or not.

Pager_Sliding::numItems

Pager_Sliding::numItems() – Returns number of items

Synopsis

require_once 'Pager/Sliding.php';

int Pager_Sliding::numItems ( )

Return value

return integer - Number of items

Pager_Sliding::numPages

Pager_Sliding::numPages() – Returns number of pages

Synopsis

require_once 'Pager/Sliding.php';

int Pager_Sliding::numPages ( )

Return value

return integer - Number of pages

Pager_Sliding::Sliding

Pager_Sliding::Sliding() – Creates a pager instance

Synopsis

require_once 'Pager/Sliding.php';

object &Sliding ( array $options )

Parameter

Pager_Sliding constructor takes an associative array of parameters as input values. This is the complete list of these options:

REQUIRED options are:

Return value

object - a specific Pager_Sliding instance or a PEAR_Error object, if fails