Hi how will I loop this Json being created in PHP MYSQL so that I wont need to declare individual queries just to produce the output I need.
I would like to create a JSON without the need of individually declaring variables and queries just to produce a single array
<?php
include("connection.php");
$sth = mysqli_query($connect,"SELECT month as month FROM data group by month ORDER BY month DESC");
$rows = array();
$rows['name'] = 'Month';
while($r = mysqli_fetch_array($sth)) {
$rows['data'][] = $r['month'];
}
$sth = mysqli_query($connect,"SELECT sum(cf) as cf FROM data Where branch='NYC' group by month");
$rows1 = array();
$rows1['name'] = 'NYC';
while($r = mysqli_fetch_assoc($sth)) {
$rows1['data'][] = $r['cf'];
}
$sth = mysqli_query($connect,"SELECT sum(cf) as cf FROM data Where branch='LA' group by month");
$rows2 = array();
$rows2['name'] = 'LA';
while($rr = mysqli_fetch_assoc($sth) ) {
$rows2['data'][] = $rr['cf'];
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
array_push($result,$rows2);
echo(json_encode($result, JSON_NUMERIC_CHECK)) ;
?>
Here is the output:
[
{
"name": "Month",
"data": [
"JANUARY",
"FEBRUARY"
]
},
{
"name": "NYC",
"data": [
189000,
252000
]
},
{
"name": "LA",
"data": [
3330504,
4440672
]
}
]