php接口分页
/**
* 物业查看的用户列表
*/
public function CustomerPageOfProperty() {
// name: "" , page: 1 , phone: "" , size: 10
$name = $this->input_data['name'];
$phone = $this->input_data['phone'];
$page = $this->input_data['page'];
$size = $this->input_data['size'];
// content 每页数据,number 第一页,size 每页数量,totalElements 总数量,totalPages 总页数,lastPageNumber是否最后一页 true false
$user_customer = M('user_customer');
$user_customer_village = M('user_customer_village');
$building = M('building');
$floor = M('floor');
$house = M('house');
$community = M('community');
$village = M('village');
$loginService = new LoginService();
$village_id = $loginService->getVillageId();
$where = [];
if ($village_id) {
$customer_ids = $user_customer_village->where(['village_id'=>$village_id])->getField('customer_id',true);
$where['id'] = ['in',$customer_ids];
}
if ($name) {
$where['name'] = ['like','%'.$name.'%'];
}
if ($phone) {
$where['phone'] = ['like',$phone.'%'];
}
// /** content 内容
// age: 42 年龄
// auth: 1 是否实名
// buildingId: 9 user_customer_village 中
// buildingName: "2栋"
// communityId: 2 user_customer_village 中
// communityName: "马陵社区"
// danger: 0 是否危险人物
// floorId: 75 user_customer_village 中
// floorName: "4层"
// houseId: 429 user_customer_village 中
// houseName: "402户"
// id: 15351
// idcard: "19780201" 身份证号
// name: "周玲" 姓名
// old: 0 是否老人
// phone: "15751596521" 手机号
// sex: 1 性别
// villageId: 5 user_customer_village 中
// villageName: "华阳公寓"
// */
if (!isset($page) || $page < 1) {
$page = 1;
}
if (!isset($size) || $size < 1) {
$size = C('PAGE_NORMAL_COUNT');
}
// 获取总数
$totalElements = $user_customer->where($where)->count();
$totalPages = ceil($totalElements / $size);
$lastPageNumber = false;
if ($page >= $totalPages) {
$lastPageNumber = true;
}
if ($page > $totalPages) {
$return_data = ['content' => [], 'totalPages' => $totalPages,'number' => $page,'size' => $size , 'lastPageNumber' => $lastPageNumber];
$this->json->ok($return_data);
}
$data_list = $user_customer->where($where)
->order('id desc')
->limit((($page - 1) * $size) . ',' . $size)
->select();
if ($data_list) {
$responses = [];
foreach ($data_list as $k => &$v) {
$response['id'] = $v['id'];
$response['age'] = $v['age'];
$response['auth'] = $v['auth'];
$response['danger'] = $v['danger'];
$response['idcard'] = $v['idcard'];
$response['name'] = $v['name'];
$response['old'] = $v['old'];
$response['phone'] = $v['phone'];
$response['sex'] = $v['sex'];
// 获取小区信息
$user_customer_village_info = $user_customer_village->where(['customer_id'=>$v['id']])->find();
if ($user_customer_village_info) {
$response['buildingId'] = $user_customer_village_info['building_id'];
$response['buildingName'] = $building->where(['id'=>$user_customer_village_info['building_id']])->getField('descreption');
$response['floorId'] = $user_customer_village_info['floor_id'];
$response['floorName'] = $floor->where(['id'=>$user_customer_village_info['floor_id']])->getField('alias');
$response['houseId'] = $user_customer_village_info['house_id'];
$response['houseName'] = $house->where(['id'=>$user_customer_village_info['house_id']])->getField('description');
$response['communityId'] = $user_customer_village_info['community_id'];
$response['communityName'] = $community->where(['id'=>$user_customer_village_info['community_id']])->getField('name');
$response['villageId'] = $user_customer_village_info['village_id'];
$response['villageName'] = $village->where(['id'=>$user_customer_village_info['village_id']])->getField('name');
} else {
$response['buildingId'] = 0;
$response['buildingName'] = '';
$response['floorId'] = 0;
$response['floorName'] = '';
$response['houseId'] = 0;
$response['houseName'] = '';
$response['communityId'] = 0;
$response['communityName'] = '';
$response['villageId'] = 0;
$response['villageName'] = '';
}
$responses[] = $response;
}
unset($k, $v);
}
$return_data = ['content' => $responses, 'totalPages' => $totalPages,'number' => $page,'size' => $size , 'lastPageNumber' => $lastPageNumber];
$this->json->ok($return_data);
}
这是一个案例。