Home  >  Article  >  Backend Development  >  Please take a look at the implementation of PHP multi-table query plus paging

Please take a look at the implementation of PHP multi-table query plus paging

WBOY
WBOYOriginal
2016-07-06 13:54:131502browse

<code>public function my_join_activity(){
        header('Content-type:text/html;charset=utf-8');
        $Model=M();
        $unionid=session('unionid');
        if(!$unionid){
            $inde=A('Index');
            $inde->login();
        }
        //使用原生sql进行多表查询 注意where条件的and语句
        $list=$Model->query("select * from activity,apply where apply.ac_id=activity.ac_id and apply.unionid='{$unionid}' order by ap_id desc");
        $this->assign('list',$list);
        $this->display();
    }</code>

The TP framework is used. How to do paging in sql? For example, 5 data can be displayed on one page. How to do it?

Reply content:

<code>public function my_join_activity(){
        header('Content-type:text/html;charset=utf-8');
        $Model=M();
        $unionid=session('unionid');
        if(!$unionid){
            $inde=A('Index');
            $inde->login();
        }
        //使用原生sql进行多表查询 注意where条件的and语句
        $list=$Model->query("select * from activity,apply where apply.ac_id=activity.ac_id and apply.unionid='{$unionid}' order by ap_id desc");
        $this->assign('list',$list);
        $this->display();
    }</code>

The TP framework is used. How to do paging in sql? For example, 5 data can be displayed on one page. How to do it?

Use count and limit to implement

<code>$User = M('User'); // 实例化User对象
$count      = $User->where('status=1')->count();// 查询满足要求的总记录数
$Page       = new \Think\Page($count,25);// 实例化分页类 传入总记录数和每页显示的记录数(25)
$show       = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$this->display(); // 输出模板</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn