0

I am developing an online examination system. Everything works fine but the questions are selected and displayed from question number one to the last question. I wanted to do the following:

  1. Select the questions and display them one after the other at random.

  2. Add a skip button so that students can skip any question they can't answer and it will be rolled back to them later.

  3. Give a time to the answering session so that when the time is up it will stop the student.

This is my code for the question selection.

<!DOCTYPE html">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
body{background-color:#f4fff8;}
#aq{
    position:relative;
    top:50px;
}
#st{
    position:relative;
    top:30px;
}

.btn{
     background-color:#dedbb8;
     color:#016e37;
     text-shadow:3px 3px 3px gray;
     box-shadow:3px 3px 3px gray;           
     height:40px;
     text-align:center;
     font-size:25px;
     font-family:'Times New Roman';
     font-weight:bold; 
     font-style:italic;
     margin-top:2px;
     border-radius: 2px;
}
.style8{
    padding-top:5px;
    font-size:20px;
}
#pt{
    padding-top:20px;
}
h1{
    position:relative;
    color:green;
  top:30px;
}
h2{
    position:relative;
    color:orange;
    top:30px;
}
</style>
</head>

<body>

<?php
require_once("dbconnect.php");
include("header.php");
include("footer.php");
include("stdlogsession.php");

extract($_GET);
extract($_POST);
extract($_SESSION);

$student_id=$_SESSION['login_user'];
$student_id = stripslashes($student_id);
$student_id = mysqli_real_escape_string($db_conn,$student_id);


if(!empty($_GET['examid']))
{
    $examid =$_GET['examid'];
}

// Selecting Database
$db = mysqli_select_db($db_conn,$mysql_database);

$rs=mysqli_query($db_conn,"select * from objquestions where exam_id='$examid'") or die('Error: ' .mysql_error($db_conn));

if(!isset($_SESSION['qn']))
{
    $_SESSION['qn']=0;
}

if(!empty($_POST['submit'])=='Answer' && isset($ans))
{
    mysqli_data_seek($rs,$_SESSION['qn']);
    $row= mysqli_fetch_row($rs);
    if($ans==$row[8])
    {
          $remarks= "Correct";
          $mark=$row[9];
    } else {
          $remarks= "Wrong";
          $mark=0;
    }               

    mysqli_query($db_conn,"insert into results(exam_id,student_id,quesNum,choice,remarks,mark) values ('$row[1]','$student_id','$row[2]', '$ans','$remarks','$mark')") or die(mysqli_error($db_conn));

    $_SESSION['qn']=$_SESSION['qn']+1;

}   

if($_SESSION['qn']>mysqli_num_rows($rs)-1)
{
        $examN=mysqli_query($db_conn,"select examName from exampaper where exam_id='$examid'") or die('Error: ' .mysqli_error($db_conn));
        $row = mysqli_num_rows($examN);
        if ($row > 0) {
            $rows=mysqli_fetch_assoc($examN);

            $Enam=$rows['examName'];
        }

        echo "<center><h1>congrats! you have successfully finished your " .$Enam."</h1></center>";
        echo "<center><h2> Click <a href=instresult.php>here</a> for your results </h2></center>";

        unset($_SESSION['qn']); 
        exit();
   }

   if(!empty($_POST['submit'])=='Skip' && !isset($ans))
     {
         mysqli_data_seek($rs,$_SESSION['qn']);
         $row= mysqli_fetch_row($rs);
         $_SESSION['qn']=$_SESSION['qn']+1;
     }

     $rs=mysqli_query($db_conn,"select * from exampaper where exam_id='$examid'") or die('Error: ' .mysql_error($db_conn));
     $rows = mysqli_num_rows($rs);
     while($rows=mysqli_fetch_row($rs))
{

echo "<center>";
echo "<table id=aq >";
echo "<tr><td>
<span class=style8>SUBJECT:</span></td>
<td class=style8>$rows[2]</td>
</tr>";
echo "<tr><td>
<span class=style8>EXAMINATION NAME:</span></td>
<td class=style8>$rows[1]</td>
</tr>";
echo "<tr><td>
<span class=style8>EXAMINATION DATE:</</span></td>
<td class=style8>$rows[3]</td>
</tr>";
echo "<tr><td>
<span class=style8>TOTAL TIME:</span></td>
<td class=style8>$rows[4]</td>
</tr>";
echo "<tr><td>
<span class=style8>INSTRUCTIONS:</span></td>
<td class=style8>$rows[5]</td>
</tr>";
echo "</table>";
echo "</center>";
}


$rs=mysqli_query($db_conn,"select * from objquestions where exam_id='$examid'") or die(mysqli_error($db_conn));
if($_SESSION['qn']>mysqli_num_rows($rs)-1)
{
unset($_SESSION['qn']);}

echo "<center>";
echo "<table id=st>";
mysqli_data_seek($rs,$_SESSION['qn']);
$row= mysqli_fetch_row($rs);
echo "<form name=myfm method=post action=exam.php>";
$n=$_SESSION['qn']+1;
echo "<tR ><td id=pt><span class=style8>Quetion ".  $n .": $row[3]</style></td></tr>";
echo "<tr><td class=style8><input type=radio name=ans value=A>$row[4]</td></tr>";
echo "<tr><td class=style8> <input type=radio name=ans value=B>$row[5]</td></tr>";
echo "<tr><td class=style8><input type=radio name=ans value=C>$row[6]</td></tr>";
echo "<tr><td class=style8><input type=radio name=ans value=D>$row[7]</td></tr>";

echo "<tr><td><input class=btn type=submit name=submit value='Answer'> <input align=right class=btn type=submit name=submit value='Skip'></td></tr></form>";
echo "</table>";
echo "</center>";
?>
</body>
</html>
Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
  • Welcome to Stackoverflow. We're here to help you with specific programming problems, not to code for you. Show us what you've tried so far and we'll help you find your errors, but NOBODY is going to code this for you for free. May if you offer some money, but SO isn't the place for such offers. http://stackoverflow.com/help/how-to-ask - http://stackoverflow.com/help/on-topic – Twinfriends Mar 10 '17 at 08:07
  • Formatting for better readability – Dmitri Zaitsev Mar 12 '17 at 15:01

0 Answers0