【ThinkPHP】PHP实现分页功能
查询列表数据,需要用到分页功能,下面是分页功能的代码:
/*** Summary of userList* @return \think\response\Json*/public function userList(){$page = input("page")?:1;//当前页数$size = input("size")?:10;//每页大小$start = ($page-1)*$size;//跳过的条数$total = 0;$pageTotal = 0;$userList = User::where([["deleted", "=", 0]])->order("id", "desc")->limit($start,$size)->select();$total = User::where([["deleted", "=", 0]])->order("id", "desc")->count();$pageTotal = ceil($total/$size);//计算总页数 ceil向上舍入为最接近的整数。return $this->success(["userList"=>$userList,"page"=>$page,"size"=>$size,"total"=>$total,"pageTotal"=>$pageTotal]);}
其中计算总页数时使用到来Math中的一个函数:ceil,作用是向上取最接近的整数,如:2.1 则取3。
floor():向下舍入为最接近的整数;
round():对浮点数进行四舍五入;
rand():返回随机整数。