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 相关文章推荐
模拟xcopy的函数
Oct 09 PHP
一个简单的MySQL数据浏览器
Oct 09 PHP
判断是否为指定长度内字符串的php函数
Feb 16 PHP
用php的ob_start来生成静态页面的方法分析
Mar 09 PHP
PHP中怎样保持SESSION不过期 原理及方案介绍
Aug 08 PHP
php查找字符串出现次数的方法
Dec 01 PHP
PHP脚本监控Nginx 502错误并自动重启php-fpm
May 13 PHP
PHP下载远程图片并保存到本地方法总结
Jan 22 PHP
PHP二维数组排序简单实现方法
Feb 14 PHP
thinkPHP实现多字段模糊匹配查询的方法
Dec 01 PHP
浅谈PHP错误类型及屏蔽方法
May 27 PHP
php中文乱码问题的终极解决方案汇总
Aug 01 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+mysql实现无限级分类 | 树型显示分类关系
2006/11/19 PHP
php 防止单引号,双引号在接受页面转义
2008/07/10 PHP
php 中英文语言转换类代码
2011/08/11 PHP
Discuz7.2版的faq.php SQL注入漏洞分析
2014/08/06 PHP
PHP析构函数destruct与垃圾回收机制的讲解
2019/03/22 PHP
php array_chunk()函数用法与注意事项
2019/07/12 PHP
Jquery实现的一种常用高亮效果示例代码
2014/01/28 Javascript
什么是Node.js?Node.js详细介绍
2014/06/01 Javascript
轻松创建nodejs服务器(5):事件处理程序
2014/12/18 NodeJs
JQuery的ON()方法支持的所有事件罗列
2015/02/28 Javascript
以JavaScript来实现WordPress中的二级导航菜单的方法
2015/12/14 Javascript
学习javascript面向对象 实例讲解面向对象选项卡
2016/01/04 Javascript
滚动条的监听与内容随着滚动条动态加载的实现
2017/02/08 Javascript
AngularJS路由Ui-router模块用法示例
2017/05/29 Javascript
jquery动态赋值id与动态取id方法示例
2017/08/21 jQuery
玩转vue的slot内容分发
2018/09/22 Javascript
js实现简单扫雷
2020/11/27 Javascript
Openlayers实现测量功能
2020/09/25 Javascript
微信小程序实现点击导航标签滚动定位到对应位置
2020/11/19 Javascript
mapboxgl实现带箭头轨迹线的代码
2021/01/04 Javascript
python3模拟百度登录并实现百度贴吧签到示例分享(百度贴吧自动签到)
2014/02/24 Python
python入门之语句(if语句、while语句、for语句)
2015/01/19 Python
Python实现信用卡系统(支持购物、转账、存取钱)
2016/06/24 Python
python中Matplotlib实现绘制3D图的示例代码
2017/09/04 Python
使用apidocJs快速生成在线文档的实例讲解
2018/02/07 Python
python基于http下载视频或音频
2018/06/20 Python
如何优雅地改进Django中的模板碎片缓存详解
2018/07/04 Python
在PYQT5中QscrollArea(滚动条)的使用方法
2019/06/14 Python
python实现统计代码行数的小工具
2019/09/19 Python
Python批量将图片灰度化的实现代码
2020/04/11 Python
HelloFresh奥地利:立即订购烹饪盒
2019/02/22 全球购物
纯净、自信、100%的羊绒服装:360Cashmere
2021/02/20 全球购物
27个经典Linux面试题及答案,你知道几个?
2013/01/10 面试题
工作态度检讨书
2014/02/11 职场文书
安全协议书
2014/04/23 职场文书
公务员考察材料
2014/12/23 职场文书