Pear的Pager分頁類是一個很好用的php分頁類,擴展性很強,能夠適應各種分頁情況的需求,至少我在幾年間大大小小的項目裡,基本上沒有為分頁額外寫過代碼,全部都是用的Pager,足見Pager的可用性之強.下面用程式碼來看看它的使用範例:
範例1
PLAIN TEXT
PHP:
<?php
require_once'Pager/Pager.php';
$params=array(
'mode' =>'Jumping',
'perPage' =>3,
'delta' =>2,
'itemData' =>array('a','b','c','d','e',[...omissis...],'z')
);
$pager= & Pager::factory($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'];
//Pager can also generate <link rel="first|prev|next|last"> tags
echo$pager->linkTags;
//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);
?>
Pager的使用,只要調整$param數組的參數,就可以應付很多種分頁情況了.代碼中的$links數組包含了一些鏈接,如上一頁/頁碼/下一頁/第一頁/末頁/全部.
例2
如今許多網站為了SEO,把動態頁使用rewrite規則偽造成靜態頁的形式,例如下面這個.htaccess配置:
RewriteEngine on
#Options FollowSymlinks
RewriteBase /
RewriteRule ^articles/([az]{1,12})/art([0-9]{1,4}).html$ /article.php?num=$2&month=$1 [L]
即使在這種情況下,Pager分頁仍然有辦法工作,見下面的code
PLAIN TEXT
PHP:
<?php
require_once'Pager/Pager.php';
//first pager
$params1=array(
'perPage' =>3,
'urlVar' => pageID_articles, //1st identifier
'itemData' =>$someArray
);
$pager1= & Pager::factory($params1);
$data1 =$pager1->getPageData();
$links1=$pager1->getLinks();
//second pager
$params2=array(
'perPage' =>8,
'urlVar' => pageID_news, //2nd identifier
'itemData' =>$someOtherArray
);
$pager2= & Pager::factory($params2);
$data2 =$pager2->getPageData();
$links2=$pager2->getLinks();
?>
透過配置$param,就能夠把連結"/articles/march/art15.html"對應到連結"/article.php?num=15&month=march" ,比較靈活的表現
擴展性
Pager類的擴展性,平心而論,也是不錯的.比如以前寫的path方式的分頁類- Pager::Pathing(),這個方法就是從Pager上擴展而來,滿足了當時的需求.
作者:volcano發表於9月22, 2006 at 7:16 am
版權資訊: 可任意轉載, 轉載時請務必以超連結形式標明文章原始出處和作者資訊及此聲明