Question on how to compare value while selected radio button

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
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Question on how to compare value while selected radio button

Post by kwh01 »

i can show every eqid into q[eqid]answer and answer by looping
but problem is. once i press the submitted button
i got no idea how to check with it.

as you can see im looping my while loop to get all the question and answer
but once the user click the radio button, how can it know the answer compare with correctanswer based on the eqid and display correct answer?
any suggestion ? :(
Last edited by kwh01 on Sat Apr 14, 2012 3:53 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Question on how to compare value while selected radio bu

Post by social_experiment »

What type of value does $row["eqid"] contain
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: Question on how to compare value while selected radio bu

Post by kwh01 »

social_experiment wrote:What type of value does $row["eqid"] contain
The value is integer.
Example :
q1_answer = "A"
q1_answer = "B"
q1_answer ="C"
q1_answer ="D"

if i checked on D
how should it know?
and i wan compare the $row["answer"] with $row["correctanswer"]..
how could it done ><
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Question on how to compare value while selected radio bu

Post by social_experiment »

As with all $_POST variables, the value can be found within $_POST['nameAttributeValue'] - in this case it will be $_POST['q1_answer']; The more difficult part comes in when you have dynamically created names like the ones you have. You can use a for loop to go through all the radio buttons and gather the answers in that manner

Could you give a bit more information on how the questions will be displayed: 1 per page, multiple questions per page, etc
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: Question on how to compare value while selected radio bu

Post by kwh01 »

social_experiment wrote:As with all $_POST variables, the value can be found within $_POST['nameAttributeValue'] - in this case it will be $_POST['q1_answer']; The more difficult part comes in when you have dynamically created names like the ones you have. You can use a for loop to go through all the radio buttons and gather the answers in that manner

Could you give a bit more information on how the questions will be displayed: 1 per page, multiple questions per page, etc
Im just displaying all the question and option in 1 page.

for example:
Question:1
What programming language u love the most?
o C++
o C#
o Java
o PHP

how to do the for loop to go through all the radio buttons and gather the answers?
do you mind give me some example?
im lost and jammed , i cant think of any solution of it :|
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Question on how to compare value while selected radio bu

Post by social_experiment »

to use a for loop you will need the amount of questions; i'll assume 10 questions in this case

Code: Select all

<?php
 // total questions
 $questionTotal = 10;
 //
 for ($i=1;$i<=10;$i++) {
     // create the variable name for the radio button
     $answerField = 'q' . $i . '_answer';
     // place each answer into an array
     $answerArray[] = $_POST[$answerField];
 }
This places all answers from the radio buttons in an array;

How will you go about checking the answers, against a database?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: Question on how to compare value while selected radio bu

Post by kwh01 »

thanks for the info, i will try it out now.
Im checking the answer against my database.
still need a query to check right?
Last edited by kwh01 on Sat Apr 14, 2012 3:54 am, edited 1 time in total.
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: Question on how to compare value while selected radio bu

Post by kwh01 »

gugi77 wrote:Hi,

Your code:
kwh01 wrote:

Code: Select all


				echo '<input type="radio" name="q' . $row["eqid"] . '_answer" value="A" />'.$row["answer1"].'<br />';
				echo '<input type="radio" name="q' . $row["eqid"] . '_answer" value="B" />'.$row["answer2"].'<br />';
				echo '<input type="radio" name="q' . $row["eqid"] . '_answer" value="C" />'.$row["answer3"].'<br />';
				echo '<input type="radio" name="q' . $row["eqid"] . '_answer" value="D" />'.$row["answer4"].'<br />';
				echo "<br />";
                                $_SESSION["q{$row["eqid"]}_correct_answer"] = $row["correctanswer"];
				$countrandom++;
			
You can get radio button value with i.e $_POST["q1_answer"]. After that to check the correct answer you can do sql 'select' statement to your table. i.e "select correct_answer from <tablename> where eqid=1" and check result query with radio button value.

Hope this help
thanks for the concept and idea, i will try it out . :)
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Question on how to compare value while selected radio bu

Post by social_experiment »

kwh01 wrote:still need a query to check right?
Yes
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: Question on how to compare value while selected radio bu

Post by kwh01 »

for this scenario, we assume that only 1 question available
in this syntax, i select the correctanswer from my table to compare the eqid with the answerfield array
if number of row return 1
it should display correct answer
else wrong answer
but it just keeping looping correct answer for me
Last edited by kwh01 on Sat Apr 14, 2012 3:54 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Question on how to compare value while selected radio bu

Post by social_experiment »

kwh01 wrote:Im really not good at array concept
The purpose of the array is have all the answers in one place;

Code: Select all

<?php
 // total questions
 $questionTotal = 10;
 $correctCount = 0;
 $incorrectCount = 0;
 //
 for ($i=1;$i<=10;$i++) {
     // create the variable name for the radio button
     $answerField = 'q' . $i . '_answer';
     // question id, how you find the question within the database
     $questionId = 'q' . $i;
     // place each answer into an array, with the question id as key
     $answerArray[$questionId] = $_POST[$answerField];
 }
 // start looping through the answer array
 foreach ($answerArray as $key => $value) {
     // select the answer from the database
     $qry = "SELECT `answer` FROM `table` WHERE  `questionId` = '" . $key . "' ";
     $sql = mysql_query($qry);
     if ($sql) {
         $ary = mysql_fetch_array($sql);
         $answer = $ary['answer'];
         // if the answer is correct, increase $correctCount
         if ($answer == $value) {
              $correctCount = $correctCount + 1;
         }
         // if the answer is incorrect, increase $incorrectCount
         else {
             $incorrectCount = $incorrectCount + 1;
         }
      }
 }
?>
This is an example of how you can use the array;

1. Is there a question id, something to identify the question within the database?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: Question on how to compare value while selected radio bu

Post by kwh01 »

social_experiment wrote: This is an example of how you can use the array;

1. Is there a question id, something to identify the question within the database?

Thanks for your example.
I'm much appreciated it.
Im using the same eqid for question id.
Last edited by kwh01 on Sat Apr 14, 2012 3:55 am, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Question on how to compare value while selected radio bu

Post by social_experiment »

This all happens after the button is clicked because the information within the $_POST array will only be available after you submit the form
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
kwh01
Forum Newbie
Posts: 20
Joined: Mon Apr 09, 2012 10:08 pm

Re: Question on how to compare value while selected radio bu

Post by kwh01 »

social_experiment wrote:This all happens after the button is clicked because the information within the $_POST array will only be available after you submit the form
Hi, thank you for ur precious samples
My code is work now! ^.^
<3
Post Reply