How to get mysql data in google chart using php loop?

I created a Google chart, and I added data taken from the database itself. However, getting data through php loops I had some difficulties because I couldn’t change this data to a diagram because of its syntax.

This is how far I left. I need to get mysql values ​​instead:

1170, 460, 45, 123, 456], 660, 1120, 145, 785, 658]

var data = google.visualization.arrayToDataTable([ ['Term', <?php while ($getchartrows = mysql_fetch_array($getchart)) { echo " ' " . $getchartrows['std_ID'] . " ' " . ","; } ?>], <?php $one = mysql_query("SELECT * FROM stusitting WHERE std_ID = '0001' AND subjectNo = '$subin' AND grade = '$gradein' AND termNo = '$tcheck' "); $getone = mysql_fetch_array($one); ?> ['01', <?php echo $getone ['marks']; ?>, 400, 495, 565, 415], ['02', 1170, 460, 45, 123, 456], ['03', 660, 1120, 145, 785, 658] ]); var options = { title: 'Class Progress' }; 
+1
source share
1 answer

Try to solve one problem after another. First get the data from the database. Next, create the data structure you need in PHP and convert it to JavaScript using json_encode() . Finally, pass it to the rendering 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); 
+5
source

Source: https://habr.com/ru/post/1497550/


All Articles