这篇文章主要介绍了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('-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('-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
)
获取数据后,一切都好办了。
套入页面就可以了,然后自己再调试调试。
<?php foreach ($outData as $k => $v) { ?>
【<?php echo $v['group_name'];?>】成绩表
上线总人数:<?php echo $v['stat']['online_count']?>出单总数:<?php echo $v['stat']['all_subcount']?>核过总数<?php echo $v['stat']['all_passcount']?>总人均:<?php echo $v['stat']['average_subcount']?>总核率<?php echo $v['stat']['all_passrate']?>
坐席人
工号
出单数
发货数
核单率
<?php foreach ($v['stat']['list'] as $listk => $listv) { ?>
<?php echo $listv['full_name']?>
<?php echo $listv['user']?>
<?php echo $listv['subcount']?>
<?php echo $listv['passcount']?><?php echo $listv['passrate']?>总计<?php echo $v['stat']['all_subcount']?><?php echo $v['stat']['all_passcount']?><?php echo $v['stat']['all_passrate']?>
相关推荐:
基于PHP实现的网站目录扫描索引工具实例
PHP实现的自定义数组排序函数与排序类的方法
PHP实现的文件操作类及文件下载功能