Jump to content

While loop radio buttons with different values


Recommended Posts

I have the following php code that produces some multiple choice questions with each answer per question giving different points.

$done = $_POST['done'];
if ($done  < '1' )  {
<form action="quiz.php" method="post" />

How many domains do you own? </BR>
<input type="radio" name="op1" value="0"> I don't  <BR/><input type="radio" name="op1" value="5"> 1 <BR/><input type="radio" name="op1" value="10"> 2 <BR/><input type="radio" name="op1" value="15"> 3 <BR/><input type="radio" name="op1" value="20"> 4 <BR/><input type="radio" name="op1" value="30"> 5 or more <BR><BR>

Do you use skype? </BR>
<input type="radio" name="op2" value="1"> Yes <BR/><input type="radio" name="op2" value="0"> No </BR></BR>

How many people are currently in your Skype contact list? </BR>
<input type="radio" name="op3" value="1"> less than 5 <BR/><input type="radio" name="op3" value="2"> 5 to 10 <BR/><input type="radio" name="op3" value="3"> 11 to 15 <BR/><input type="radio" name="op3" value="4"> 15 to 30 <BR/><input type="radio" name="op3" value="5"> more than 30  <BR><BR>

 <input type="hidden" name="done" value="1"/>
 <input type="submit" value="Submit...."/>

} else {


$total = $score1 + $score2+ $score3;

print "Your score Is " . $total  . "!";

I am trying to modify the code so i can make it work with a database i have.This is the structure of the table question which also includes the answers and the points of each answer.

que_id | test_id | que_desc | ans1 | ans2 | ans3 | ans4 | true_ans | type | points1 | points2 | points3 | points4 |


And this is what i have done so far:


include 'dbconnect.php';

include 'functions.php';

if(isset($subid) && isset($testid))

$rs=mysql_query("select * from question where test_id=20",$conn) or die(mysql_error());
while($row = mysql_fetch_array($rs)) {
$id = $row["id"];
$question = $row["que_desc"];
$opt1 = $row["ans1"];
$opt2 = $row["ans2"];
$opt3 = $row["ans3"];
$opt4 = $row["ans4"];
$score1= $row['points1'];
$score2= $row['points2'];
$score3= $row['points3'];
$score4= $row['points4'];
$answer = $row["true_ans"];

echo "$question<br>";
echo "<input type='radio' id='radio1' name='$i' value='$opt1'/><label for='radio1'>$opt1</label><br>";
echo "<input type='radio' id='radio1' name='$i' value='$opt2'/><label for='radio1'>$opt2</label><br>";
echo "<input type='radio' id='radio1' name='$i' value='$opt3'/><label for='radio1'>$opt3</label><br>";
echo "<input type='radio' id='radio1' name='$i' value='$opt4'/><label for='radio1'>$opt4</label><br>";
echo "<br>";


 <input type="hidden" name="done" value="1"/>
 <input type="submit" value="Submit...."/>

} else {
$total = ...........


My question is how to pass each answer points using the While loop i have above and how to calculate the total score.

Edited by GFOnyX
Link to comment
Share on other sites

  • 4 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.