排行榜的实现

422101-20181228104432764-990394410.png

500名之后,排名就没意义了。

// 获取排行榜
public function getMyRankingList() {
    $uid = input('post.uid/d');
    if (!$uid) {
        $this->json->setErr('10001',lang('10001'));
        $this->json->Send();
    }

    $user = new UserModel();
    $user_info = $user->where('id',$uid)->field('id as uid,avatarurl,nickname,continue_anwser_times,usable_integral')->find();
    if (!$user_info) {
        $this->json->setErr('20001',lang('20001'));
        $this->json->Send();
    }

    $out_data = [];
    // 获取前500个排名靠前的用户,如果不在其中,那么你的排名就是 '-' 横杠
    $user_info['rank'] = '-';
    $top_500 = $user->where('status',UserModel::USER_STATUS_PASS)->order('usable_integral desc,id asc')->limit(500)->column('id');
    $offset = array_search($uid,$top_500);
    if ($offset !== FALSE) { // 注意了 ,小心排第一被过滤掉
        $user_info['rank'] = $offset+1;
    }

    $out_data = $user_info;

    $this->json->setAttr('data',$out_data);
    $this->json->Send();
}

/**
 * 分页
 */
public function getRankingList() {
    $page = input('post.page/d',1);

    $per_page = input('post.per_page/d',config('normal_per_page'));
    // 获取商品数据
    $user = new UserModel();
    $where['status'] = UserModel::USER_STATUS_PASS;
    // 获取分页数据
    $count = $user->where($where)->count();
    $total_page = ceil($count / $per_page);

    if ($page > $total_page) {
        $this->json->setErr(0,lang('tips_no_more'));
        $return_data = ['data_list' => [],'total_page' => $total_page, 'current_page'=>$page];
        $this->json->setAttr('data',$return_data);
        $this->json->Send();
    }

    $user_list = $user->where($where)
        ->order('usable_integral desc,id asc')
        ->limit((($page - 1) * $per_page) . "," .$per_page)
        ->field('id as uid,avatarurl,nickname,continue_anwser_times,usable_integral')
        ->select();

    foreach ($user_list as $k=>&$v) {
        $v['rank'] = (($page - 1) * $per_page) + ($k + 1);
    }


    $return_data = ['data_list' => $user_list,'total_page' => $total_page, 'current_page'=>$page];


    $this->json->setErr(0, '获取成功');
    $this->json->setAttr('data',$return_data);
    $this->json->Send();
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值