本文实例讲述了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来处理获取,其中变量命名要清晰,这样也有利于阅读代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
$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就是要输出的数据结构类型。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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
)
|
获取数据后,一切都好办了。
套入页面就可以了,然后自己再调试调试。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
<!-- 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程序设计有所帮助。