欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ThinkPHP使用心得分享-分頁(yè)類Page的用法

 更新時(shí)間:2014年05月15日 09:50:26   作者:  
ThinkPHP中的Page類能輕松實(shí)現(xiàn)查詢內(nèi)容分頁(yè)的實(shí)現(xiàn),本文小總結(jié)了一下關(guān)于學(xué)習(xí)過(guò)程中對(duì)Page類的使用方法。

ThinkPHP中的Page類在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,所以使用前要引入Page類:

復(fù)制代碼 代碼如下:

import('ORG.Util.Page'); //Page類的引入
$db = M('abc');//實(shí)例化數(shù)據(jù)表abc
$where = array(
'id'=>'2';
);//條件語(yǔ)句$where,例表中字段id的值為2
$count = $db->where($where)->count();//獲取符合條件的數(shù)據(jù)總數(shù)count
$page = new Page($count, 10);//實(shí)例化page類,傳入數(shù)據(jù)總數(shù)和每頁(yè)顯示10條內(nèi)容
$limit = $page->firstRow . ',' . $page->listRows;//每頁(yè)的數(shù)據(jù)數(shù)和內(nèi)容$limit
$result =$db->where($where))->limit($limit)->select();//分頁(yè)查詢結(jié)果
$this->result = $result;//賦值
$this->show = $page->show();//獲取分頁(yè)的底部信息

以上代碼是分頁(yè)類實(shí)現(xiàn)的基本語(yǔ)句,當(dāng)然喜歡使用原生sql語(yǔ)句的朋友也可以配合原生sql語(yǔ)句實(shí)現(xiàn)查詢分頁(yè):

復(fù)制代碼 代碼如下:

        import('ORG.Util.Page'); //Page類的引入
        $db = M('abc');//實(shí)例化數(shù)據(jù)表abc
        $where = array(
           'id'=>'2';
        );//條件語(yǔ)句$where,例表中字段id的值為2
        $count = $db->where($where)->count();//獲取符合條件的數(shù)據(jù)總數(shù)count
        $page = new Page($count, 10);//實(shí)例化page類,傳入數(shù)據(jù)總數(shù)和每頁(yè)顯示10條內(nèi)容
        $Modle = new Model();//實(shí)例化新數(shù)據(jù)模型
        $sql = 'select id,name from abc where '.$where.' limit '.$page->firstRow.','.$page->listRows;//sql語(yǔ)句
        $result = $Modle->query($sql);//執(zhí)行sql語(yǔ)句
        $this->result = $result
        $this->show=$page->show();

當(dāng)然,分布查詢獲取的內(nèi)容也可以先對(duì)查詢完的數(shù)據(jù)進(jìn)行處理再賦值,比如

復(fù)制代碼 代碼如下:

     ...

    $result =$db->where($where))->limit($limit)->select();//分頁(yè)查詢結(jié)果
    $res = abc($result);//abc方法(自定義方法或php函數(shù))對(duì)結(jié)果$result進(jìn)行數(shù)據(jù)排序或重組處理等
    $this->result = $res;//賦值

相關(guān)文章

最新評(píng)論