先上function中封装的函数
<?php function get_page($arr,$p,$pageSize) { import("Think.Page"); //导入分页类 $count = count($arr); $Page = new Think\Page($count,$pageSize); $start=($p- 1) *$pageSize; $length= $pageSize; $page_count=ceil($count/$pageSize); $cut_qa= array_slice($arr, $start, $length, true); $page = $Page->show(); $res=array( ‘list‘=>$cut_qa, ‘page‘=>$page, ‘count‘=>$page_count ); return $res; } ?>
$arr为需要分页的数组,$p为前端传过来的当前页,$pageSize为每页输出的条数。
下面为控制器中的代码:
public function owner_contract(){ //查询数据库 $list=M(‘contract‘)->select(); $company=get_page($list,I(‘p‘),10); $this->assign(‘company‘,$company[‘list‘]); if (I("get") == "ajax") { $this->ajaxReturn(array( "content" => $this->fetch(‘Contract/owner_contract_list‘), "page"=>$company[‘count‘], "p"=> I(‘p‘) )); } else { $this->display(); } }