| 導購 | 订阅 | 在线投稿
分享
 
 
 

Pear::Pager分頁類簡介

2008-12-23 08:13:01  編輯來源:互聯網  简体版  手機版  移動版  評論  字體: ||

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/([a-z]{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

版權信息:可以任意轉載, 轉載時請務必以超鏈接形式標明文章原始出處和作者信息及此聲明

永久鏈接 - http://www.ooso.net/index.php/archives/250

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/([a-z]{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 版權信息:可以任意轉載, 轉載時請務必以超鏈接形式標明文章原始出處和作者信息及此聲明 永久鏈接 - [url=http://www.ooso.net/index.php/archives/250]http://www.ooso.net/index.php/archives/250[/url]
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有