ThinkPHP使用心得分享-分页类Page的用法_PHP教程

WBOY
Libérer: 2016-07-13 10:29:40
original
869 Les gens l'ont consulté

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;//赋值

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/770585.htmlTechArticleThinkPHP中的Page类在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,所以使用前要引入Page类: 复制代码 代码如下: import('ORG.Util.Page'); //Page类的引入...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!