How do I find an average with radio buttons?
Posted: Tue Nov 11, 2008 5:55 pm
a
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
Question 1: How good was this response
Answer1: <input type="radio" name="question[1]" value="0">
Answer2: <input type="radio" name="question[1]" value="1">
Answer3: <input type="radio" name="question[1]" value="2">
Question 2: How good is Jcart's golf game
Answer1: <input type="radio" name="question[2]" value="0">
Answer2: <input type="radio" name="question[2]" value="1">
Answer3: <input type="radio" name="question[2]" value="2">Code: Select all
if (!empty($_POST['question'])) { //make sure input exists
$average = array_sum($_POST['question']) / count($_POST['question']);
} else {
$average = 0;
}
//or shortened to
$average = !empty($_POST['question'] ? array_sum($_POST['question']) / count($_POST['question']) : 0;Code: Select all
<?php
/* set the allowed order by columns */
$default_sort = 'semester';
/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}
/* connect to db */
$db = mysql_connect(Sorry) or die(mysql_error());
mysql_select_db("",$db) or die(mysql_error());
/* delete a record specified by $id */
if (isset ($_GET['id']) ) {
$id = $_GET['id'];
$result =mysql_query("DELETE FROM surveybook WHERE id=$id", $db);
}
/* construct and run our query */
$result = mysql_query("SELECT * FROM surveybook ORDER BY $order",$db);
/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
printf("The number of records %d\n", $numrows);
if ($numrows == 0) {
echo "No data to display!";
exit;
}
/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>\n";
echo "<TR>\n";
echo "<TD><b>Delete</b></TD>";
foreach ($row as $heading=>$column) {
/* hyperlink it so that we can order by this column */
echo "<TD><b>";
echo "<a href=\"{$_SERVER['PHP_SELF']}?order=$heading\">$heading</a>";
echo "</b></TD>\n";
}
echo "</TR>\n";
/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>\n";
$id=$row["id"];
echo "<TD><a href=\"{$_SERVER['PHP_SELF']}?id=$id\">Delete</a></TD>\n";
foreach ($row as $column) {
echo "<TD>$column</TD>\n";
}
echo "</TR>\n";
}
echo "</TABLE>\n";
Thats incorrect. You would only need to use mysql's AVG() function if you have a normalize set of data to average out. Since from what you've shown me you do not want to store the answers themselves, even though you probably should for a larger featureset, we can simply perform the calculation prior to sending it to mysql and simply save the result (which is exactly what I posted previously).Rex wrote:I want to store the average value in the database but I was told to use the AVG in the database to get the average and to send it to the database without first averaging.
Rex
Code: Select all
<?php
$Course_number = $_REQUEST['Course_number'];
$Semester = $_REQUEST['Semester'];
$Course_title = $_REQUEST['Course_title'];
$Instructor_name = $_REQUEST['Instructor_name'];
$Average = $_REQUEST['Average'];
$Comments = $_REQUEST['Comments'];
$Message = $_REQUEST['Message'];
if ($Message): ?>
Thank You For Filling Out the Form
<?php
$db = mysql_connect(REMOVED) or die(mysql_error());
mysql_select_db("",$db) or die(mysql_error());
mysql_query("INSERT INTO `surveybook` (`id`, `course_number`, `semester`, `course_title`, `instructor_name`, `average`, `comments`) VALUES (NULL, '$Course_number', '$Semester', '$Course_title', '$Instructor_name', '$Average', '$Comments')",$db);
endif;?>
Code: Select all
<html><body>
<title>View Questionnaire Records</title>
<HR SIZE="3" WIDTH="75%" COLOR="darkgreen">
<center><font size=6 color=black>View Questionnaire Records</font></center>
<p>
<form action="viewquestion.php" method="post">
<p>Keyword : <input type="text" name="name"></input></p>
<input type="HIDDEN" name="Message" value="True">
<input type="submit"></input></p>
</form>
<br>
<?php $name = $_REQUEST['name'];
$Message = $_REQUEST['Message'];
if ($Message): ?>
Thank You For Filling Out the Form
<?php
$db = mysql_connect("", "", "") or die(mysql_error());
mysql_select_db("",$db) or die(mysql_error());
$result = mysql_query("SELECT * FROM questionnaire WHERE comments LIKE '%$name%'",$db);
echo "<br />";
while($row = mysql_fetch_array($result)){
echo "<br />";
echo "Keyword ". $name. " is found in ".$row['comments'];
}
echo "<br />";
endif;?></p>
</body>
</html>