I'm looking for a loop that retrieves the data from the MySQL result set with an unknown amount of columns and rows and puts it in GViz format. With the following code I can read the data dynamically, but I can't get it into the format that Google Visualization API desires:
$cols = array();
$row = array();
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
if (!$meta) {
echo "No information available<br />\n";
}
$cols[]= "{id: '".$i."', label: '".$meta->name."', type: '".$meta->type."'}";
$i++;
}
while($r = mysql_fetch_assoc($result)) {
$row[]= $r;
}
$jsonTable = json_encode($cols);
$jsonTable2 = json_encode($row);
$Table3 = "{cols: {$jsonTable}, rows: $jsonTable2}";
echo $jsonTable;
echo '<br>';
echo $jsonTable2;
echo '<br>';
echo $Table3;
echo '<br>';
The JS error I get from the browser in debug-mode is:
Error: Invalid type, {[id= '0', label= 'mon', type= 'string']}, for column "0".
I have looked at the data parameter on googles documentation, but their data is hard coded as always. And this SO-page does not generate the cols and rows dynamically.
I'm glad for any help on getting the data parameter right. Thanks!