thinkphp3.2 分页方式汇总

时间:2023-12-22 11:38:02

//自定义分页

$page = $_GET['page'] ? $_GET['page'] : 1 ;
$count = $this->Table("user")->count();
$page_size = 6;
$last_num = ceil($count/$page_size);
$page_limit = ($page-1)*$page_size;
$data = $this->Table("user")->limit($page_limit,$page_size)->getField('id,username,imgs');
return $data;

//框架分页
$count = $this->Table("user")->count();// 查询满足要求的总记录数
$Page = new \Think\Page($count,6);// 实例化分页类 传入总记录数和每页显示的记录数(25)
$show = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list = $this->Table("user")->limit($Page->firstRow.','.$Page->listRows)->getField('id,username,imgs');
$this->assign('list',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$data = array($list,$show);
return $data;

以上是Model层代码实例

=====================================================================================================================================================

现在是Controller层代码实例

$loan = M("loan");
$page = $_GET['page'] ? $_GET['page'] : 1 ;
$count = $loan->where("is_shelves = 1")->count();
$page_size = 6;
$last_num = ceil($count/$page_size);
$page_limit = ($page-1)*$page_size;
$data = $loan->where("is_shelves = 1")->limit($page_limit,$page_size)->field('corporate,img,corporate_url,person,propaganda,xquota,dquota,xterm,dterm,rate,qtime,type,authentication,condition,process,science')->select();
$data['page'] = $page+1;
return $this->response_json(SUCC,$data);

====================================================================================================================================================================================================================================

条件查询加分页
public function HomeSearch(){
$word = $_GET['word'];//接收查询关键字
$condition['person'] = array('like',"%$word%");//查询条件
$condition['is_shelves'] = 1;//查询条件
$loan = M("loan");
$page = $_GET['page'] ? $_GET['page'] : 1 ;
$count = $loan->where($condition)->count();
$page_size = 6;
$last_num = ceil($count/$page_size);
$page_limit = ($page-1)*$page_size;
$data = $loan->where($condition)->limit($page_limit,$page_size)->field('id,corporate,img,corporate_url,person,propaganda,xquota,dquota,xterm,dterm,rate,qtime,type,authentication,condition,process,science')->select();
  if($page==1){
  if($last_num==1){
  $data['currentpage'] = $page;
  }else{
  $data['currentpage'] = $page;
  $data['nextpage'] = $page+1;
  }

  }elseif ($page==$last_num){
  $data['lastpage'] = $page-1;
  $data['currentpage'] = $page;
  }else{
  $data['lastpage'] = $page-1;
  $data['currentpage'] = $page;
  $data['nextpage'] = $page+1;
  }
    return $this->response_json(SUCC,$data);
}