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世纪万年历
Dec 06 PHP
给apache2.2加上mod_encoding模块後 php5.2.0 处理url出现bug
Apr 12 PHP
php设计模式 Chain Of Responsibility (职责链模式)
Jun 26 PHP
PHP 透明水印生成代码
Aug 27 PHP
解决php接收shell返回的结果中文乱码问题
Jan 23 PHP
Linux下创建nginx脚本-start、stop、reload…
Aug 03 PHP
Laravel 4 初级教程之安装及入门
Oct 30 PHP
PHP图片处理之使用imagecopyresampled函数裁剪图片例子
Nov 19 PHP
php读取der格式证书乱码解决方法
Jun 22 PHP
php获取、检查类名、函数名、方法名的函数方法
Jun 25 PHP
浅谈ThinkPHP5.0版本和ThinkPHP3.2版本的区别
Jun 17 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编程最快明白》第三讲:php数组
2010/11/01 PHP
PHP学习笔记之三 数据库基本操作
2011/01/17 PHP
解析php时间戳与日期的转换
2013/06/06 PHP
thinkphp文件引用与分支结构用法实例
2014/11/26 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
thinkPHP学习笔记之安装配置篇
2015/03/05 PHP
Symfony2学习笔记之插件格式分析
2016/03/17 PHP
php中array_column函数简单实现方法
2016/07/11 PHP
PHP制作登录异常ip检测功能的实例代码
2016/11/16 PHP
微信公众号模板消息群发php代码示例
2016/12/29 PHP
谈谈impress.js初步理解
2015/09/09 Javascript
javascript设计模式--策略模式之输入验证
2015/11/27 Javascript
iscroll碰到Select无法选择下拉刷新的解决办法
2016/05/21 Javascript
js中Number数字数值运算后值不对的解决方法
2017/02/28 Javascript
解析Vue2 dist 目录下各个文件的区别
2017/11/22 Javascript
Vue使用vue-area-linkage实现地址三级联动效果的示例
2018/06/27 Javascript
vue2.0的虚拟DOM渲染思路分析
2018/08/09 Javascript
详解Vue改变数组中对象的属性不重新渲染View的解决方案
2018/09/21 Javascript
Vue表单绑定的实例代码(单选按钮,选择框(单选时,多选时,用 v-for 渲染的动态选项)
2019/05/13 Javascript
微信小程序开发实现的选项卡(窗口顶部/底部TabBar)页面切换功能图文详解
2019/05/14 Javascript
微信小程序仿抖音短视频切换效果的实例代码
2020/06/24 Javascript
[00:33]2016完美“圣”典风云人物:Sccc宣传片
2016/12/03 DOTA
python实现数通设备端口监控示例
2014/04/02 Python
解决sublime+python3无法输出中文的问题
2018/12/12 Python
使用IDLE的Python shell窗口实例详解
2019/11/19 Python
Spring @Enable模块驱动原理及使用实例
2020/06/23 Python
基于Python爬取fofa网页端数据过程解析
2020/07/13 Python
纯CSS和jQuery实现的在页面顶部显示的进度条效果2例(仿手机浏览器进度条效果)
2014/04/16 HTML / CSS
萨克斯第五大道英国:Saks Fifth Avenue英国
2019/04/01 全球购物
注塑工厂厂长岗位职责
2013/12/02 职场文书
大二学期个人自我评价
2014/01/13 职场文书
敬老院活动感想
2015/08/07 职场文书
2016年幼儿园教师政治学习心得体会
2016/01/23 职场文书
护士医德医风心得体会
2016/01/25 职场文书
详解MongoDB排序时内存大小限制与创建索引的注意事项
2022/05/06 MongoDB
python如何为list实现find方法
2022/05/30 Python