I need some help in extracting data from a tabel into Json. I need to query the data and return all the records for the current year that meets the WHERE statement and at the same time group the results by MONTH.
What I have as the query is:
$query_Promoter = "
SELECT COUNT(RecordID) AS Score4, FeedBackDate
FROM ".$FeedBack."
WHERE FeedBackDate >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
AND A = 4
OR B = 4
OR C = 4
OR D = 4
OR E = 4
OR F = 4
OR G = 4
OR H = 4
OR L = 4
OR M = 4
OR N = 4
GROUP BY MONTH(FeedBackDate)";
$Promoter =$conn->query($query_Promoter);
$totalRows_Promoter = mysqli_num_rows($Promoter);
I then loop through the result like:
if($totalRows_Promoter > 0) {
$rows_Promoter = array();
$rows_Promoter ['name'] = 'Promoters';
while($row_Promoter = mysqli_fetch_array($Promoter )) {
$rows_Promoter['Month'][] = date("M", strtotime($row_Promoter['FeedBackDate']));
$rows_Promoter['data'][] = $row_Promoter['Score4'];
}
}
$result = array();
if($totalRows_Promoter > 0) {
array_push($result,$rows_Promoter);
}
print json_encode($result, JSON_NUMERIC_CHECK);
// The resulting JSON is:
[{"name":"Promoters","Month":["Jan","Jan","Jan","Jan"],"data":[3,10,17,1]}]
I am trying to get the result as :
[{"name":"Promoters","Month":["Jan","Feb","May","Jun"],"data":[3,10,17,1]}]
Can anyone see what I am doing wrong or am I approaching this the wrong way.
Many thanks in advance for your time.