DeTechn Blog

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[&#39;name&#39;]&nbsp;=&nbsp;$name = trim(input("name", ''));
    if (!empty($name)) {
        $list&nbsp;=&nbsp;$list->where("name", "EQ", $name);
    }
    $select[&#39;mobile&#39;]&nbsp;=&nbsp;$mobile = trim(input('mobile', ''));
    if (!empty($mobile)) {
        $list&nbsp;=&nbsp;$list->where("mobile", "EQ", $mobile);
    }
    $list&nbsp;=&nbsp;$list->order('id DESC')
        ->paginate(2, false, ['query' => Request::instance()->param()])->each(function ($item,&nbsp;$key) use ($operator) {
            $member_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;$item['id'];
            $item[&#39;rowspan&#39;]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&nbsp;count($operator);
            $item[&#39;order_number&#39;]&nbsp;=&nbsp;Db::name(&quot;Order&quot;)-&gt;where(&quot;member_id&quot;,&nbsp;&quot;EQ&quot;,&nbsp;$member_id)->count();
            //统计每个会员拥有产品数量
            foreach ($operator&nbsp;as&nbsp;$k => $v) {
                $operator_id&nbsp;=&nbsp;$v['id'];
                $row[]       = [
                    'operator'        => $v['name'],
                    'card_number'     => self::number_count("MemberCard", $member_id,&nbsp;$operator_id),
                    'package_number'  => self::number_count("MemberPackage", $member_id,&nbsp;$operator_id),
                    'traffic_number'  => self::number_count("MemberTraffic", $member_id,&nbsp;$operator_id),
                    'pool_number'     => self::number_count("MemberPool", $member_id,&nbsp;$operator_id),
                    'backward_number' => self::number_count("MemberBackward", $member_id,&nbsp;$operator_id),
                ];
            }
            $item[&#39;child&#39;]&nbsp;=&nbsp;$row;
            return $item;
        });
    $this->assign([
        'list'   => $list,
        'select' => $select
    ]);
    return $this->fetch();
}
?>

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »