這篇文章主要介紹了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思路,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) { ?> <p class="col-xs-12 col-sm-6 widget-container-col ui-sortable"> <p class="widget-box widget-color-blue"> <p class="widget-header"> <h5 class="widget-title bigger lighter"> <i class="ace-icon fa fa-table"></i> 【<?php echo $v['group_name'];?>】成绩表 </h5> </p> <p class="widget-body"> <p 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> </p> </p> </p> </p> <?php }?> <!-- end -->
相關推薦:
#
以上是PHP實作的統計資料功能詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!