如何将每个月的数据放在各自的月度栏目中

时间:2017-06-24 08:44:34

标签: php date

我还在学习,请帮帮我

我有这个代码会显示每月的统计信息。如何将每个月的每个数据放在各自的列中?

这是我到目前为止所得到的。

$result= $myDB->query("SELECT * FROM ".$myDB->prefix("statistics")." WHERE year='2017'") or die(mysql_error()); 
while($row = $myDB->fetchArray($result))
{
$id=$row['id'];
$year=$row['year'];
$sum=$row['sum'];
$january=$row['january'];
$february=$row['february'];
$march=$row['march'];
$april=$row['april'];
$may=$row['may'];
$june=$row['june'];
$july=$row['july'];
$august=$row['august'];
$september=$row['september'];
$october=$row['october'];
$november=$row['november'];
$december=$row['december'];
}


echo "<table class='table table-striped table-bordered'><thead><tr>";

echo "<th class='text-center'>Year</th>";
for($m=1; $m<=12; ++$m){
    $monthName=date('M', mktime(0, 0, 0, $m, 1)).'<br>';
echo "<th class='text-center'>$monthName</th>";
}
echo "</tr></thead>";

echo "<tr>";
echo "<td class='text-center'>$year</td>";

for($m=1; $m<=12; ++$m){
echo "<td class='text-center'> data </td>";
}

echo "</tr>";

echo "</table>";

1 个答案:

答案 0 :(得分:1)

当你在月份名称中存储数据时,你可以使用$$来获取它:试试这个:

echo "<table class='table table-striped table-bordered'><thead><tr>";

echo "<th class='text-center'>Year</th>";
for($m=1; $m<=12; ++$m){
    $monthName=date('M', mktime(0, 0, 0, $m, 1));
echo "<th class='text-center'>$monthName</th>";
}
echo "</tr></thead>";

echo "<tr>";
echo "<td class='text-center'>$year</td>";

for($m=1; $m<=12; ++$m){
    $monthName=strtolower(date('F', mktime(0, 0, 0, $m, 1)));
echo "<td class='text-center'>".$$monthName." </td>";
}