1
我想動態生成谷歌餅圖與他們提供的庫。谷歌餅圖動態與MySQL和PHP
我對獲取數據的代碼從MySQL:
$query = "SELECT name FROM mybase WHERE name = '$name' ";
$mysqli = new mysqli();
$mysqli->connect('localhost', 'blabla', 'blabla', 'blabla');
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
//here I cant figure out how to make data in format for pie chart
}
我需要得到這個數據到Java腳本。看到x倍意味着它是第幾次在數據庫中列出:
<html>
<head>
<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 data = google.visualization.arrayToDataTable([
['Name', 'Seen x times'],
['somenamefrommysql', 11],
['somenamefrommysql', 2],
['somenamefrommysql', 2],
['somenamefrommysql', 2],
['somenamefrommysql', 7]
]);
var options = {
title: 'My Daily Activities'
};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
如果你的HTML和JS都產生了良好的餅圖,那麼你爲什麼不使用你的PHP代碼動態生成的JS代碼?更具體地說,你必須生成'data'變量,它只是從PHP的角度來看的一個字符串。另一方面,如果你有大量的數據,也許這不是最好的方法,你可能需要使用一些奇特的AJAX調用... –