I want to use Highcharts (bar) with my database, but I am struggling how to bring the JSON using my select.
I have three levels: Gold, Silver, Platinum and I need to get the minimum year and maximum year and cover the gaps if that level is not present in some year
I have this select:
select B.year, A.level, COUNT(B.id_level) as n_level from level AS A
ier join company_level AS B ON A.id_level = B.id_level
GROUP BY B.id_level, B.year
ORDER BY B.year
My result:
year level n_level
2010 Siver 1
2010 Platinum 2
2010 Gold 2
2011 Gold 1
2013 Gold 1
2014 Platinum 1
2015 Silver 1
In my result I don't have Silver and Platinum for 2011 and I would like to put the Silver and Platinum in the list but with number_level (n_level) with 0. My PHP:
$rows = mysqli_query($this->co, $query);
$bln = array();
$bln['name'] = 'Year';
$row['name'] = 'Level';
$year ="";
while ($r = mysqli_fetch_array($rows)) {
if($year !== $r['year']){
$bln['data'][] = $r['year'];
}
$row['data'][] = $r['n_level'];
$year = $r['year'];
}
$rslt = array();
array_push($rslt, $bln);
array_push($rslt, $row);
print json_encode($rslt, JSON_NUMERIC_CHECK);
My PHP Result:
[{"name":"Year","data":[2010,2011,2013,2014,2015]},{"name":"Level","data":[2,2,1,1,1,1,1]}]
But my goal is:
[{"name":"Year","data":[2010,2011,2013,2014,2015]},{"name":"Gold","data":[2,2,1,1,1]}, {"name":"Silver","data":[1,0,0,0,1]}, {"name":"Platinum","data":[2,2,1,1,1]}]
Thank you for any help.
