垂直显示月份名称而不是水平显示

时间:2011-11-05 17:28:30

标签: php css mysql html-table

我有一个表格,在表格中显示月份(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

我尝试了几种不同的方式,但没有运气。我希望我能在这里向你展示一些关于我尝试过的内容的代码,但我真的没有表现出来。

有没有人知道如何在视觉上显示这张表?

2 个答案:

答案 0 :(得分: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>';

(未经测试的代码警告)

这样就完成了将从商店获取数据的部分与其输出分开。这通常是可取的,因为您不会将输出逻辑的问题与获取数据混在一起。这减少了问题。

答案 1 :(得分:0)

首先,您可能希望停止执行12个 n 查询,并在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
    ...
);

我不知道你用什么类与数据库进行交互,所以让我假设有一些方法可以从结果集中获取关联数组

$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而不是第一列中的数字