title: How to select table rows and display them one after the other at random in php
body: <p>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:</p>
  
  <ol>
  <li><p>Select the questions and display them one after the other at random.</p></li>
  <li><p>Add a skip button so that students can skip any question they can't answer and it will be rolled back to them later.</p></li>
  <li><p>Give a time to the answering session so that when the time is up it will stop the student.</p></li>
  </ol>
  
  <p>This is my code for the question selection.</p>
  
  <pre><code>&lt;!DOCTYPE html"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
  
  &lt;style type="text/css"&gt;
  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;
  }
  &lt;/style&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;?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' &amp;&amp; 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']&gt;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 &gt; 0) {
              $rows=mysqli_fetch_assoc($examN);
  
              $Enam=$rows['examName'];
          }
  
          echo "&lt;center&gt;&lt;h1&gt;congrats! you have successfully finished your " .$Enam."&lt;/h1&gt;&lt;/center&gt;";
          echo "&lt;center&gt;&lt;h2&gt; Click &lt;a href=instresult.php&gt;here&lt;/a&gt; for your results &lt;/h2&gt;&lt;/center&gt;";
  
          unset($_SESSION['qn']); 
          exit();
     }
  
     if(!empty($_POST['submit'])=='Skip' &amp;&amp; !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 "&lt;center&gt;";
  echo "&lt;table id=aq &gt;";
  echo "&lt;tr&gt;&lt;td&gt;
  &lt;span class=style8&gt;SUBJECT:&lt;/span&gt;&lt;/td&gt;
  &lt;td class=style8&gt;$rows[2]&lt;/td&gt;
  &lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td&gt;
  &lt;span class=style8&gt;EXAMINATION NAME:&lt;/span&gt;&lt;/td&gt;
  &lt;td class=style8&gt;$rows[1]&lt;/td&gt;
  &lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td&gt;
  &lt;span class=style8&gt;EXAMINATION DATE:&lt;/&lt;/span&gt;&lt;/td&gt;
  &lt;td class=style8&gt;$rows[3]&lt;/td&gt;
  &lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td&gt;
  &lt;span class=style8&gt;TOTAL TIME:&lt;/span&gt;&lt;/td&gt;
  &lt;td class=style8&gt;$rows[4]&lt;/td&gt;
  &lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td&gt;
  &lt;span class=style8&gt;INSTRUCTIONS:&lt;/span&gt;&lt;/td&gt;
  &lt;td class=style8&gt;$rows[5]&lt;/td&gt;
  &lt;/tr&gt;";
  echo "&lt;/table&gt;";
  echo "&lt;/center&gt;";
  }
  
  
  $rs=mysqli_query($db_conn,"select * from objquestions where exam_id='$examid'") or die(mysqli_error($db_conn));
  if($_SESSION['qn']&gt;mysqli_num_rows($rs)-1)
  {
  unset($_SESSION['qn']);}
  
  echo "&lt;center&gt;";
  echo "&lt;table id=st&gt;";
  mysqli_data_seek($rs,$_SESSION['qn']);
  $row= mysqli_fetch_row($rs);
  echo "&lt;form name=myfm method=post action=exam.php&gt;";
  $n=$_SESSION['qn']+1;
  echo "&lt;tR &gt;&lt;td id=pt&gt;&lt;span class=style8&gt;Quetion ".  $n .": $row[3]&lt;/style&gt;&lt;/td&gt;&lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td class=style8&gt;&lt;input type=radio name=ans value=A&gt;$row[4]&lt;/td&gt;&lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td class=style8&gt; &lt;input type=radio name=ans value=B&gt;$row[5]&lt;/td&gt;&lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td class=style8&gt;&lt;input type=radio name=ans value=C&gt;$row[6]&lt;/td&gt;&lt;/tr&gt;";
  echo "&lt;tr&gt;&lt;td class=style8&gt;&lt;input type=radio name=ans value=D&gt;$row[7]&lt;/td&gt;&lt;/tr&gt;";
  
  echo "&lt;tr&gt;&lt;td&gt;&lt;input class=btn type=submit name=submit value='Answer'&gt; &lt;input align=right class=btn type=submit name=submit value='Skip'&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/form&gt;";
  echo "&lt;/table&gt;";
  echo "&lt;/center&gt;";
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>