Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

Randomizing questions order in quiz

$
0
0

Hey guys. As you can see from my post count i am new here and also new to php.

I am trying to modify a code sample of a quiz system that uses mysql database to store all the info such as users,questions,answers,score etc. I have manage to modify the things i want but i am having a hard time  making the questions appear in random order when the user takes the test. The table that sores the questions and the answers is named "mst_question" and it has the following structure:

 

que_id test_id  |  que_desc  |  ans1  |   ans2   | ans4  |  ans4true_ans

 

 

I have tried two things:

 

1.Modified the query by adding the order by rand() function.I read it is not the fastest way but at this stage i don't care about speed. The results returned were indeed random but the score doesnt seem to work properly and sometimes i get the same question twice. More specific this the query i tried on my effort of eliminating duplicates but as i said it is not working properly:

$rs=mysql_query("SELECT DISTINCT *
FROM mst_question
WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid )",$cn)

2. I tried soring the results into an array and use the shuffle function but that didnt work either.

 

I am guessing the query is correct and the reason it doesnt work properly has something to do with the way the results are being displayed but as i said i am a newbie to php/mySQL.

 

This is the modified content of the php file. Any ideas/suggestions are welcome

<?php
session_start();
include("database.php");
extract($_POST);
extract($_GET);
extract($_SESSION);

if(isset($subid) && isset($testid))
{
$_SESSION[sid]=$subid;
$_SESSION[tid]=$testid;
header("location:quiz.php");
}
if(!isset($_SESSION[sid]) || !isset($_SESSION[tid]))  //An den exei tethei subject kai testid tous paei sto index.php
{
    header("location: index.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Online Quiz</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="quiz.css" rel="stylesheet" type="text/css">
</head>

<body>
<?php
include("header.php");

$rs=mysql_query("SELECT DISTINCT * FROM mst_question WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid ) ORDER BY rand()",$cn) or die(mysql_error());
if(!isset($_SESSION[qn]))
{
    $_SESSION[qn]=0;
    mysql_query("delete from mst_useranswer where sess_id='" . session_id() ."'") or die(mysql_error());
    $_SESSION[trueans]=0;
    
}
else
{    
        if($submit=='Next Question' && isset($ans))
        {
                mysql_data_seek($rs,$_SESSION[qn]);
                $row= mysql_fetch_row($rs);    
                mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
                if($ans==$row[7])
                {
                            $_SESSION[trueans]=$_SESSION[trueans]+1;
                }
                $_SESSION[qn]=$_SESSION[qn]+1;
        }
        else if($submit=='Get Result' && isset($ans)) 
        {
                mysql_data_seek($rs,$_SESSION[qn]);
                $row= mysql_fetch_row($rs);    
                mysql_query("insert into mst_useranswer(sess_id, test_id, que_des, ans1,ans2,ans3,ans4,true_ans,your_ans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());
                if($ans==$row[7])
                {
                            $_SESSION[trueans]=$_SESSION[trueans]+1;
                }
                echo "<h1 class=head1> Result</h1>";
                $_SESSION[qn]=$_SESSION[qn]+1;
                echo "<Table align=center><tr class=tot><td>Total Question<td> $_SESSION[qn]";
                echo "<tr class=tans><td>True Answer<td>".$_SESSION[trueans];
                $w=$_SESSION[qn]-$_SESSION[trueans];
                echo "<tr class=fans><td>Wrong Answer<td> ". $w;
                echo "</table>";
                mysql_query("insert into mst_result(login,test_id,test_date,score) values('$login',$tid,'".date("d/m/Y")."',$_SESSION[trueans])") or die(mysql_error());
                echo "<h1 align=center><a href=review.php> Review Question</a> </h1>";
                unset($_SESSION[qn]);
                unset($_SESSION[sid]);
                unset($_SESSION[tid]);
                unset($_SESSION[trueans]);
                exit;
        }
}
$rs=mysql_query("SELECT DISTINCT *
FROM mst_question
WHERE que_id IN (SELECT DISTINCT que_id FROM mst_question where test_id=$tid )",$cn) or die(mysql_error());
if($_SESSION[qn]>mysql_num_rows($rs)-1)
{
unset($_SESSION[qn]);
echo "<h1 class=head1>Some Error  Occured</h1>";
session_destroy();
echo "Please <a href=index.php> Start Again</a>";

exit;
}
mysql_data_seek($rs,$_SESSION[qn]);
$row= mysql_fetch_row($rs);
echo "<form name=myfm method=post action=quiz.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$_SESSION[qn]+1;
echo "<tR><td><span class=style2>Que ".  $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name=ans value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name=ans value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name=ans value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name=ans value=4>$row[6]";

if($_SESSION[qn]<mysql_num_rows($rs)-1)
echo "<tr><td><input type=submit name=submit value='Next Question'></form>";
else
echo "<tr><td><input type=submit name=submit value='Get Result'></form>";
echo "</table></table>";
?>
</body>
</html>

Viewing all articles
Browse latest Browse all 13200

Trending Articles