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

后端 未结 1 1848
长情又很酷
长情又很酷 2020-12-20 07:15

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

相关标签:
1条回答
  • 2020-12-20 07:40

    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);
    
    0 讨论(0)
提交回复
热议问题