在我的Demo.php中,我按照以下说明设置了Google图表可视化:
<!--Load the AJAX API-->
<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() {
var jsonPieChartData = $.ajax({
url: "getVehicleData.php",
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var piechartdata = new google.visualization.DataTable(jsonPieChartData);
var options = {
title: 'Vehicles Currently in Stock'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(piechartdata, options);
}
</script>
我也按照下面的说明编写了getVehicleData脚本,我已在http://jsonlint.com/上验证了JSON,但我仍然在Google Chrome上收到错误。
$sql_qry = "SELECT `make`, COUNT(*) as total FROM `vehicle` GROUP BY `make` ORDER BY COUNT(*) DESC";
$data = perform_query($db, $sql_qry);
echo '{
"cols": [
{
"id": "",
"label": "Make",
"pattern": "",
"type": "string"
},
{
"id": "",
"label": "TotalNumber",
"pattern": "",
"type": "number"
}
],
"rows": [';
foreach ($data as $d){
//echo $d['make'] . $d['total'];
echo ' {
"c": [
{
"v": "'.$d['make'].'",
"f": null
},
{
"v": "'.$d['total'].'",
"f": null
}
]
}';
}
echo '] }' ;
以下是我在Google Chrome上遇到的错误:
Uncaught Error: Invalid JSON string: {
"cols": [
{
"id": "",
"label": "Make",
"pattern": "",
"type": "string"
},
{
"id": "",
"label": "TotalNumber",
"pattern": "",
"type": "number"
}
],
"rows": [ {
"c": [
{
"v": "Vauxhall",
"f": null
},
{
"v": "2",
"f": null
}
]
} {
"c": [
{
"v": "Ford",
"f": null
},
{
"v": "1",
"f": null
}
]
} {
"c": [
{
"v": "Toyota",
"f": null
},
{
"v": "1",
"f": null
}
]
} {
"c": [
{
"v": "Mercedes",
"f": null
},
{
"v": "1",
"f": null
}
]
} {
"c": [
{
"v": "Citreon",
"f": null
},
{
"v": "1",
"f": null
}
]
}] }
答案 0 :(得分:0)
$sql_qry = "SELECT `make`, COUNT(*) as total FROM `vehicle` GROUP BY `make` ORDER BY COUNT(*) DESC";
$data = perform_query($db, $sql_qry);
echo '{
"cols": [
{
"id": "",
"label": "Make",
"pattern": "",
"type": "string"
},
{
"id": "",
"label": "TotalNumber",
"pattern": "",
"type": "number"
}
],
"rows": [';
foreach ($data as $d){
//echo $d['make'] . $d['total'];
echo ' {
"c": [
{
"v": "'.$d['make'].'",
"f": null
},
{
"v": "'.$d['total'].'",
"f": null
}
]
},'; // added a , here, but I don't know PHP well enough to NOT add the comma for the LAST loop
}
echo '] }' ;
看到评论 - 我不太了解PHP足以抑制它,在foreach的最后一个循环中 - 我是一个javascript书呆子,我知道PHP足够危险
编辑:这次重写可能更好
$sql_qry = "SELECT `make`, COUNT(*) as total FROM `vehicle` GROUP BY `make` ORDER BY COUNT(*) DESC";
$data = perform_query($db, $sql_qry);
$output = '';
foreach ($data as $d){
if ($output != '') {
$output .= ',';
}
$output .= '{
"c": [
{
"v": "'.$d['make'].'",
"f": null
},
{
"v": "'.$d['total'].'",
"f": null
}
]
}';
}
echo '{
"cols": [
{
"id": "",
"label": "Make",
"pattern": "",
"type": "string"
},
{
"id": "",
"label": "TotalNumber",
"pattern": "",
"type": "number"
}
],
"rows": [' . $output . '] }' ;
答案 1 :(得分:-1)
最后在这里排序的是答案:
// Connect to the MySQL database
require_once('includes/db_connect.php');
require_once('includes/shared.php');
$sql_qry = "SELECT `make`, COUNT(*) as total FROM `vehicle` GROUP BY `make` ORDER BY COUNT(*) DESC";
$res = perform_query($db, $sql_qry);
echo '
{
"cols": [
{"id":"","label":"Topping","pattern":"","type":"string"},
{"id":"","label":"Slices","pattern":"","type":"number"}
],
"rows": [';
$total_rows = $res->fetchColumn();
$row_num = 0;
while($row = $res->fetch(PDO::FETCH_ASSOC)) {
// do loop stuff
$row_num++;
if ($row_num == $total_rows){
echo '
{"c":[{"v":"'.$row['make'].'","f":null},{"v":'.$row['total'].',"f":null}]}
';
}else{
echo '
{"c":[{"v":"'.$row['make'].'","f":null},{"v":'.$row['total'].',"f":null}]},
';
}
}
echo ']
}';