Reputation: 275
I just want to know how can I add the data type here? Google Charts requires data type when I use this code:
Im getting an error from google charts that it datatype needs to be defined...please help. Thanks!
while($r = mysql_fetch_assoc($query)) {
$google_JSON = "{cols: [";
$column = array_keys($r);
foreach($column as $key=>$value){
$google_JSON_cols[]="{id: '".$key."', label: '".$value."'}";
}
$google_JSON .= implode(",",$google_JSON_cols)."],rows: [";
$google_JSON_rows[] = "{c:[{v: '".$r['id']."'}, {v: ".$r['count']."}]}";
}
// you may need to change the above into a function that loops through rows, with $r['id'] etc, referring to the fields you want to inject..
//pass it into google charts data
echo $google_JSON.implode(",",$google_JSON_rows)."]}";
Upvotes: 0
Views: 585
Reputation: 5992
I haven't done any PHP in ages so I'm not going to offer to provide any patches to your code but the data type goes in the array elements of the cols hash reference i.e., where you create {id:"key", label: "value"} you need to add 'type: "number"' or 'type: "string"' etc. How you determine whether it is a number of a string by referring back to your query is not going to be that simple. You could look at the column type via whatever metadata the mysql module provides for the column but most charts require number types for the series and string/number types for the x-axis values. You could always set the first column to string or number and the other columns to number but it would depend on the sort of chart you are creating.
BTW, I'm surprised you cannot create the structure natively in PHP and then convert to JSON rather than building the JSON string up like this - you can in Perl quite easily.
Upvotes: 1