ThinkPHP5 分页后追加数据方法
因为分页后数是对象而不是数组,所以不能用foreach方式,而是采用each遍历处理。另外包闭函数传参了解一下!
<?php
function index()
{
//查询运营商数据
$operator = Db::name("Operator")->where("status", "EQ", 1)->select();
//查询会员
$list = Db::name("Member")->field("id,name,nick_name");
$select['name'] = $name = trim(input("name", ''));
if (!empty($name)) {
$list = $list->where("name", "EQ", $name);
}
$select['mobile'] = $mobile = trim(input('mobile', ''));
if (!empty($mobile)) {
$list = $list->where("mobile", "EQ", $mobile);
}
$list = $list->order('id DESC')
->paginate(2, false, ['query' => Request::instance()->param()])->each(function ($item, $key) use ($operator) {
$member_id = $item['id'];
$item['rowspan'] = count($operator);
$item['order_number'] = Db::name("Order")->where("member_id", "EQ", $member_id)->count();
//统计每个会员拥有产品数量
foreach ($operator as $k => $v) {
$operator_id = $v['id'];
$row[] = [
'operator' => $v['name'],
'card_number' => self::number_count("MemberCard", $member_id, $operator_id),
'package_number' => self::number_count("MemberPackage", $member_id, $operator_id),
'traffic_number' => self::number_count("MemberTraffic", $member_id, $operator_id),
'pool_number' => self::number_count("MemberPool", $member_id, $operator_id),
'backward_number' => self::number_count("MemberBackward", $member_id, $operator_id),
];
}
$item['child'] = $row;
return $item;
});
$this->assign([
'list' => $list,
'select' => $select
]);
return $this->fetch();
}
?>