【问题标题】:Unable to extract data to get google chart. Nothing is being displayed but getdata.php shows json encoded data无法提取数据以获取谷歌图表。没有显示任何内容,但 getdata.php 显示 json 编码数据
【发布时间】:2013-02-25 18:18:36
【问题描述】:

这是我的 2 个文件。 //getdata.php:提取数据并使用json_encode转换为json格式。这是编码数据的正确方法吗?我没有更改列名。有必要吗?

<?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函数打印。什么都没有显示。

<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>

【问题讨论】:

    标签: php mysql google-visualization


    【解决方案1】:

    您需要花一些时间来了解 ajax 的工作原理。它不是同步的,因为它不会立即返回响应,而是在请求完成后通过回调传递响应。

    var jsonData = $.ajax({
        url:"getData.php",
        dataType:"json",
        async:false,
        success : function(response) {
            // Init chart here
        }
        });
    

    您可能会因为使用 mysql 函数而受到指责,这些函数将在 PHP 5.5 中被弃用并在未来被删除。

    【讨论】:

    • :很抱歉,您能说得更具体些吗?只是想显示它然后我就切换到mysqli ..我没有正确传递或转换json数据吗?
    • 在您尝试使用结果数据的上下文中,它根本不存在。它将存在于回调的上下文中。
    • 所以我定义了调用我的数据文件-getdata.php 的var jsonData。它有 json 编码的数据。我很抱歉,但无法得到你所说的..
    • 如果你阅读 jquery 的 API 文档,你会发现$.ajax 的返回根本不是你的数据。相反,它是所谓的承诺。 $.ajax 将通过定义为成功的回调传递您的数据。获取数据后,您可以设置图表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-25
    相关资源
    最近更新 更多