从mysql计算平均评级并将其显示为图形

时间:2013-07-31 20:16:37

标签: php mysql

请帮我解决这个问题。因为我刚刚进入PHP / Mysql的学习阶段。

我有一个PHP反馈表,作为1-5的评级系统。您可以在http://innovatrix.co.in/feedback_priyajit/feedback%20form1.html

找到我的表单

每次用户提供反馈时,它都会将表单值保存到mysql数据库中。下面是我的数据库结构。

enter image description here

现在我想计算每一行的平均数据(比如等待),并将其作为图形显示在php文件中,并在每个选项上显示单独的图表,但在同一页面上。

我知道我可以使用查询SELECT AVG(waiting) FROM feedback来获得“等待”的平均值

但是,如何为同一文件中的每个选项执行此操作,并将其显示为图形。数据库将经常更新,因此它也应反映图表。

请帮助我实现这个目标。

下面是我用来将表单值存储到数据库中的php文件。

<title>process</title>
<?php
$host="localhost";
$user_name="pramir_feedback";
$pwd="feedback";
$database_name="pramir_feedback";
$db=mysql_connect($host, $user_name, $pwd);
if (mysql_error() > "") print mysql_error() . "<br>";
mysql_select_db($database_name, $db);
if (mysql_error() > "") print mysql_error() . "<br>";
$waiting = $_POST['radio1'];
$consultation = $_POST['radio2'];
$preoperative = $_POST['radio3'];
$specialists = $_POST['radio4'];
$assistants = $_POST['radio5'];
$painful = $_POST['radio6'];
$operatingroom = $_POST['radio7'];
$thought = $_POST['radio8'];
$recommend = $_POST['radio9'];
$suggestions = $_POST['suggestions'];
$query = "insert into feedback (waiting, consultation, preoperative, specialists, assistants, painful, operatingroom, thought, recommend, suggestions) values ('" . $waiting . "', '" . $consultation . "', '" . $preoperative . "', '" . $specialists . "', '" . $assistants . "', '" . $painful . "', '" . $operatingroom . "', '" . $thought . "', '" . $recommend . "', '" . $suggestions . "')";
if (mysql_error() > "") print mysql_error() . "<br>";
$qresult = mysql_query($query);
echo "<h1>Thank you for submitting your details!</h1>";
?>  

1 个答案:

答案 0 :(得分:2)

如果您想要一个查询中的所有平均值,则可以用逗号分隔它们。

SELECT AVG(waiting), AVG(consultation), AVG(preoperative), AVG(specialists), ...... FROM feedback

如果您想知道如何将它们放入图表中,请查看许多jQuery图形或绘图制作者之一,例如:http://www.jqplot.com/tests/bar-charts.php