thinkphp6 paginate() 分页数据处理后输出

来源:https://blog.csdn.net/baidu_29994443/article/details/101070031

替换处理

<?php
//model里面的分页函数
public function pageQuery(){    
    $rs = $this->where($where)->field(true)->order('id desc')->paginate()
    ->each(function($item, $key){
        if($item['status']==1){
            $item['name1'] = $item['name2'];
        }
        $item['imgSize'] = round($item['imgSize']/1024/1024,2);
        return $item;
    });
    return $rs;
}
?>

如果each里面涉及到别的表,则代码如下:

<?php
//model里面的分页函数
public function pageQuery(){    
    $rs = $this->where($where)->field(true)->order('id desc')->paginate()   
    ->each(function($item, $key){
        $urs = Db::name('u')->where('isShow',1)->select();
        foreach ($urs as $rkey=>$rv){
            if($item['userScore']>=$rv['startScore'] && $item['userScore']<$rv['endScore']){
               $item['userRank'] = $rv['rankName'];
            }
        }
        return $item;
    });
    return $rs;
}
?>

如果each里面涉及到外部参数,则代码如下:

<?php
//model里面的分页函数
public function pageQuery(){
//从别的表获得参数值,一次获取,each中可以重复使用
$urs = Db::name('u')->where('isShow',1)->select(); 
$rs = $this->where($where)->field(true)->order('id desc')->paginate() 
->each(function($item, $key) use ($urs){ 
//使用外部传来的参数$urs
foreach ($urs as $rkey=>$rv){
if($item['userScore']>=$rv['startScore'] && $item['userScore']<$rv['endScore']){
  $item['userRank'] = $rv['rankName'];
}
}
return $item;
});
return $rs;
}
?>

 

posted @ 2020-10-08 10:46  1O(∩_∩)O1  阅读(4681)  评论(0编辑  收藏  举报