Reputation: 654
I'm trying to get info from my database into a Google chart. To do so I need to format a JSON file according to Google's specifications (found here https://developers.google.com/chart/interactive/docs/reference#dataparam )
It needs to be formated as the following example:
{
cols: [
{label: 'NEW A', type: 'string'},
{label: 'B-label', type: 'number'},
{label: 'C-label', type: 'date'}
],
rows: [
{c:[
{v: 'a'},
{v: 1.0, f: 'One'},
{v: new Date(2008, 1, 28, 0, 31, 26), f: '2/28/08 12:31 AM'}
]},
{c:[
{v: 'b'},
{v: 2.0, f: 'Two'},
{v: new Date(2008, 2, 30, 0, 31, 26), f: '3/30/08 12:31 AM'}
]},
{c:[
{v: 'c'},
{v: 3.0, f: 'Three'},
{v: new Date(2008, 3, 30, 0, 31, 26), f: '4/30/08 12:31 AM'}
]}
]
}
However when I try to achieve this, I fail to get the JSON exactly the same. My PHP code:
$result = mysqli_query($dbConnection, $sql);
$json['cols'][] = array("label" => "year-week", "type" => "string");
$json['cols'][] = array("label" => "# non-bundle games given", "type" => "number");
$json['cols'][] = array("label" => "Users joined", "type" => "number");
$json['cols'][] = array("label" => "Users left", "type" => "number");
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$json['rows'][]['c'] = array_values($row);
}
}
// CLOSE CONNECTION
mysqli_close($dbConnection);
echo json_encode($json);
This gives me the following:
{
"cols": [
{
"label": "year-week",
"type": "string"
},
{
"label": "# non-bundle games given",
"type": "number"
},
{
"label": "Users joined",
"type": "number"
},
{
"label": "Users left",
"type": "number"
}
],
"rows": [
{
"c":
[
"201431",
"3",
"45",
"1"
]
},
{
"c":
[
"201432",
"3",
"0",
"1"
]
}
]
}
As you can see the rows part is not right. I tried several ways to build the PHP code, but I'm just guessing now. Any advice on how to write the PHP code to output according to Google's specifications?
Upvotes: 0
Views: 543
Reputation: 11088
In Json:
[]
is an json array
{}
is a json object
If some indices or values don't have quotes, it is not json
it is a javascript object
.
So you cannot use json_encode
, you must manually output the string representation of the object.
Furthermore you are just returning the array values as a single array, but it needs to be split up using the key as an index.
foreach($row as $key => $value)
{
echo "$key: \'$value\'";
}
Upvotes: 1
Reputation: 503
why not using json_encode() ? i think it will make a proper json from array!
http://php.net/manual/en/function.json-encode.php
Upvotes: 1
Reputation: 307
Use PHP to create a list say any array of the values you'll pass to the graph. The JSON passed to the API for Plotting doesn't min you having whitespaces in your code so loop through the results then implode in the end and have all that returned in a method.. Casein point
function prepMyGraphValues(){
//Initialize here
$myData = [];
//Do SQL stuff here
$sql = ...
//Have a counter
$count = 0;
//Loop through results
while($data=$sql->fetch_assoc()){
//Loop Thru the results adding to$myData
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$myData[] = "{c:[
{v: $count.0, f: 'One'},
{v: new Date($data[prep_date]), f: '$data[prep_date2]'}
]}";
}
}
}
return implode(',',$myData);
}
It's Not tidy but can work for you
Upvotes: 0