我想创建一个饼图,它基本上应该显示学生在分配给他的所有任务中完成了多少任务。所以我的任务表看起来像这样
任务 task_id | task_student_id | 任务状态
*task_student_id* 引用学生表中的 *user_id*
因此,假设我的带有 user_id 的学生的示例数据在任务表中看起来像这样
任务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, $completed 并将其填充到图表中。不知道哪一个是我的问题的最佳选择。有人可以帮忙吗?