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 相关文章推荐
用PHP和ACCESS写聊天室(一)
Oct 09 PHP
PHP教程 变量定义
Oct 23 PHP
PHP STRING 陷阱原理说明
Jul 24 PHP
file_get_contents获取不到网页内容的解决方法
Mar 07 PHP
如何取得中文字符串中出现次数最多的子串
Aug 08 PHP
CI框架开发新浪微博登录接口源码完整版
May 28 PHP
Codeigniter出现错误提示Error with CACHE directory的解决方案
Jun 12 PHP
PHP执行Curl时报错提示CURL ERROR: Recv failure: Connection reset by peer的解决方法
Jun 26 PHP
PHP遍历目录函数opendir()、readdir()、closedir()、rewinddir()总结
Nov 18 PHP
php实现微信发红包
Dec 05 PHP
php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
Dec 21 PHP
PHP使用finfo_file()函数检测上传图片类型的实现方法
Apr 18 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
模仿OSO的论坛(一)
2006/10/09 PHP
在PHP里得到前天和昨天的日期的代码
2007/08/16 PHP
PHP实现递归复制整个文件夹的类实例
2015/08/03 PHP
php 数组随机取值的简单实例
2016/05/23 PHP
Laravel中使用FormRequest进行表单验证方法及问题汇总
2016/06/19 PHP
基于JQuery实现相同内容合并单元格的代码
2011/01/12 Javascript
IE6下focus与blur错乱的解决方案
2011/07/31 Javascript
jquery实现简单的拖拽效果实例兼容所有主流浏览器
2013/06/21 Javascript
js/jquery去掉空格,回车,换行示例代码
2013/11/05 Javascript
js如何获取兄弟、父类等节点
2014/01/06 Javascript
JavaScript异步回调的Promise模式封装实例
2014/06/07 Javascript
js数组去重的方法汇总
2015/07/29 Javascript
jQuery实现文本框邮箱输入自动补全效果
2015/11/17 Javascript
jQuery插件扩展测试实例
2016/06/21 Javascript
解决node.js安装包失败的几种方法
2016/09/02 Javascript
jQuery dateRangePicker插件使用方法详解
2017/07/28 jQuery
用js屏蔽被http劫持的浮动广告实现方法
2017/08/10 Javascript
vue项目中v-model父子组件通信的实现详解
2017/12/10 Javascript
Postman如何实现参数化执行及断言处理
2020/07/28 Javascript
[01:50]2014DOTA2西雅图邀请赛 专访欢乐周宝龙
2014/07/08 DOTA
[03:39]2015国际邀请赛主赛事首日精彩回顾
2015/08/05 DOTA
Python进程间通信之共享内存详解
2017/10/30 Python
Django实现简单分页功能的方法详解
2017/12/05 Python
Python 存储字符串时节省空间的方法
2019/04/23 Python
Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)
2020/01/03 Python
详解Python3 中的字符串格式化语法
2020/01/15 Python
对Tensorflow中Device实例的生成和管理详解
2020/02/04 Python
CSS3常用的几种颜色渐变模式总结
2016/11/18 HTML / CSS
英国第一的滑雪服装和装备零售商:Snow+Rock
2020/02/01 全球购物
体育专业个人的求职信范文
2013/09/21 职场文书
土木工程个人自荐信范文
2013/11/30 职场文书
体操比赛口号
2014/06/10 职场文书
社区学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
幼儿园重阳节活动总结
2015/05/05 职场文书
法人身份证明书
2015/06/18 职场文书
新学期新寄语,献给新生们!
2019/11/15 职场文书