Home >Backend Development >PHP Tutorial >Please take a look at the implementation of PHP multi-table query plus paging
<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?
<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>