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程序--记数器
Oct 09 PHP
PHP 地址栏信息的获取代码
Jan 07 PHP
PHP SESSION的增加、删除、修改、查看操作
Mar 20 PHP
优化WordPress中文章与评论的时间显示
Jan 12 PHP
Yii CGridView用法实例详解
Jul 12 PHP
PHP XML和数组互相转换详解
Oct 26 PHP
PHP微信模板消息操作示例
Jun 29 PHP
Laravel框架使用Redis的方法详解
May 30 PHP
PHP中单例模式的使用场景与使用方法讲解
Mar 18 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
Feb 21 PHP
tp5框架前台无限极导航菜单类实现方法分析
Mar 29 PHP
PHP手机号码及邮箱正则表达式实例解析
Jul 11 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读取二进制流(C语言结构体struct数据文件)的深入解析
2013/06/13 PHP
基于PHP实现短信验证码接口(容联运通讯)
2016/09/06 PHP
又一个小巧的图片预加载类
2007/05/05 Javascript
js如何取消事件冒泡
2013/09/23 Javascript
前端开发过程中浏览器版本的两种判定方法
2013/10/30 Javascript
js导入导出excel(实例代码)
2013/11/25 Javascript
javascript将数字转换整数金额大写的方法
2015/01/27 Javascript
全面解析Bootstrap表单使用方法(表单样式)
2015/11/24 Javascript
七个不允许错过的jQuery小技巧
2015/12/21 Javascript
逻辑表达式中与或非的用法详解
2016/06/06 Javascript
使用jquery获取url及url参数的简单实例
2016/06/14 Javascript
vuejs动态组件给子组件传递数据的方法详解
2016/09/09 Javascript
Javascript农历与公历相互转换的简单实例
2016/10/09 Javascript
jQuery EasyUI右键菜单实现关闭标签/选项卡
2016/10/10 Javascript
js 定位到某个锚点的方法
2016/11/19 Javascript
jQuery Password Validation密码验证
2016/12/30 Javascript
分享十三个最佳JavaScript数据网格库
2017/04/07 Javascript
利用vscode编写vue的简单配置详解
2017/06/17 Javascript
Angular实现较为复杂的表格过滤,删除功能示例
2017/12/23 Javascript
p5.js入门教程之图片加载
2018/03/20 Javascript
去掉vue 中的代码规范检测两种方法(Eslint验证)
2018/03/21 Javascript
利用Python绘制数据的瀑布图的教程
2015/04/07 Python
Python 自动刷博客浏览量实例代码
2017/06/14 Python
Diango + uwsgi + nginx项目部署的全过程(可外网访问)
2018/04/22 Python
python实现txt文件格式转换为arff格式
2018/05/31 Python
Python制作词云图代码实例
2019/09/09 Python
解决Pycharm的项目目录突然消失的问题
2020/01/20 Python
Python找出列表中出现次数最多的元素三种方式
2020/02/24 Python
Python脚本破解压缩文件口令实例教程(zipfile)
2020/06/14 Python
matplotlib subplot绘制多个子图的方法示例
2020/07/28 Python
老公给老婆的道歉信
2014/01/10 职场文书
简短大学毕业感言
2014/01/18 职场文书
团队经理竞聘书
2014/03/31 职场文书
学校创先争优活动总结
2014/08/28 职场文书
给领导敬酒词
2015/08/12 职场文书
python实现自动化群控的步骤
2021/04/11 Python