How to get mysql data into a google chart using php loop?

社会主义新天地 提交于 2019-12-29 08:26:07

问题


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 amend that data to the chart because of its syntax.

This is as far as I have gone. I need to get the mysql values in place of:

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:


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);


来源:https://stackoverflow.com/questions/11219282/how-to-get-mysql-data-into-a-google-chart-using-php-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!