我每个月都在计算游戏中项目分布的统计数据。
我有一张这样的表:
Date itemname quantity avg. price
Mar-2012 x 10 3.45
Mar-2012 y 12 4.66
Apr-2012 x 23 4.56
Apr-2012 y 9 4.6
如何在PHP中构建一个数组并构建一个表,显示过去6个月的以下报告?
Mar-2012 Apr-2012
quantity avg_price quantity avg_price
x 10 3.45 23 4.56
y 12 4.66 9 4.6
我无法创建此类报告。有什么帮助吗?
由于
找到解决方案;我想知道PHP中是否有一种简单的方法可以做到这一点。我明白我必须应用一些逻辑。解决方案遵循(kohana 2.x代码):
控制器:
$db = Database::instance();
$months = $db -> query( "select distinct from_unixtime( timestamp, '%m-%Y') month
from stats_items
where timestamp >= ( unix_timestamp() - ( 6 * 30 * 24 * 3600 ) ) order by timestamp asc" ) -> as_array();
$res = ORM::factory('stats_item') -> find_all();
foreach ( $res as $r )
{
$stats[$r->name][date('M-Y', $r -> timestamp)]['total'] = $r -> total;
$db = Database::instance();
$months = $db -> query( "select distinct from_unixtime( timestamp, '%m-%Y') month
from stats_items where timestamp >= ( unix_timestamp() - ( 6 * 30 * 24 * 3600 ) ) order by timestamp asc" ) -> as_array();
$res = ORM::factory('stats_item') -> find_all();
foreach ( $res as $r )
{
$stats[$r->name][date('M-Y', $r -> timestamp)]['total'] = $r -> total;
$stats[$r->name][date('M-Y', $r -> timestamp)]['avg_price'] = $r -> avg_price;
}
$view->months = $months;
$view->stats_items = $stats;stats[$r->name][date('M-Y', $r -> timestamp)]['avg_price'] = $r -> avg_price;
}
$view->months = $months;
$view->stats_items = $stats;
查看:
<table>
<?php
echo "<tr><td width='20%'></td>";
foreach ( $months as $month )
echo "<td colspan='2' style='text-align:center' width='20%'>" . $month -> month . '</td>' ;
echo '</tr>';
echo '<tr>';
echo '<td>Item</td>';
foreach ( $months as $month )
echo "<td>Total</td><td>Avg Price</td>";
echo '</tr>';
$i=0;
foreach ( $stats_items as $key => $value )
{
$class = ( $i % 2 == 0 ) ? 'alternaterow' : '' ;
echo "<tr class='$class'>";
echo '<td>'. kohana::lang( $key) . '</td>' ;
foreach ( $value as $key2 => $value2 )
{
echo "<td class='tright'>".$value2['total'].'</td>';
echo "<td class='tright'>".$value2['avg_price'].'</td>';
}
echo '</tr>';
$i++;
}
?>
答案 0 :(得分:0)
就个人而言,我会创建一个类Item
并将每个项目及其所有适当的数据投射到其中,然后制作对象方法来处理所有计算。
然后根据收集的数据制作HTML格式的数据表。
答案 1 :(得分:0)
$result = preg_match_all('/(.*?)[ ]+(.*?)[ ]+(.*?)[ ]+(.*)/',$subject,PREG_SET_ORDER);
$grouped = array();
foreach($result as $res){
$grouped[$res[1]][] = arrary($res[2],$res[3],$res[4]);
}
$width = 18;
foreach($grouped as $key => $group){
$len = strlen($key);
echo str_repeat(' ',floor(($width- $len)/2)) . $key . str_repeat(' ',ceil(($width- $len)/2)) ;
}
foreach($grouped as $key => $group){
echo 'quantity avg_price ';
}
for($i =0;$i < 100;$i++){
foreach($grouped as $key => $group){
echo $group[$i][0] . $group[$i][1] . $group[$i][2] ;
}
}
像这样(未经测试)