在頁面上顯示大量數據,是WEB項目中很常見的一個作法,但限於屏幕的大小,為了便於用戶閱讀,我們通常需要將數據分頁顯示,所以分頁功能是大部份項目不可缺少的。PEAR::Pager是一個功能強大的分頁類,使用非常方便。
系統需求:PHP4.3.* /PHP5,PEAR::Pager
PEAR::Pager有兩種分頁顯示模式,一是Jumping,二是Sliding。兩者有什麼區別,我們來看兩個例子:
Jumping.php
<?php
require_once 'Pager/Pager.php';
$params = array(
'mode' => 'Jumping',
'perPage' => 3,
'delta' => 5,
'itemData' => array('a','b','c','d','e','z','ty','xc','fg','fg','jk','hj','ty','xc','e','z','ty','xc','fg','fg','jk','hj','ty','xc')
);
echo "<BR>當前分頁模式:".$params['mode'];
echo "<BR>每頁顯示數據條數:".$params['perPage'];
echo "<BR>顯示頁數:".$params['delta'];
echo "<BR>具體數據數組:";
print_r($params['itemData']);
$pager = & Pager::factory($params);
$data = $pager->getPageData();
$links = $pager->getLinks();
echo "<P>最後效果:";
echo $links['all'];
echo $pager->linkTags;
echo '<P>當前頁的數據: ' ;
echo "<pre>";
print_r($data);
echo "</pre>";
echo "其它類方法得到的數據:<P>";
echo 'getCurrentPageID()...: ';
var_dump($pager->getCurrentPageID());
echo "<BR>";
echo 'getNextPageID()......: ';
var_dump($pager->getNextPageID());
echo "<BR>";
echo 'getPreviousPageID()..: ';
var_dump($pager->getPreviousPageID());
echo "<BR>";
echo 'numItems()...........: ';
var_dump($pager->numItems());
echo "<BR>";
echo 'numPages()...........: ';
var_dump($pager->numPages());
echo "<BR>";
echo 'isFirstPage()........: ';
var_dump($pager->isFirstPage());
echo "<BR>";
echo 'isLastPage().........: ';
var_dump($pager->isLastPage());
echo "<BR>";
echo 'isLastPageComplete().: ';