How do I find an average with radio buttons?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rex
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 5:41 pm

How do I find an average with radio buttons?

Post by Rex »

a
Last edited by Rex on Tue Nov 11, 2008 10:07 pm, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do I find an average with radio buttons?

Post by John Cartwright »

Note that I have removed your database credentials. I would advise never posting this information publically.

Secondly, you'll likely get a much better response if you pose a more direct question(s).

To start you off, I would suggest you rename your fields more appropriately, especially since your radios are not grouped together for each question.

E.g., you should be doing something like:

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">
Now from looking at your code, it does not look like you want to store the values of theses answers in the database, instead simply storing the average values of the questions. If that is the case, you could simply do

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;
Rex
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 5:41 pm

Re: How do I find an average with radio buttons?

Post by Rex »

That is the best response I've heard all day I'll give it a value of 2 on your provided scale on mine it would be a 5 for the highest, but I do have to send the inputs to a database and they have to be averaged and have the capability to be sorted in surveysort.php

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";
 
Thank You!
Last edited by Rex on Tue Nov 11, 2008 10:11 pm, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do I find an average with radio buttons?

Post by John Cartwright »

Lets just clarify: you want to store the answers in the database or the average value of the answers?
Rex
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 5:41 pm

Re: How do I find an average with radio buttons?

Post by Rex »

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do I find an average with radio buttons?

Post by John Cartwright »

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
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
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 5:41 pm

Re: How do I find an average with radio buttons?

Post by Rex »

Well what if I needed it to be where the average now is, but instead the 8 question's values being requested and then inserted into only one column of the database table.

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;?> 
 
 
Last edited by Rex on Tue Nov 11, 2008 10:12 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do I find an average with radio buttons?

Post by John Cartwright »

Thats the 3rd time I've had to remove your database credentials. Let it be the last.
Rex
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 5:41 pm

Re: How do I find an average with radio buttons?

Post by Rex »

You need more than just a password to get into this one. I just thought echo meant display onscreen. It turns out they average on retrieve so they do have to in as value per question, they have to come out as average per instructor.

Other than that sorry for causing you to waste your time.
Rex
Forum Newbie
Posts: 6
Joined: Tue Nov 11, 2008 5:41 pm

Re: How do I find an average with radio buttons?

Post by Rex »

Well it's still encrypted but yeah after looking at that again Thanks for taking that down. I am worried now. I've been working on this the better part of the last 24hrs and just didn't much think of it. No credentials this time.

This is what retrieves it but in between I have a long way to go

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>
 
 
Post Reply