[英]Passing json data from PHP to javascript - Google Pie Chart
I'm trying to pass a json data from php after a mysql query to my javascript that loads the google charts api. 我正在尝试将mysql查询后的json数据从php传递到我的加载Google图表API的JavaScript。 Query is successful however the data format doesn't seem to be correct. 查询成功,但是数据格式似乎不正确。 Any idea on what i'm doing wrong here? 对我在这里做错的任何想法吗? I saved it as .html file and just opening it via browser. 我将其另存为.html文件,只是通过浏览器将其打开。 Hope that should it. 希望应该。
< ?php
$DB_hostname = "localhost";
$DB_Name = "root";
$DB_pass = "root";
$tbl_name="tblname"; // Table name
$con = mysql_connect($DB_Hostname,$DB_Name,$DB_pass) or die(mysql_error());
mysql_select_db("dbname", $con);
$sql="SELECT * FROM $tbl_name WHERE is86 = 1";
$result=mysql_query($sql);
$count1 = mysql_num_rows($result);
$sql="SELECT * FROM $tbl_name WHERE is86 = 0";
$result=mysql_query($sql);
$count2 = mysql_num_rows($result);
$data[0] = array("Changes","count1");
$data[1] = array("Apps with IA Architecture",.$count1.);
$data[2] = array("Apps with no IA Architecture",.$count2.);
$data = json_encode($data);
? >
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/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 data = new google.visualization.DataTable(<?=$data?>);
var options = {
title: 'Architecture Changes',
is3D: 'true',
width: 800,
height: 600
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
Your data format is incorrect for using the DataTable constructor like that, but you should be able to use the arrayToDataTable
method: 您的数据格式不适合使用像这样的DataTable构造函数,但是您应该能够使用arrayToDataTable
方法:
var data = google.visualization.arrayToDataTable(<?=$data?>);
Also, you shouldn't include the .
另外,您不应包含.
's before and after the $count
variables when adding them to the arrays: 将它们添加到数组时,在$count
变量之前和之后:
$data[0] = array("Changes", "count1");
$data[1] = array("Apps with IA Architecture", $count1);
$data[2] = array("Apps with no IA Architecture", $count2);
引号?
var data = new google.visualization.DataTable('<?=$data?>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.