我试图使用谷歌图表和集成在我的安卓应用程序。在此之前,我试图检查我的html代码在谷歌浏览器。在代码中,我试图整合从MYSQL获得数据。我的代码如下
<?php
$server = "localhost";
$user="root";
$password="";
$database = "mobiledb";
$connection = mysql_connect($server,$user,$password)
or
trigger_error(mysql_error(),E_USER_ERROR);
$db = mysql_select_db($database,$connection);
$sth = mysql_query("SELECT * FROM `table` WHERE `x-axis` AND `y-axis` LIMIT 0, 30 ");
while($r = mysql_fetch_assoc($sth)) {
$arr2=array_keys($x-axis);
$arr1=array_values($y-axis);
}
for($i=0;$i<count($arr1);$i++)
{
$chart_array[$i]=array((string)$arr2[$i],intval($arr1[$i]));
}
echo "<pre>";
$data=json_encode($chart_array);
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
$(function () {
var data = new google.visualization.DataTable();
data.addColumn("string", "x-axis");
data.addColumn("number", "y-axis");
data.addRows(<?php $data ?>);
});
var options = {
title: 'Details',
is3D: 'true',
width: 800,
height: 600
};
var chart = new google.visualization.ColumnChart(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>
但是我无法看到图表。我得到以下错误";$ data = json_encode (chart_array美元);>当我检查页面时,我发现了以下错误*Uncaught SyntaxError:意外的令牌<*请帮我解决这个问题
查询
$sth = mysql_query("SELECT `x-axis`,`y-axis` FROM `table` LIMIT 0, 30 ");
如果你想要列的x轴和y轴那么使用这个
然后改正
data.addRows(<?php echo $data ?>);
你可能需要修改这一行
data.addRows(<?php $data ?>); //incorrect
data.addRows(<?php echo($data); ?>); //you forgot to echo it
你们的PHP
代码有这么多问题
//what is this?
$sth = mysql_query("SELECT * FROM `table` WHERE `x-axis` AND `y-axis` LIMIT 0, 30 ");
//probably you want this?
$sth = mysql_query("SELECT `x-axis`, `y-axis` FROM `table` LIMIT 0, 30 ");
//this is wrong
while($r = mysql_fetch_assoc($sth)) {
$arr2=array_keys($x-axis);
$arr1=array_values($y-axis);
}
//try this
while($r = mysql_fetch_assoc($sth)) {
$arr2=array_keys($r['x-axis']);
$arr1=array_values($r['y-axis']);
}
//where is $chart_array defined? It should be like this
$chart_array = array();
for($i=0;$i<count($arr1);$i++)
{
$chart_array[$i]=array((string)$arr2[$i],intval($arr1[$i]));
}