ThinkPHP中的Page類在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,因此使用前要引入Page類:php
1 import('ORG.Util.Page'); //Page類的引入 2 $db = M('abc');//實例化數據表abc 3 $where = array( 4 'id'=>'2'; 5 );//條件語句$where,例表中字段id的值爲2 6 $count = $db->where($where)->count();//獲取符合條件的數據總數count 7 $page = new Page($count, 10);//實例化page類,傳入數據總數和每頁顯示10條內容 8 $limit = $page->firstRow . ',' . $page->listRows;//每頁的數據數和內容$limit 9 $result =$db->where($where))->limit($limit)->select();//分頁查詢結果 10 $this->result = $result;//賦值 11 $this->show = $page->show();//獲取分頁的底部信息
以上代碼是分頁類實現的基本語句,固然喜歡使用原生sql語句的朋友也能夠配合原生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();
固然,分佈查詢獲取的內容也能夠先對查詢完的數據進行處理再賦值,好比
函數
1 ... . . 9 $result =$db->where($where))->limit($limit)->select();//分頁查詢結果 10 $res = abc($result);//abc方法(自定義方法或php函數)對結果$result進行數據排序或重組處理等 10 $this->result = $res;//賦值
以上就是Jc對ThinkPHP中分頁類的使用小結,有哪些不對的地方或能夠改進的地方但願各位碼友提出~this