Php 垂直显示月份名称,而不是水平显示
我有一个表,它在一个表中显示月份(一月到十二月),如下所示。Php 垂直显示月份名称,而不是水平显示,php,css,mysql,html-table,Php,Css,Mysql,Html Table,我有一个表,它在一个表中显示月份(一月到十二月),如下所示。此表从数据库获取值,它不是固定的 <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&
此表从数据库获取值,它不是固定的
<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
...
...
我尝试了几种不同的方法,但没有成功。我希望我能在这里给你展示一些我尝试过的代码,但我真的没有什么东西要展示
有没有人知道如何以可视方式显示此表 您可以首先从数据库获取数据,并以可访问的方式将其存储到数组中,如:
$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>';
$cols=array('2011','2012','2013');
$rows=爆炸(“,”,“一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月,总计”);
回声“,”;
foreach($cols作为$col)
{
回显“”,$col“”;
}
回声';
foreach($行作为$行)
{
回声','',$行';
foreach($cols作为$col)
{
回显“”,$array[$col][$row],“”;
}
回声';
}
回声';
(未测试代码警告)
完成后,您将从存储中获取数据的部分与数据的输出分离。这通常是可取的,因为您不会将输出逻辑的问题与获取数据混为一谈。这就减少了问题。首先,您可能希望停止执行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
$tableRows = array(
0 => array( // 0 will be for sum from each year
2011 => 0,
2012 => 0
),
1 => array( // January
2011 => 0
...
);
因此,您将在每一行中获得每个月的总和,如果M
列为空,则为每年的总和,并在最后一行中获得整个结果的总和(如果不需要,您可以放弃它)
现在,您必须创建其他数组,以便在向表中添加行时更容易迭代。我建议它看起来像这样:
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
$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>
月
需要更正的是在第一列中显示月份名称和总计
,而不是数字