MySQLi json将解析后的数据编码到Google Chart中

时间:2013-08-23 16:24:43

标签: php javascript json mysqli

我无法弄清楚将MySQLi数据从正确的json echo移动到Google Chart代码的正确方法。

我查询数据并在json中回显其结果。

$query = mysqli_query($con,"SELECT Count(e.song_id) 
AS song_count, c.show_id, e.show_id, DATE_FORMAT(c.show_date, '%Y') AS s_year
FROM tbl_song_shows e, tbl_shows c
WHERE e.song_id='{$sid}' AND c.show_id = e.show_id
GROUP BY s_year
ORDER BY s_year DESC");


//SHOW STATS TO GRAPH//

$table = array();
$table['cols'] = array(
    /* define your DataTable columns here
     * each column gets its own array
     * syntax of the arrays is:
     * label => column label
     * type => data type of column (string, number, date, datetime, boolean)
     */
    // I assumed your first column is a "string" type
    // and your second column is a "number" type
    // but you can change them if they are not
    array('id' => 'Year','label' => 's_year', 'type' => 'string'),
    array('id' => 'value','label' => 'song_count', 'type' => 'number')

);

$rows = array();
while($r = mysqli_fetch_array($query, MYSQL_ASSOC)){
$temp = array();
    // each column needs to have data inserted via the $temp array
    $temp[] = array('v' =>  $r['s_year']);    
$temp[] = array('v' =>  (int) $r['song_count']);

    // insert the temp array into $rows
    $rows[] = array('c' => $temp);
}

// populate the table with rows of data
$table['rows'] = $rows;

// encode the table as JSON
$jsonTable = json_encode($table);

// set up header; first two prevent IE from caching queries
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');

// return the JSON data
echo $jsonTable;

返回: { “COLS”:[{ “ID”: “年”, “标签”: “s_year”, “类型”: “串”},{ “ID”: “值”, “标签”: “song_count”,”类型 “:” 号码 “}],” 行 “:[{” C “:[{” v “:” 2013 “},{” v “:5}]},{” C “:[{” v” : “2012”},{ “v”:1}]},{ “C”:[{ “v”: “2005”},{ “v”:1}]},{ “C”:[{” v “:” 2003 “},{” v“:1}]}]}

如果我只是将其粘贴到Google图表中,那么图表工作正常。 但我不想硬编码那里的数据,我想将回显的数据发送到Javascript for Google Chart。

  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">

    // Load the Visualization API and the piechart package.
    google.load('visualization', '1', {'packages':['corechart']});

    // Set a callback to run when the Google Visualization API is loaded.
    google.setOnLoadCallback(drawChart);

    function drawChart() {




var jsonData = {"cols":[{"id":"Year","label":"s_year","type":"string"},{"id":"value","label":"song_count","type":"number"}],"rows":[{"c":[{"v":"2013"},{"v":5}]},{"c":[{"v":"2012"},{"v":1}]},{"c":[{"v":"2005"},{"v":1}]},{"c":[{"v":"2003"},{"v":1}]}]}



      // Create our data table out of JSON data loaded from server.
     var data = new google.visualization.DataTable(jsonData);


      var options = {
           title: 'Yearly Song Counts',
         chartArea:{left:40, width:850, height:250},
    legend: {position: 'none'},
colors:['#94B599'],
hAxis: {gridlines:{count:6}}
        };
      // Instantiate and draw our chart, passing in some options.
      // Do not forget to check your div ID
      var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
      chart.draw(data, options);
    }
    </script>

我不应该做类似的事情:

var jsonData = $jsonTable   

任何人都可以帮我解决这个问题吗?

我还有一个上一个问题:MySQL Find previous date that a song was played没有得到回答,如果有人能帮我回答其中任何一个,我会非常感激。谢谢。

2 个答案:

答案 0 :(得分:1)

你很亲密。您只需要echo PHP上下文中的变量(<?php ... ?>

var jsonData = <?php echo $jsonTable;  ?>

答案 1 :(得分:0)

如果您上面的<script>块是.php脚本的一部分,那么您可以让PHP将其输出嵌入JS代码中,例如

<script>
    var jsonData = <?php echo json_encode($table); ?>;
    ...
</script>