我找不出正確的方式將我的MySQLi數據從正確的json回顯移動到Google圖表代碼。MySQLi json編碼的數據解析爲谷歌圖表
我查詢數據並在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 Chart中, 但我不想在那裏硬編碼數據,我想將回顯的數據發送到Google Chart的Javascript。
<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還沒有得到答案,如果任何人都可以幫助我回答其中任何一個,我會非常感激。謝謝。
謝謝你的工作,但因爲我有幾個查詢從我的數據庫開放運行現在圖表工作,但任何數據庫代碼請求數據下面的腳本不再起作用。我必須弄清楚發生了什麼事。在上面的查詢中,我在頭部的Google Chart Javascript中可以讀取$ jsonTable。 – davejt