我正在尝试从查询中动态构建一个表
这是一个查询
SELECT MONTH(c.pat_added_date) AS 月份,COUNT(a.ana_id) 作为总计,YEAR(c.pat_added_date) AS year FROM lab_patient_analysis AS b
LEFT JOIN lab_analysis AS a ON a.ana_id = b.ana_id LEFT JOIN lab_patients AS c ON c.pat_id = b.pat_id WHERE a.ana_id = '3' AND YEAR(c.pat_added_date) BETWEEN '2012' AND '2013' GROUP BY MONTH(c.pat_added_date) ORDER BY YEAR(c.pat_added_date), MONTH(c.pat_added_date)
这是查询返回的内容
month | total | year
1 13 2012
7 9 2012
8 33 2012
3 21 2013
6 8 2013
....
这是我用于数组的 php 函数
function array_set(&$a, $path, $value) {
if(!is_array($path))
$path = explode($path[0], substr($path, 1));
$key = array_pop($path);
foreach($path as $k) {
if(!isset($a[$k]))
$a[$k] = array();
$a = &$a[$k];
}
$a[$key ? $key : count($a)] = $value;
}
以及我被卡住的地方
$yr = null;
while($row ... ))
{
if($yr != $row['year']) {
$yr = $row['year'];
array_set($array, array($row['year'], $row['month']), $row['total']);
echo '</tr></tr>';
echo '<th scope="row">'.$row['year'].'</th>';
}
echo '<td>'.$row['total'];
}
返回的数组
Array
(
[2012] => Array
(
[1] => 13
[7] => 9
[8] => 33
)
[2013] => Array
(
[3] => 21
[6] => 8
)
)
我想制作下表
Jan| Feb| Mar | Apr | May | Jun | Jul | Sep | Oct | Nov | Dec
2012 13 0 0 0 0 0 9 33 0 0 0
2013 0 0 21 0 0 8 0 0 0 0 0
希望这是有道理的