Page 1 of 1
Question on how to compare value while selected radio button
Posted: Wed Apr 11, 2012 11:43 pm
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 ?

Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 1:23 am
by social_experiment
What type of value does $row["eqid"] contain
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 2:12 am
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 ><
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 4:38 am
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
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 4:52 am
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

Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 5:16 am
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?
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 5:47 am
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?
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 5:51 am
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 .

Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 6:00 am
by social_experiment
kwh01 wrote:still need a query to check right?
Yes
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 6:09 am
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
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 6:55 am
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?
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 7:36 am
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.
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 8:06 am
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
Re: Question on how to compare value while selected radio bu
Posted: Thu Apr 12, 2012 10:09 am
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