【ThinkPHP】PHP实现分页功能

查询列表数据,需要用到分页功能,下面是分页功能的代码:

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():返回随机整数。

相关推荐
apcipot_rain3 小时前
C++转Java日记(06)
java·开发语言
好的佩奇9 小时前
Dart 之集合元素
android·flutter·dart
SomeOtherTime10 小时前
Rust所有权(AI回答)
开发语言·后端·rust
Easyhood10 小时前
Android 开发入门实践(十四):Compose 副作用入门与四个常用 API
android·开发语言·kotlin·android jetpack·compose
白杨尚青11 小时前
C++进阶篇(六):map 和 set 的模拟实现——用红黑树封装 mymap/myset(迭代器 + operator[])
开发语言·c++·迭代器·stl·set·map·红黑树
FlightYe11 小时前
音视频修炼之容器与协议(四):RTSP详解
android·linux·ffmpeg·音视频·dash
疯华整猫13 小时前
删掉 LeakCanary 那天,我用 Trae 自己撸了一套:最难的不是分析堆,是决定什么时候 dump
android·人工智能·性能优化
饮水桶_T15 小时前
JAVA IO流笔记
开发语言·数据结构·算法
Felven17 小时前
C. Brr Brrr Patapim
c语言·开发语言
路溪非溪18 小时前
python标准库使用总结
开发语言·python