2011-11-05 72 views
0

我有一張表格,如下所示在表格中顯示月份(1月至12月)。
該表從數據庫中獲取值,但不是固定的。垂直顯示月份名稱而不是水平

<table width="100%" style="font-size:9px"> 
<thead> 
<tr> 
<th colspan="14" style="text-align:center">Year: <?PHP echo $year?></th> 
</tr> 
<tr > 
<th>Jan</th> 
<th>Feb</th> 
<th>Mar</th> 
<th>Apr</th> 
<th>May</th> 
<th>Jun</th> 
<th>Jul</th> 
<th>Aug</th> 
<th>Sep</th> 
<th>Oct</th> 
<th>Nov</th> 
<th>Dec</th> 
<th>Total</th> 
</tr></thead><tbody> 
<tr > 
<?PHP 
for ($i=1; $i<=12; $i++) { 
    $yearstart = $year."-".$i."-01"; 
    $yearend = $year."-".$i."-31"; 
    $sql="SELECT SUM(total) as totalsales_$i from salesorder where user_id=7 and created BETWEEN '$yearstart' AND '$yearend' "; 
    $res = $db->sql_query($sql); 
    $row = $db->sql_fetchrow($res); 
    $total = $total + $row['totalsales_'.$i]; 
?> 
<td style="width: 96px; text-align:right"> 
<?PHP if ($row['totalsales_'.$i] !='') 
    echo "$".number_format($row['totalsales_'.$i],2); 
    else  
    echo "<font color=#c2c2c2>$0.00</font>";?> 
</td> 
<?PHP //for ?> 
<td style="text-align:right"><?PHP echo "$".number_format($total,2) ;?></td> 
</tr>          
</tbody></table> 

OUTPUT: 
    Jan feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Total 
    $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0 
    $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0 
    $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0  $0 
    ... 
    ... 

但我需要的是如何垂直顯示錶,如下

   2011 2012 2013 ... 

    Jan   $0.00 $0.00 $0.00 $0.00 
    feb   $0.00 $0.00 $0.00 $0.00 
    Mar   $0.00 $0.00 $0.00 $0.00 
    Apr   $0.00 $0.00 $0.00 $0.00 
    May   $0.00 $0.00 $0.00 $0.00 
    Jun   $0.00 $0.00 $0.00 $0.00 
    Jul   $0.00 $0.00 $0.00 $0.00 
    Aug   $0.00 $0.00 $0.00 $0.00 
    Sep   $0.00 $0.00 $0.00 $0.00 
    Oct   $0.00 $0.00 $0.00 $0.00 
    Nov   $0.00 $0.00 $0.00 $0.00 
    Dec   $0.00 $0.00 $0.00 $0.00 
    Total  $0.00 $0.00 $0.00 $0.00 

我嘗試了幾種不同的方式,但沒有運氣。我希望我可以在這裏向你展示一些關於我所嘗試的代碼,但我真的沒有表現出來。

有沒有人有任何想法如何顯示這張表vefully?

回答

0

首先,你可能要停止執行12個ñ查詢並獲取所有信息以一氣呵成和聚集機制,權力在MySQL:

SELECT 
    YEAR(created) as `Y`, 
    MONTH(created) AS `M`, 
    SUM(total) AS `SUM` 
FROM 
    salesorder 
WHERE 
    user_id = 7 
GROUP BY 
    YEAR(created), 
    MONTH(created) 
WITH ROLLUP 

這會導致這樣的結果集:

Y | M | SUM 
2011 | 10 | 20.00 
2011 | 11 | 10.00 
2011 | 12 | 5.00 
2011 | NULL | 35.00  // M == NULL -> sum for this year 
2012 | 1 | 7.00 
2012 | 2 | 7.00 
2012 | NULL | 14.00 
NULL | NULL | 49.00  // M & Y == NULL -> sum for whole resultset 

所以,你將有個別月份和每一行中,每年總和如果M欄爲空,並總和(可以丟棄它,如果你不需要它)整個結果在最後一排

現在,您必須創建其他數組,以便在向表中添加行時更容易進行迭代。我會建議它看起來像這樣:

$tableRows = array(
    0 => array(// 0 will be for sum from each year 
      2011 => 0, 
      2012 => 0 
    ), 
    1 => array(// January 
      2011 => 0 
    ... 
); 

我不知道您使用與數據庫進行交互什麼課,所以讓我假設有一些方法,允許從ResultSet中獲取關聯數組

$years = array(); 
while($dbRow = $res->fetchAssoc()){ 
    $y = intval($dbRow['Y']); 
    if($y){ 
     $m = intval($dbRow['M']); 
     $tableRow[$m][$y] = $dbRow['SUM']; 
    }  
} 
$allYears = array_keys($tableRows[0]); // get all years we have 

最後,你可以在你的表目前它:

<table> 
    <thead> 
    <tr> 
     <th>Month</th> 
     <?php foreach($allYears as $year): ?> 
     <th><?=$year?></th> 
     <?php endforeach; ?> 
    </tr> 
    <thead> 
    <tfoot> 
    <? foreach($tableRows as $month => $years): ?> 
    <tr> 
     <td><?=$month?></td> 
    <?php foreach($allYears as $y): ?> 
     <td><?=$years[$y]?></td> 
    <?php endforeach; 
    if(!$month): ?> 
    </tr> 
    </tfoot> 
    <tbody> 
    <?php else: ?> 
    </tr> 
    <?php endif; 
    endforeach; ?> 
    </tbody> 
</table> 

需要糾正的是,以顯示月份名稱的Total不是數字的冷杉t列

+0

謝謝dev-null-dweller。它工作完美。 – librium

2

您可以首先從數據庫中的數據並將其存儲在可訪問的方式到一個數組,如:

$array[$year][$monthOrTotal] = $value; 

然後你可以遍歷數組在每一個你喜歡的方向射出你的表:

$cols = array('2011', '2012', '2013'); 
$rows = explode(',', 'Jan,feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,Total'); 

echo '<table>', '<tr>', '<th></th>'; 
foreach($cols as $col) 
{ 
    echo '<th>', $col, '</th>'; 
} 
echo '</tr>'; 

foreach($rows as $row) 
{ 
    echo '<tr>', '<th>', $row, '</th>'; 
    foreach($cols as $col) 
    { 
     echo '<td>', $array[$col][$row], '</td>'; 
    } 
    echo '</tr>'; 
} 
echo '</table>'; 

(未測試的代碼警告)

這樣做,你已經分居了,你獲得你的店和它的輸出數據的部分。這通常是可取的,因爲您不會將問題與輸出邏輯混合在一起來獲取數據。這樣可以減少問題。

+0

謝謝你我會試試看。 – librium

相關問題