thinkphp原生数据分页机制 --- 2018-10-26

//获得连接监控设备数据

public function getMonitorList()

{

    try {

        $page = I('post.page'); //当前页数

        //判断页数是否为空

        if(empty($page)){

            $page = 1;

        }

        $page_num = 10;//每页显示条数

        //数据开始

        $page_start = ($page - 1) * $page_num;

        //获取数据 搜索

        $search=I("post.phone");

        $map['phone']=array("like","%$search%");

        $res = M('monitor')->where($map)->order('create_time desc')->limit("$page_start, $page_num")->select();

        $count = M('monitor')->where($map)->count();// 查询满足要求的总记录数

        //组装数据并返回

        $data['data'] = $res;

        $data['count'] = $count;

        $data['page_num'] = $page_num;

        echo json_encode(['code' => 200, 'status'=>1,'data' => $data]);

    } catch (\Exception $e) {

        echo json_encode(['code' => -1, 'msg' => $e->getMessage()]);

    }

}

你可能感兴趣的:(thinkphp原生数据分页机制 --- 2018-10-26)