I'm making a chart that lists a certain amount per month, but you can have more that has nothing, so it does not return any value and does not list it on the chart.
I do not know if I should change the query or programming, can anyone help me?
My query looks like this:
SELECT DATE_FORMAT(dt_validade, '%y-%m') as anoMes, count(*) as qtde
FROM tabela
WHERE dt_validade > '{$today}'
GROUP BY anoMes
ORDER BY anoMes
Her return is more or less that
anoMes | qtde 14-08 | 2 14-09 | 5 14-12 | 10 14-12 | 10 14-12 | 10 15-01 | 1 15-03 | 1
And with that, in php I wrap a loop in the result, I play the data for an array that sends a json_encode to the data for the chart, but with that it will only show me in the data graph for months 08, 09 and 12 , but I need to also show the months 10 and 11 with zero values.
And in short, what I did in programming was this:
$cols = array(
array(
'Período',
'Quantidade'
)
);
$rows = array();
$query = "A query passada";
$result = $query->result();
foreach ($result as $r) {
array_push($rows, array(
$r->mesAno,
(int) $r->qtde
));
}
$dados = array_merge($cols, $rows);
return json_encode($dados);
And as a suggestion given to me, the expected result is this:
anoMes | qtde
14-08 | 2
14-09 | 5
14-10 | 0
14-11 | 0
14-12 | 10
15-01 | 1
15-03 | 1
Link to the build in SQLFIDDLE , so I guess I can get a little clearer.