无法提取数据以获取谷歌图表。没有显示任何内容,但getdata.php显示json编码的数据 [英] Unable to extract data to get google chart. Nothing is being displayed but getdata.php shows json encoded data
问题描述
这是我的2个文件。
//getdata.php:提取数据并使用json_encode转换为json格式。这是编码数据的正确方法吗?我不改变列名。这是必要的吗?
Here are my 2 files. //getdata.php: extracts data and converts to json format using json_encode. Is it the right way of encoding the data? I am not changing the column names. Is that necessary?
<?php
mysql_connect('localhost','akshita','123456');
mysql_select_db('rcusers');
$sqlquery1="select userid,group_name,req_nodes,actualPE from jobs where userid='zhang' limit 200";
$sqlresult1=mysql_query($sqlquery1);
$rows=array();
while($r=mysql_fetch_assoc($sqlresult1)){
$rows[]=$r;
}
print json_encode($rows);
?>
// chartDraw.php:使用api函数来打印它。没有显示任何内容。
//chartDraw.php: uses api functions to print it. Nothing is being displayed.
<html>
<head>
<!--Load the AJAX API -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></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 = $.ajax({
url:"getData.php",
dataType:"json",
async:false
}).responseText;
//Create our data table out of JSON data loaded from server
var data=new google.visualization.DataTable(jsonData);
//Instantiate and draw our chart, passing in some options
var chart=new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data,{width:400,height:240});
}
</script>
</head>
<body>
<!--Div that will hold the pie chart -->
<div id="chart_div"></div>
</body>
</html>
推荐答案
您需要花一些时间来了解ajax的工作方式。它不是同步的,它不会立即返回响应,而是在请求完成后通过回调传递响应。
You need to take some time to understand how ajax works. Its not synchronous in that it will not immediately return a response, instead the response is passed via callbacks once the request is completed.
var jsonData = $.ajax({
url:"getData.php",
dataType:"json",
async:false,
success : function(response) {
// Init chart here
}
});
您可能会因为使用mysql函数而被指责,这些函数将在PHP 5.5中被弃用并在未来被删除。
You will likely get chided for using mysql functions which will be deprecated in PHP 5.5 and removed in the future.
这篇关于无法提取数据以获取谷歌图表。没有显示任何内容,但getdata.php显示json编码的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!