PHP实现的统计数据功能详解

2017-03-01 14:35:57 php
这篇文章主要介绍了PHP实现的统计数据功能,结合实例形式分析了php数据查询与显示处理的相关操作技巧,需要的朋友可以参考下

本文实例讲述了PHP实现的统计数据功能。分享给大家供大家参考,具体如下:

统计,就是把基本的数据,整合起来。

用到sql的,有group by 功能,count功能,order by功能等等。

sql将收集的数据,进行统计分析。

一般情况下,sql处理后得到的数据,还要通过php的逻辑来进行整理。

以一定的格式,展示到前台。

一般都是以数组的方式展示,这也是数据结构的概念。

{上线数,出单总数,核过总数,总人均,总核率,{(坐席人1,工号1,出单数1,发货数1,核单率1),(坐席人2,工号2,出单数2,发货数2,核单率2)}}

如果用php展示成上面的结构的话,就很好处理了。

首先通过sql获取初次处理的数据,

别小看这初次处理的数据,处理的好,会非常的便捷。

SELECT a.user,count(order_id) as subcount,b.passcount,c.full_name from vicidial_order a LEFT JOIN (SELECT user,count(order_id) as passcount from vicidial_order where time > UNIX_TIMESTAMP('2015-11-7') and user_group = 'TeamOne' and verifysta = 'Y' GROUP BY user ) b on a.user = b.user LEFT JOIN vicidial_users c on a.user = c.user where time > UNIX_TIMESTAMP('2015-11-7') and a.user_group = 'TeamOne' GROUP BY a.user ;

sql思路,归类订单表,以user来进行归类。

获取每个人,当天的订单提交总数count()。

还要获取每个人,订单通过审核的总数,通过where筛选。

然后关联查询其他相关数据。

 

有了这些基本数据,其他的相关数据都能出来了。

通过php来处理获取,其中变量命名要清晰,这样也有利于阅读代码。

$select_sql = "SELECT a.user,count(order_id) as subcount,b.passcount,c.full_name from vicidial_order a LEFT JOIN (SELECT user,count(order_id) as passcount from vicidial_order where time > UNIX_TIMESTAMP('".$today."') and user_group = '".$user_group."' and verifysta = 'Y' GROUP BY user ) b on a.user = b.user LEFT JOIN vicidial_users c on a.user = c.user where time > UNIX_TIMESTAMP('".$today."') and a.user_group = '".$user_group."' GROUP BY a.user ";
$rows = mysqli_query( $db_conn, $select_sql );
$row_counts_list = mysqli_num_rows( $rows );
if ( $row_counts_list != 0 )
{
  $i = 0;
  while($rs = mysqli_fetch_assoc( $rows )) // mysqli_fetch_assoc 获取键值数据   mysqli_fetch_field 获取一条数据 mysqli_fetch_fields 获取多组数据  mysqli_fetch_row
  {
    $outData['list'][$i]['user'] = $rs['user'];
    $outData['list'][$i]['full_name'] = $rs['full_name'];
    $outData['list'][$i]['subcount'] = $rs['subcount'];
    $outData['list'][$i]['passcount'] = $rs['passcount'];
    $outData['list'][$i]['passrate'] = round(($rs['passcount']/$rs['subcount'])*100)."%";
    $outData['all_subcount'] += $rs['subcount'];
    $outData['all_passcount'] += $rs['passcount'];
    $i++;
  }
  $outData['all_passrate'] = round(($outData['all_passcount']/$outData['all_subcount'])*100)."%";
  $outData['online_count'] = $row_counts_list;
  $outData['average_subcount'] = round($outData['all_subcount']/$outData['online_count'],1);
}

其中outData就是要输出的数据结构类型。

Array
(
  [list] => Array
    (
      [0] => Array
        (
          [user] => 8001
          [full_name] => 魏硕磊
          [subcount] => 3
          [passcount] => 2
          [passrate] => 67%
        )
      [1] => Array
        (
          [user] => 8004
          [full_name] => 刘庆
          [subcount] => 2
          [passcount] => 2
          [passrate] => 100%
        )
      [2] => Array
        (
          [user] => 8005
          [full_name] => 章厚英
          [subcount] => 4
          [passcount] => 3
          [passrate] => 75%
        )
    )
  [all_subcount] => 9
  [all_passcount] => 7
  [all_passrate] => 78%
  [online_count] => 3
  [average_subcount] => 3
)

获取数据后,一切都好办了。

套入页面就可以了,然后自己再调试调试。

<!-- begin -->
<?php foreach ($outData as $k => $v) { ?>
<div class="col-xs-12 col-sm-6 widget-container-col ui-sortable">
  <div class="widget-box widget-color-blue">
    <div class="widget-header">
      <h5 class="widget-title bigger lighter">
        <i class="ace-icon fa fa-table"></i>
        【<?php echo $v['group_name'];?>】成绩表
      </h5>
    </div>
    <div class="widget-body">
      <div class="widget-main no-padding">
        <table>
        </table>
        <table class="table table-striped table-bordered table-hover">
          <thead style="text-align:center;font-size:16px">
            <tr>
              <td colspan="2">上线总人数:</td>
              <td colspan="3"><?php echo $v['stat']['online_count']?></td>
            </tr>
            <tr>
              <td colspan="2">出单总数:</td>
              <td style="color:red"><?php echo $v['stat']['all_subcount']?></td>
              <td >核过总数</td>
              <td style="color:red"><?php echo $v['stat']['all_passcount']?></td>
            </tr>
            <tr>
              <td colspan="2">总人均:</td>
              <td style="color:red"><?php echo $v['stat']['average_subcount']?></td>
              <td >总核率</td>
              <td style="color:red"><?php echo $v['stat']['all_passrate']?></td>
            </tr>
          </thead>
          <thead class="thin-border-bottom">
            <tr>
              <th>
                <i class="ace-icon "></i>
                坐席人
              </th>
              <th>
                <i class="ace-icon "></i>
                工号
              </th>
              <th>
                <i class="ace-icon "></i>
                出单数
              </th>
              <th>
                <i class="ace-icon "></i>
                发货数
              </th>
              <th>
                <i class="ace-icon "></i>
                核单率
              </th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($v['stat']['list'] as $listk => $listv) { ?>
            <tr>
              <td class=""><?php echo $listv['full_name']?></td>
              <td>
                <a href="#"><?php echo $listv['user']?></a>
              </td>
              <td class="">
                <a href="#"><?php echo $listv['subcount']?></a>
              </td>
              <td class=""><?php echo $listv['passcount']?></td>
              <td class=""><?php echo $listv['passrate']?></td>
            </tr>
            <?php }?>
            <tr style="color:red;font-size:16px">
                <td class=""colspan="2">总计</td>
                <td class=""><?php echo $v['stat']['all_subcount']?></td>
                <td class=""><?php echo $v['stat']['all_passcount']?></td>
                <td class=""><?php echo $v['stat']['all_passrate']?></td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<?php }?>
<!-- end -->

希望本文所述对大家PHP程序设计有所帮助。