本文實例講述了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相關內容感興趣的讀者可查看本站專題:《php+mysql數據庫操作入門教程》、《PHP基於pdo操作數據庫技巧總結》、《PHP+MongoDB數據庫操作技巧大全》、《php+Oracle數據庫程序設計技巧總結》、《php+mssql數據庫程序設計技巧總結》、《php+redis數據庫程序設計技巧總結》、《php+mysqli數據庫程序設計技巧總結》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。