Pear的Pager分頁類是一個很好用的php分頁類,擴展性很強,能夠適應各種分頁情況的需求,至少我在幾年間大大小小的項目裡,基本上沒有為分頁額外寫過代碼,全部都是用的Pager,足見Pager的可用性之強.下面用代碼來看看它的使用范例:
例子1
PLAIN TEXT
PHP:
<?php
require_oncePager/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:
echoPAGED DATA: ;print_r($data);
//Results from methods:
echogetCurrentPageID()...: ;var_dump($pager->getCurrentPageID());
echogetNextPageID()......: ;var_dump($pager->getNextPageID());
echogetPreviousPageID()..: ;var_dump($pager->getPreviousPageID());
echonumItems()...........: ;var_dump($pager->numItems());
echonumPages()...........: ;var_dump($pager->numPages());
echoisFirstPage()........: ;var_dump($pager->isFirstPage());
echoisLastPage().........: ;var_dump($pager->isLastPage());
echoisLastPageComplete().: ;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/([a-z]{1,12})/art([0-9]{1,4}).html$ /article.php?num=$2&month=$1 [L]
即使在這種情況下,Pager分頁仍然有辦法工作,見下面的code
PLAIN TEXT
PHP:
<?php
require_oncePager/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
版權信息:可以任意轉載, 轉載時請務必以超鏈接形式標明文章原始出處和作者信息及此聲明
永久鏈接 - http://www.ooso.net/index.php/archives/250