ThinkPHP使用心得分享-分頁類Page的用法_PHP教程

來源:互聯網
上載者:User
ThinkPHP中的Page類在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,所以使用前要引入Page類:

複製代碼 代碼如下:
import('ORG.Util.Page'); //Page類的引入
$db = M('abc');//執行個體化資料表abc
$where = array(
'id'=>'2';
);//條件陳述式$where,例表中欄位id的值為2
$count = $db->where($where)->count();//擷取合格資料總數count
$page = new Page($count, 10);//執行個體化page類,傳入資料總數和每頁顯示10條內容
$limit = $page->firstRow . ',' . $page->listRows;//每頁的資料數和內容$limit
$result =$db->where($where))->limit($limit)->select();//分頁查詢結果
$this->result = $result;//賦值
$this->show = $page->show();//擷取分頁的底部資訊

以上代碼是分頁類實現的基本語句,當然喜歡使用原生sql語句的朋友也可以配合原生sql語句實現查詢分頁:

複製代碼 代碼如下:
import('ORG.Util.Page'); //Page類的引入
$db = M('abc');//執行個體化資料表abc
$where = array(
'id'=>'2';
);//條件陳述式$where,例表中欄位id的值為2
$count = $db->where($where)->count();//擷取合格資料總數count
$page = new Page($count, 10);//執行個體化page類,傳入資料總數和每頁顯示10條內容
$Modle = new Model();//執行個體化新資料模型
$sql = 'select id,name from abc where '.$where.' limit '.$page->firstRow.','.$page->listRows;//sql語句
$result = $Modle->query($sql);//執行sql語句
$this->result = $result
$this->show=$page->show();

當然,分布查詢擷取的內容也可以先對查詢完的資料進行處理再賦值,比如

複製代碼 代碼如下:
...

$result =$db->where($where))->limit($limit)->select();//分頁查詢結果
$res = abc($result);//abc方法(自訂方法或php函數)對結果$result進行資料排序或重組處理等
$this->result = $res;//賦值

http://www.bkjia.com/PHPjc/770585.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/770585.htmlTechArticleThinkPHP中的Page類在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,所以使用前要引入Page類: 複製代碼 代碼如下: import('ORG.Util.Page'); //Page類的引入...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.