当前位置:Gxlcms > php框架 > thinkPHP统计排行与分页显示功能示例

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

时间:2021-07-01 10:21:17 帮助过:14人阅读

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

1.分页参数

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

2.分页对象

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

html

  1. <include file="Public:head" title="" />
  2. <style type="text/css">
  3. .top {
  4. font-size: 18px;
  5. border-bottom: #ddd 1px solid;
  6. margin-bottom: -1px;
  7. font-weight: bold;
  8. }
  9. .top .title {
  10. margin:10px;
  11. border:1px solid #EF6C00;
  12. display:-webkit-box;
  13. border-radius: 3px;
  14. }
  15. .top .title .title_child {
  16. width: 50%;
  17. line-height:40px;
  18. -webkit-box-flex:1;
  19. display:block;
  20. color:#EF6C00;
  21. text-decoration:none;
  22. }
  23. .top .title .title_child.active {
  24. color:#FFF;
  25. background:#EF6C00;
  26. }
  27. .page{
  28. margin-right: 10px;
  29. }
  30. .ranknum{
  31. font-weight: bold;
  32. color:#F92672;
  33. }
  34. #myrank{
  35. color: #FFF;
  36. font-weight:bold;
  37. background-color: #FBC853;
  38. }
  39. </style>
  40. <script type="text/javascript">
  41. </script>
  42. <body>
  43. <div class="top text-center">
  44. <div class="title">
  45. <a class="title_child <if condition='$type neq 1'>active</if>" href="{sh::U('User/ranklist', array('type' => 0))}">月排行</a>
  46. <a class="title_child <if condition='$type eq 1'>active</if>" href="{sh::U('User/ranklist', array('type' => 1))}">总排行</a>
  47. </div>
  48. </div>
  49. <div id="myrank" class="alert alert-danger text-center">
  50. 我的商户数:{sh:$my_user_count}    当前排名: {sh:$my_rank}
  51. </div>
  52. <div id="datalist">
  53. <table class="table table-hover">
  54. <thead>
  55. <tr>
  56. <th>  #</th>
  57. <th>姓名</th>
  58. <th>商户数</th>
  59. </tr>
  60. </thead>
  61. <tbody>
  62. <volist name="list" id="vo">
  63. <tr>
  64. <th scope="row" class="ranknum">
  65. <if condition="$vo.rank eq 1"><img src="{sh::RES}public/img/gold.png" style="width: 30px;">
  66. <elseif condition="$vo.rank eq 2"/><img src="{sh::RES}public/img/silver.png" style="width: 30px;">
  67. <elseif condition="$vo.rank eq 3"/><img src="{sh::RES}public/img/copper.png" style="width: 30px;">
  68. <else />
  69.   {sh:$vo.rank}
  70. </if>
  71. </th>
  72. <td>{sh:$vo.name}</td>
  73. <td>{sh:$vo.usercount}</td>
  74. </tr>
  75. </volist>
  76. </tbody>
  77. </table>
  78. <div class="page text-right">
  79. {sh:$page}
  80. </div>
  81. </div>
  82. </body>
  83. </html>

php

  1. // 排行榜
  2. public function ranklist(){
  3. $type = $this->_get('type','trim');
  4. $this->assign('type',$type);
  5. $opener_id = $this->opener_id;
  6. if($type == 0){ // 上月排行
  7. $arrLastMonth = $this->getLastMonthStartEndDay();
  8. $lastStartDay = $arrLastMonth['lastStartDay'];
  9. $lastEndDay = $arrLastMonth['lastEndDay'].' 23:59:59';
  10. $b_time = strtotime($lastStartDay);
  11. $e_time = strtotime($lastEndDay);
  12. $where['b.addtime'] = array(array('gt',$b_time),array('lt',$e_time),'and');
  13. }
  14. $where['a.status'] = array('eq','1');
  15. M()->query('SET @rank =0;');
  16. $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);
  17. $all = M()->table(''.$subQuery.' a')->getField('a.id,a.usercount,a.name,(@rank:=IFNULL(@rank,0)+1) as rank');
  18. $count = count($all);
  19. $Page = new Page($count, 10);
  20. $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();
  21. foreach ($list as $k => $v) {
  22. $list[$k]['rank'] = $k + 1 + $Page->firstRow;
  23. }
  24. // 我的商户
  25. $my_user_count = $all[$opener_id]['usercount']?$all[$opener_id]['usercount']:0;
  26. $my_rank = $all[$opener_id]['rank']?$all[$opener_id]['rank']:'-';
  27. $this->assign('my_user_count',$my_user_count);
  28. $this->assign('my_rank',$my_rank);
  29. $this->assign('page',$Page->show());
  30. $this->assign('list', $list);
  31. $this->display();
  32. }
  33. // 获取上一月开始与结束日期
  34. private function getLastMonthStartEndDay(){
  35. $thismonth = date('m');
  36. $thisyear = date('Y');
  37. if ($thismonth == 1) {
  38. $lastmonth = 12;
  39. $lastyear = $thisyear - 1;
  40. } else {
  41. $lastmonth = $thismonth - 1;
  42. $lastyear = $thisyear;
  43. }
  44. $lastStartDay = $lastyear . '-' . $lastmonth . '-1';
  45. $lastEndDay = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay)); //t 给定月份所应有的天数,28到31
  46. return array('lastStartDay'=>$lastStartDay,'lastEndDay'=>$lastEndDay);
  47. }

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

案例效果

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》、《smarty模板入门基础教程》及《PHP模板技术总结》。

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

人气教程排行