quizmain.php
<?php
session_start();
require_once("connection.php");
extract($_REQUEST);
$number = (int) $_GET['n']; //starting value 1
$n1=rand(1,100);
$_SESSION['RQuestionNumber']=$n1;
$q=mysql_query("select * from quiz WHERE qno = '".$n1."'");
$a=mysql_fetch_array($q);
?>
<p><strong>Question <?php echo $number;?> </strong></p>
</div>
<p>
<label> <?php echo $a[1]?><br />
<br />
<form method="post" action="process1.php">
<input type="radio" name="question" value="a" />
<?php echo $a[2]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="b" />
<?php echo $a[3]?></label>
</p>
<p><br />
<label>
<input type="radio" name="question" value="c" />
<?php echo $a[4]?></label>
</p>
<p><br />
<label> </label>
<label>
<input type="radio" name="question" value="d" />
<?php echo $a[5]?></label>
</p>
<p> </p>
<p>
<label>
<input type="submit" value="Submit" name="Submit" />
<input type="hidden" name="number" value="<?php echo $number; ?>" />
</form>
process1.php
in this page i want to print all the questions which appear througout the quiz along with the correct answers and user choices and i don't know that how to get these questions from the database one by one (from the random variable as user see the questions randomly in the same sequence) and print them when number variable reaches equal to the total variable (i.e. after showing all the 10 questions).
<?php
session_start();
require_once("connection.php");
if(!isset($_SESSION['score']))
{
$_SESSION['score'] = 0;
}
if($_POST)
{
$number = $_REQUEST['number'];
$selected_choice = $_REQUEST['question'];
$next = $number+1;
$total=10;
$q = mysql_query("SELECT ans FROM quiz WHERE qno = '".$_SESSION['RQuestionNumber']."'");
$result=mysql_fetch_array($q);
if(!isset($_SESSION['answers']))
{
$answers = array();
}
else
{
$answers = $_SESSION['answers'];
}
$answers[] = array($_SESSION['RQuestionNumber'], $selected_choice);
$_SESSION['answers'] = $answers;
for($i=1;$i<=$total;$i++)
{
echo $_SESSION['answers'][$i][0];
echo $_SESSION['answers'][$i][1];
echo "<br />";
}
echo "<br />";
//Compare
if($result[0][0] == $selected_choice){
//Answer is correct
$_SESSION['score']++;
}
if($number == $total)
{
echo $_SESSION['score'];
session_destroy();
}
else
{
header("location: quizmain.php?n=".$next);
}
}
?>
Aucun commentaire:
Enregistrer un commentaire