【问题标题】:Display month names vertically instead of horizontally垂直显示月份名称而不是水平显示
【发布时间】:2011-12-22 17:48:13
【问题描述】:

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

我尝试了几种不同的方法,但都没有成功。我希望我可以在这里向您展示一些关于我尝试过的代码,但我真的没有什么可展示的。

有谁知道如何以 vetically 方式显示此表?

【问题讨论】:

    标签: php css mysql html-table


    【解决方案1】:

    你可以先从数据库中获取数据,然后以可访问的方式存储到数组中,比如:

    $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>';
    

    (未测试代码警告)

    完成后,您已经分离了从商店获取数据的部分和它的输出。这通常是可取的,因为您不会将输出逻辑的问题与获取数据混为一谈。这样就减少了问题。

    【讨论】:

      【解决方案2】:

      首先,您可能希望停止执行 12n 个查询并一次性获取所有信息并使用 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,而不是数字

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-18
        • 2020-08-26
        • 2017-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多