我有以下代码
$objDbResultByModel = $objDatabase->Query($modelQuery);
$return_arr = array();
echo "<h3>Total Model No<br></h3><strong>";
while ($mixRow = $objDbResultByModel->FetchArray()) {
$row_array['Model'] = $mixRow['Manu']." - " .$mixRow['model'];
$row_array['Quantity'] = $mixRow['total'];
array_push($return_arr,$row_array);
$jsonTable = json_encode($return_arr);
echo $jsonTable;
我在浏览器中得到以下结果。
[
{"Model No.":"HP - 3120-MT","Quantity":"1"},
{"Model No.":"IBM - 6087-CN8","Quantity":"1"},
{"Model No.":"Fujitsu - D2594","Quantity":"4"},
{"Model No.":"Fujitsu - D2750","Quantity":"15"},
{"Model No.":"HP - DC 7800","Quantity":"43"},
{"Model No.":"HP - DC7700","Quantity":"1"},
{"Model No.":"HP - DC7900","Quantity":"8"},
{"Model No.":"Fujitsu - E5720","Quantity":"1"},
{"Model No.":"RM - RM","Quantity":"5"},
{"Model No.":"HP - XW4400","Quantity":"1"},
{"Model No.":"HP - XW4600","Quantity":"2"}
]
但是如果我将 jsonTable 与 google charts api 一起使用,它就不起作用。我做错了什么?见下文。
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Model');
data.addColumn('number', 'Quantity');
data.addRows([
<?php echo $jsonTable; ?>
]);
// Set chart options
var options = {'title':'Model Quantity in this Shipment',
'width':400,
'height':300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
你能帮我解决这里的问题吗?
最佳答案
DataTable 不能使用您输入的数据格式。改变这个:
$row_array['Model'] = $mixRow['Manu']." - " .$mixRow['model'];
$row_array['Quantity'] = $mixRow['total'];
为此:
$row_array = array($mixRow['Manu'] . " - " . $mixRow['model'], $mixRow['total']);
关于用于 Google Charts Api 的 PHP MySQL 查询数组 json_encode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24934375/