Jump to content

Recommended Posts

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

<?php
$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...."/>
</form>

<?php
} else {

$score1=$_POST['op1'];
$score2=$_POST['op2'];
$score3=$_POST['op3'];

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

print "Your score Is " . $total  . "!";
}
?>
</div>

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:

<?php
session_start();
require_once('config.php');

include 'dbconnect.php';

include 'functions.php';
extract($_POST);
extract($_GET);
extract($_SESSION);

if(isset($subid) && isset($testid))
{
$_SESSION[sid]=$subid;
$_SESSION[tid]=$testid;
header("location:$self_page?page=kinder.php");
}
?>

<?php
$rs=mysql_query("select * from question where test_id=20",$conn) or die(mysql_error());
$i=0;
while($row = mysql_fetch_array($rs)) {
$i++;    
$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>";
}

?>

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

<?php
} else {
$total = ...........
}

?>
</div>
</body>

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
  • 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.

Guest
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.