Saturday, 10 August 2013

correct answer displaying in php

correct answer displaying in php

I m randomly displaying qus from my database thing is that when i click on
submit button how can check my answer beacuse the code i have return works
fine without rand() function here is my test code
<html>
<body>
<form action="./evaluate.php" method="post">
<?php
$server = mysql_connect ('localhost', 'root', '');
mysql_select_db("aptitude", $server);
$question = mysql_query("SELECT * FROM `main` ORDER BY RAND() LIMIT 0,30");
$x = 1;
while ($row = mysql_fetch_assoc($question))
{
echo '<input type="hidden" name="Q_no" value="$Q_no"/>' ;
echo "Q";
echo $x .'&nbsp &nbsp';
echo $row['Question'] . '<br />';
echo 'A<input type="radio" name="a'.$x.'" value=1 />' .$row['answer1'] ;
echo 'B<input type="radio" name="a'.$x.'" value=2 />' .$row['answer2'] ;
echo 'C<input type="radio" name="a'.$x.'" value=3 />' .$row['answer3'] ;
echo 'D<input type="radio" name="a'.$x.'" value=4 />' .$row['answer4'] .
'<br /><br /> <br />';
$x++;
}
mysql_close($server);
?>
<input type="submit" name="Submit" value="Submit" />
<br>
</form>
</body>
</html
>
enter code here
And this is my evaluate or code for checking answers
enter code here
<html>
<body>
<?php
$server = mysql_connect ('localhost', 'root', '');
mysql_select_db("aptitude", $server);
$question = mysql_query("SELECT * FROM `main`");
$x = 0;
$score = 0;
while ($row = mysql_fetch_assoc($question))
{
echo $row['Question'] . '?<br />';
$answered = $row['answer'.$_POST['a'.$x]] ;
$correct = $row['correct'] ;
if ($answered == $correct ) {
$score++;
$acolor = 'green' ;
}
else {
$acolor = 'red' ;
}
echo 'you answered <font color=' . $acolor . '>' . $answered . '<font
color=black> <br />';
echo 'the correct answer was ' . $correct . '<br />' ;
echo '-------------------------------------- <br />' ;
$x = $x + 1;
}
echo 'You had a total of ' . $score . ' out of ' . $x . ' questions right!';
mysql_close($server);
?>
</body>
</html>
what changes should i make i m having database with column as 'Q_no' ,
'Question' ,'answer1','answer2','answe3r','answer4' & 'correct'

No comments:

Post a Comment