zoukankan      html  css  js  c++  java
  • 排行榜的实现

    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();
    }
    
  • 相关阅读:
    Matlab中使用Java api画图图形并保存为jpeg格式
    基于注解的Mybatis mapper 接口注意事项
    Chap 2 Representing and Manipulating Information (CS:APP)
    设计模式之享元模式
    辛星浅析Linux中的postfix
    ZOJ 1364 Machine Schedule(二分图最大匹配)
    Scrapy系列教程(3)------Spider(爬虫核心,定义链接关系和网页信息抽取)
    恩布企业 IM iOS端 1.1 公布, 开源手机 IM
    leetcode_Power of Two_easy
    小强的HTML5移动开发之路(45)——汇率计算器【1】
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10189161.html
Copyright © 2011-2022 走看看