我想创建一个饼图,它基本上应该显示学生已经完成的所有任务中完成了多少任务。所以我的任务表看起来像这样
任务 task_id | task_student_id | task_status
* task_student_id *引用学生表中的* user_id *
因此,假设我的带有user_id的学生的样本数据在任务表
中如下所示task_id | task_student_id | task_status 1 6完成 2 6完成 3 6 not_complete 4 6 not_complete 5 6完成
因此,该学生的饼图应显示已完成和未完成任务的数量或百分比(即40%未完成60%完成)
我在网上找到了一个教程。有两个php文件,第一个允许你从学生表中选择user_id
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart,table package.
google.load('visualization', '1', {'packages':['corechart','table']});
function drawItems(num) {
var jsonPieChartData = $.ajax({
url: "getpiechartdata.php",
data: "q="+num,
dataType:"json",
async: false
}).responseText;
var jsonTableData = $.ajax({
url: "gettabledata.php",
data: "q="+num,
dataType:"json",
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
var piechartdata = new google.visualization.DataTable(jsonPieChartData);
var tabledata = new google.visualization.DataTable(jsonTableData);
// Instantiate and draw our pie chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(piechartdata, {
width: 700,
height: 500,
chartArea: { left:"5%",top:"5%",width:"90%",height:"90%" }
});
// Instantiate and draw our table, passing in some options.
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(tabledata, {showRowNumber: true, alternatingRowStyle: true});
}
</script>
</head>
<body>
<form>
<select name="users" onchange="drawItems(this.value)">
<option value="">Select a student:</option>
<?php
$dbuser="";
$dbname="";
$dbpass="";
$dbserver="";
// Make a MySQL Connection
mysql_connect($dbserver, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
// Create a Query
$sql_query = "SELECT user_id, user_name FROM students";
// Execute query
$result = mysql_query($sql_query) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo '<option value='. $row['user_id'] . '>'. $row['user_name'] . '</option>';
}
mysql_close($con);
?>
</select>
</form>
<div id="chart_div"></div>
<div id="table_div"></div>
</body>
</html>
然后第二个php文件根据所选的user_id
填充图表<?php
$q=$_GET["q"];
$dbuser="";
$dbname="";
$dbpass="";
$dbserver="";
$sql_query = "SELECT task_status, COUNT(*) FROM tasks
WHERE task_student_id=" . $q . ""
$con = mysql_connect($dbserver,$dbuser,$dbpass);
if (!$con){ die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname, $con);
$result = mysql_query($sql_query);
$data = array('cols' => array(array('label' => 'Not completed', 'type' => 'string'),
array('label' => 'Completed', 'type' => 'string')),
'rows' => array());
while($row = mysql_fetch_row($result)) {
$data['rows'][] = array('c' => array(array('v' => $row[0]), array('v' => $row[1])));
}
echo json_encode($data);
mysql_close($con);
?>
我确定我在对数组和查询做错了。此外,我正在考虑创建两个单独的SQL查询并将它们保存在两个php变量中; $ not_completed,$完成并在图表中填充它。不知道哪一个是我的问题的最佳选择。有人可以帮忙吗?
答案 0 :(得分:0)
查看https://developers.google.com/chart/以查找Google图表的文档。如果你已经有变量$ complete和$ not_complete,你可以使用这个例子http://pastebin.com/qsB0hqmJ,你应该使用mysql_result改进你的mysql / php代码。
$tasks_completed = mysql_result(mysql_query("SELECT COUNT(*) FROM tasks..."),0);