thinkPHP统计排行与分页显示功能示例


Posted in PHP onDecember 02, 2016

本文实例分析了thinkPHP统计排行与分页显示功能。分享给大家供大家参考,具体如下:

1.分页参数

count 总数
firstRow 起始行
listRows 每一次获取记录数
list 每一页的记录(要与count对应一致就行)

2.分页对象

可以针对真实的数据表
也可以针对统计出来的数据表,或者说是虚拟的表
因为LIMIT是最后执行的,哪怕你进行group操作,哪怕你进行子查询

html

<include file="Public:head" title="" />
<style type="text/css">
.top {
  font-size: 18px;
  border-bottom: #ddd 1px solid;
  margin-bottom: -1px;
  font-weight: bold;
}
.top .title {
  margin:10px;
  border:1px solid #EF6C00;
  display:-webkit-box;
  border-radius: 3px;
}
.top .title .title_child {
  width: 50%;
  line-height:40px;
  -webkit-box-flex:1;
  display:block;
  color:#EF6C00;
  text-decoration:none;
}
.top .title .title_child.active {
  color:#FFF;
  background:#EF6C00;
}
.page{
  margin-right: 10px;
}
.ranknum{
  font-weight: bold;
  color:#F92672;
}
#myrank{
  color: #FFF;
  font-weight:bold;
  background-color: #FBC853;
}
</style>
<script type="text/javascript">
</script>
<body>
<div class="top text-center">
  <div class="title">
    <a class="title_child <if condition='$type neq 1'>active</if>" href="{sh::U('User/ranklist', array('type' => 0))}">月排行</a>
    <a class="title_child <if condition='$type eq 1'>active</if>" href="{sh::U('User/ranklist', array('type' => 1))}">总排行</a>
  </div>
</div>
<div id="myrank" class="alert alert-danger text-center">
  我的商户数:{sh:$my_user_count}    当前排名: {sh:$my_rank}
</div>
<div id="datalist">
<table class="table table-hover">
   <thead>
    <tr>
     <th>  #</th>
     <th>姓名</th>
     <th>商户数</th>
    </tr>
   </thead>
   <tbody>
     <volist name="list" id="vo">
    <tr>
     <th scope="row" class="ranknum">
     <if condition="$vo.rank eq 1"><img src="{sh::RES}public/img/gold.png" style="width: 30px;">
     <elseif condition="$vo.rank eq 2"/><img src="{sh::RES}public/img/silver.png" style="width: 30px;">
     <elseif condition="$vo.rank eq 3"/><img src="{sh::RES}public/img/copper.png" style="width: 30px;">
     <else />
       {sh:$vo.rank}
     </if>
     </th>
     <td>{sh:$vo.name}</td>
     <td>{sh:$vo.usercount}</td>
    </tr>
    </volist>
   </tbody>
</table>
<div class="page text-right">
    {sh:$page}
</div>
</div>
</body>
</html>

php

// 排行榜
public function ranklist(){
    $type = $this->_get('type','trim');
    $this->assign('type',$type);
    $opener_id = $this->opener_id;
    if($type == 0){ // 上月排行
      $arrLastMonth = $this->getLastMonthStartEndDay();
      $lastStartDay = $arrLastMonth['lastStartDay'];
      $lastEndDay = $arrLastMonth['lastEndDay'].' 23:59:59'; 
      $b_time = strtotime($lastStartDay);
      $e_time = strtotime($lastEndDay);
      $where['b.addtime'] = array(array('gt',$b_time),array('lt',$e_time),'and'); 
    }
    $where['a.status'] = array('eq','1');
    M()->query('SET @rank =0;');
    $subQuery = M()->table('sh_opener a')->join('sh_user b on a.id = b.opener_id')->where($where)->group('a.id')->order('usercount desc')->field('a.id,count(b.id) as usercount,a.name')->select(false);
    $all = M()->table(''.$subQuery.' a')->getField('a.id,a.usercount,a.name,(@rank:=IFNULL(@rank,0)+1) as rank');
    $count   = count($all);
    $Page    = new Page($count, 10);
    $list    = M()->table('sh_opener a')->join('sh_user b on a.id = b.opener_id')->where($where)->group('a.id')->order('usercount desc')->limit($Page->firstRow.','.$Page->listRows)->field('count(b.id) as usercount,a.name,a.id')->select();
    foreach ($list as $k => $v) {
      $list[$k]['rank'] = $k + 1 + $Page->firstRow;
    }
    // 我的商户
    $my_user_count = $all[$opener_id]['usercount']?$all[$opener_id]['usercount']:0;
    $my_rank = $all[$opener_id]['rank']?$all[$opener_id]['rank']:'-';
    $this->assign('my_user_count',$my_user_count);
    $this->assign('my_rank',$my_rank);
    $this->assign('page',$Page->show());
    $this->assign('list', $list);
    $this->display();
}
// 获取上一月开始与结束日期
private function getLastMonthStartEndDay(){
    $thismonth = date('m');
    $thisyear = date('Y');
    if ($thismonth == 1) {
      $lastmonth = 12;
      $lastyear = $thisyear - 1;
    } else {
      $lastmonth = $thismonth - 1;
      $lastyear = $thisyear;
    }
    $lastStartDay = $lastyear . '-' . $lastmonth . '-1';
    $lastEndDay  = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay)); //t 给定月份所应有的天数,28到31
    return array('lastStartDay'=>$lastStartDay,'lastEndDay'=>$lastEndDay);
}

这里用的是thinkphp的分页类实现的。

案例效果

thinkPHP统计排行与分页显示功能示例

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

PHP 相关文章推荐
PHP5在Apache下的两种模式的安装
Sep 05 PHP
WIN98下Apache1.3.14+PHP4.0.4的安装
Oct 09 PHP
php获得文件扩展名三法
Nov 25 PHP
PHP开发入门教程之面向对象
Dec 05 PHP
隐藏X-Space个人空间下方版权方法隐藏X-Space个人空间标题隐藏X-Space个人空间管理版权方法
Feb 22 PHP
php公用函数列表[正则]
Feb 22 PHP
CI框架自动加载session出现报错的解决办法
Jun 17 PHP
php的dl函数用法实例
Nov 06 PHP
一个PHP实现的轻量级简单爬虫
Jul 08 PHP
php similar_text()函数的定义和用法
May 12 PHP
php求今天、昨天、明天时间戳的简单实现方法
Jul 28 PHP
php进行md5加密简单实例方法
Sep 19 PHP
thinkPHP交易详情查询功能详解
Dec 02 #PHP
php变量与数组相互转换的方法(extract与compact)
Dec 02 #PHP
php图像处理函数imagecopyresampled用法详解
Dec 02 #PHP
PHP面向对象继承用法详解(优化与减少代码重复)
Dec 02 #PHP
PHP面向对象程序设计高级特性详解(接口,继承,抽象类,析构,克隆等)
Dec 02 #PHP
PHP面向对象程序设计之命名空间与自动加载类详解
Dec 02 #PHP
PHP面向对象程序设计之类与反射API详解
Dec 02 #PHP
You might like
php&amp;java(二)
2006/10/09 PHP
PHP写杨辉三角实例代码
2011/07/17 PHP
php获取服务器信息的实现代码
2013/02/04 PHP
php微信公众号开发之微信企业付款给个人
2018/10/04 PHP
PHP+jQuery实现即点即改功能示例
2019/02/21 PHP
基于jquery的监控数据是否发生改变
2011/04/11 Javascript
JavaScript用JQuery呼叫Server端方法示例代码
2014/09/03 Javascript
jquery幻灯片插件bxslider样式改进实例
2014/10/15 Javascript
JavaScript通过Date-Mask将日期转换成字符串的方法
2015/06/04 Javascript
BootStrap实现响应式布局导航栏折叠隐藏效果(在小屏幕、手机屏幕浏览时自动折叠隐藏)
2016/11/30 Javascript
JavaScript 动态三角函数实例详解
2017/01/08 Javascript
jQuery点击头像上传并预览图片
2017/02/23 Javascript
node.js中EJS 模板快速入门教程
2017/05/08 Javascript
基于Vue实现拖拽功能
2020/07/29 Javascript
JS实现的RC4加密算法示例
2018/08/16 Javascript
详解解决Vue相同路由参数不同不会刷新的问题
2018/10/12 Javascript
Vue组件系列开发之模态框
2019/04/18 Javascript
node.js中 mysql 增删改查操作及async,await处理实例分析
2020/02/11 Javascript
Django使用Celery异步任务队列的使用
2018/03/13 Python
在python中,使用scatter绘制散点图的实例
2019/07/03 Python
python监控nginx端口和进程状态
2019/09/06 Python
python初步实现word2vec操作
2020/06/09 Python
python爬虫用mongodb的理由
2020/07/28 Python
美国女性奢华品牌精品店:INTERMIX
2017/10/12 全球购物
伦敦新晋轻奢耳饰潮牌:Tada & Toy
2020/05/25 全球购物
高中生的学习总结自我鉴定
2013/10/26 职场文书
高一地理教学反思
2014/01/18 职场文书
公开承诺书格式
2014/05/21 职场文书
应届生面试求职信
2014/07/02 职场文书
加强作风建设心得体会
2014/10/22 职场文书
领导班子整改方案
2014/10/25 职场文书
2015年社区综治宣传月活动总结
2015/03/25 职场文书
2015年教师个人业务工作总结
2015/10/23 职场文书
vue-router中hash模式与history模式的区别
2021/06/23 Vue.js
Python实战之OpenCV实现猫脸检测
2021/06/26 Python
使用CSS实现一个搜索引擎的原理解析
2021/09/25 HTML / CSS