CakePHP Pagination (分頁功能) 加入自己的參數
生活随笔
收集整理的這篇文章主要介紹了
CakePHP Pagination (分頁功能) 加入自己的參數
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
如果還不知道什麼是 Pagination 或者還不了解如何使用,請(qǐng)參考: CakePHP Pagination (分頁功能) 。
通常在管理後臺(tái)實(shí)作時(shí),常設(shè)定許多查詢條件來查詢資料,比如:起始、結(jié)束時(shí)間。通常這些參數(shù)都是用GET的方式在傳遞。以下的範(fàn)例程式,主要說明如何把GET的參數(shù),融入CakePHP的分頁功能裏面。
Controller:post_controller.php
Code
class?PostController?extends?AppController?{
?public?$name?=?'Post';
?public?$uses?=?null;
?function?all()?{
?//?分頁設(shè)定
?$this->paginate?=?array(
?'fields'?=>?array('Article.id',?'Article.title',?'Article.created'),?
?'limit'?=>?3,?
?'order'?=>?'Article.created?desc',?
?);
?/*?如果有自訂的GET參數(shù),取得到,加到?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
Code
<?php
?$args?=?$this->passedArgs;
?unset($args['page']);?/*?記得要去除?page?參數(shù),否則?CakePHP?會(huì)當(dāng)成一般的參數(shù)處理,屆時(shí)連結(jié)會(huì)多出很多page參數(shù)?*/
?//?把額外的參數(shù)傳給分頁模組
?$paginator->options(
?array('url'=>$args));
?$th?=?array($paginator->sort("標(biāo)題",?"title"),?$paginator->sort("建立時(shí)間",?"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
Code
<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">標(biāo)題</a></th>
?<th><a?href="/post/all/3/page:2/a:c/b:d/sort:created/direction:asc">建立時(shí)間</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
通常在管理後臺(tái)實(shí)作時(shí),常設(shè)定許多查詢條件來查詢資料,比如:起始、結(jié)束時(shí)間。通常這些參數(shù)都是用GET的方式在傳遞。以下的範(fàn)例程式,主要說明如何把GET的參數(shù),融入CakePHP的分頁功能裏面。
Controller:post_controller.php
Code
class?PostController?extends?AppController?{
?public?$name?=?'Post';
?public?$uses?=?null;
?function?all()?{
?//?分頁設(shè)定
?$this->paginate?=?array(
?'fields'?=>?array('Article.id',?'Article.title',?'Article.created'),?
?'limit'?=>?3,?
?'order'?=>?'Article.created?desc',?
?);
?/*?如果有自訂的GET參數(shù),取得到,加到?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
Code
<?php
?$args?=?$this->passedArgs;
?unset($args['page']);?/*?記得要去除?page?參數(shù),否則?CakePHP?會(huì)當(dāng)成一般的參數(shù)處理,屆時(shí)連結(jié)會(huì)多出很多page參數(shù)?*/
?//?把額外的參數(shù)傳給分頁模組
?$paginator->options(
?array('url'=>$args));
?$th?=?array($paginator->sort("標(biāo)題",?"title"),?$paginator->sort("建立時(shí)間",?"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
Code
<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">標(biāo)題</a></th>
?<th><a?href="/post/all/3/page:2/a:c/b:d/sort:created/direction:asc">建立時(shí)間</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
由上面,可以看到自訂的參數(shù)也是以 /參數(shù)名稱:參數(shù)值 的方式,加到連結(jié)裏面。同理,在controller 中,可以透過passedArgs取得以/參數(shù)名稱:參數(shù)值的數(shù)值。
?
原文:http://otaku.kigi.tw/2008/03/cakephp-pagination_25.html
轉(zhuǎn)載于:https://www.cnblogs.com/derrck/archive/2009/05/14/1456585.html
總結(jié)
以上是生活随笔為你收集整理的CakePHP Pagination (分頁功能) 加入自己的參數的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [TJOI2013]松鼠聚会
- 下一篇: 用Python实现简单的人脸识别,10分