I\'ve created a Google chart and I\'ve added data taken from the database itself. When getting data through php loops though, I have had some difficulty because I couldn\'t
Try to tackle one problem after the other. First, get the data you need from the database. Then, create the data structure you need in PHP, and convert it to JavaScript using json_encode()
. Finally, pass it to the visualization function in JavaScript:
<?php
// query data
$result = mysql_query(...);
// format data structure
$data = array();
$i = 0;
while($row = mysql_fetch_array($result)) {
$i += 1;
array_push($data, array($i) + $row);
}
// convert to JavaScript
?>
var raw_data = <?php echo json_encode($data) ?>;
// visualize
var data = google.visualization.arrayToDataTable(raw_data);