通常在管理後台實作時,常設定許多查詢條件來查詢資料,比如:起始、結束時間。通常這些參數都是用GET的方式在傳遞。以下的範例程式,主要說明如何把GET的參數,融入CakePHP的分頁功能裏面。
Controller:post_controller.php
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
class PostController extends AppController {
public $name = 'Post';
public $uses = null;
function all() {
// 分頁設定
$this->paginate = array(
'fields' => array('Article.id', 'Article.title', 'Article.created'),
'limit' => 3,
'order' => 'Article.created desc',
);
/* 如果有自訂的GET參數,取得到,加到 Controller 的 passedArgs */
$tmp = '';
if(isset($_GET['a']) && ($tmp = trim($_GET['a'])) != "")
$this->passedArgs['a'] = $tmp;
if(isset($_GET['b']) && ($tmp = trim($_GET['b'])) != "")
$this->passedArgs['b'] = $tmp;
$this->loadModel("Article");
/*$this->Article->recursive = false;*/
$this->Article->recursive = 0;
$this->set('articles', $this->paginate($this->Article,"Article.user_id='".$this->UserID."'"));
}// all
}// enc Post
View: all.ctp
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<?php
$args = $this->passedArgs;
unset($args['page']); /* 記得要去除 page 參數,否則 CakePHP 會當成一般的參數處理,屆時連結會多出很多page參數 */
// 把額外的參數傳給分頁模組
$paginator->options(
array('url'=>$args));
$th = array($paginator->sort("標題", "title"), $paginator->sort("建立時間", "created"), "操作");
echo $html->tableHeaders($th);
$size = count($articles);
for($i = 0; $i < $size; $i++) {
$tr = array($articles[$i]["Article"]["title"], $articles[$i]["Article"]["created"], "<a href='/post/edit/".$articles[$i]["Article"]["id"]."'>編輯</a>");
echo $html->tableCells($tr, array("bgcolor"=>"#999999"), array("bgcolor"=>"white"));
}// end for
?>
</table>
<center>
<?php
echo $paginator->first('第一頁'). ' ';
echo $paginator->prev('<< 前一頁').' ';
echo $paginator->numbers(array('separator'=>' - '));
echo ' '.$paginator->next('下一頁 >>');
echo ' '.$paginator->last("最後一頁");
?>
<br>
<?php
echo $paginator->counter(array(
'format' => '目前在第 %page% 頁 / 總共 %pages% 頁, 每頁 %current% 筆資料, 總共 %count% 筆, 目前是第 %start% 至 %end% 筆資料'
));
?>
</center
HTML
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='5' width='80%' align='center'>
<tr><th><a href="/post/all/3/page:2/a:c/b:d/sort:title/direction:desc">標題</a></th>
<th><a href="/post/all/3/page:2/a:c/b:d/sort:created/direction:asc">建立時間</a></th>
<th>操作</th>
</tr>
<tr bgcolor="#999999>資料顯示在這裏(略)</tr></table>
<center>
<span><a href="/post/all/3/page:1/a:c/b:d/sort:title/direction:asc">第一頁</a></span> <a href="/post/all/3/page:1/a:c/b:d/sort:title/direction:asc"><< 前一頁</a> <span><a href="/post/all/3/page:1/a:c/b:d/sort:title/direction:asc">1</a></span> - <span class="current">2</span> - <span><a href="/post/all/3/page:3/a:c/b:d/sort:title/direction:asc">3</a></span> - <span><a href="/post/all/3/page:4/a:c/b:d/sort:title/direction:asc">4</a></span> <a href="/post/all/3/page:3/a:c/b:d/sort:title/direction:asc">下一頁 >></a> <span><a href="/post/all/3/page:4/a:c/b:d/sort:title/direction:asc">最後一頁</a></span><br>
目前在第 2 頁 / 總共 4 頁, 每頁 3 筆資料, 總共 12 筆, 目前是第 4 至 6 筆資料</center
由上面,可以看到自訂的參數也是以 /參數名稱:參數值 的方式,加到連結裏面。同理,在controller 中,可以透過passedArgs取得以/參數名稱:參數值的數值。
原文:http://otaku.kigi.tw/2008/03/cakephp-pagination_25.html