By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,638 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,638 IT Pros & Developers. It's quick & easy.

How to keep track of the questions in a quiz using PHP

P: 2
Hi.I'm doing a quiz for students.I have 25 questions displaying 1 question per page with a next button.I already manage to count its scores.But my problem is how to keep track if a certain question has no answer.So that in case a student wants to skip a question, he/she can go back to it.

The questions are shown according to its question id(order by qid).After showing all the questions,and if there's a question with no answer,it will automatically display again one by one with a next button.I hope someone can help me.

Here is my code.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3.     include('config.php');
  4.  
  5.     if(!isset($a))
  6.     {
  7.         $a=0;
  8.         //unset($_SESSION['score']);
  9.     }
  10.  
  11.     if(isset($_POST['next'])) {
  12.         $a=$_POST['a'];
  13.  
  14.  
  15.     }
  16.  
  17.     $sql1="SELECT * FROM questionpart2 ORDER by qid LIMIT 1 OFFSET $a";
  18.     $result=mysql_query($sql1);
  19.     $num = mysql_num_rows($result);
  20.     echo "<form method='post' action=''>";
  21.  
  22.  
  23.     if($result) {
  24.      while ($row = mysql_fetch_array($result))
  25.     {
  26.  
  27.      $qid = $row["qid"];
  28.     $questions = $row["question"];
  29.     $opt1 = $row["choice1"];
  30.     $opt2 = $row["choice2"];
  31.     $opt3 = $row["choice3"];
  32.     $opt4 = $row["choice4"];
  33.     $answer = $row["answer"];
  34.  
  35.  
  36.  
  37.         echo"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  38.      echo '<p>'.$x.'.&nbsp;&nbsp;&nbsp;' . $questions . '</p>
  39.                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  40.                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
  41.  
  42.                <input type="radio" name="choice[' . $qid . ']" value="' . $opt1 .'"/>A.' . $opt1 . '
  43.                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
  44.  
  45.                <input type="radio" name="choice[' . $qid . ']" value="' . $opt2 .'"/>B.' . $opt2 . '
  46.                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
  47.  
  48.                <input type="radio" name="choice[' . $qid . ']" value="' . $opt3 .'"/>C.' . $opt3 . '
  49.                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
  50.  
  51.                <input type="radio" name="choice[' . $qid . ']" value="' . $opt4 .'"/>D.' . $opt4 . '
  52.                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    
  53.                <input type="hidden" name="question_id" value"'.$qid.'"/>
  54.                <input type="hidden" name="rightanswer[' . $qid . ']" value="' . $answer . '" />';
  55.                echo "<input type='submit' name='next' value='next'><br><br>";
  56.     }
  57.  
  58.  
  59.     $b=$a+1;
  60.     echo "<input type='hidden' value='$b' name='a'>";
  61.     echo "<input type='hidden' value='count' name='count'>";
  62.  
  63.     echo "</form>";
  64.     }
  65.  
  66. ?>
  67. <?php
  68.  
  69.     if(isset($_POST['choice']))
  70.     {
  71.         //$_SESSION['question_id'] = array();
  72.         //$id = $_POST['question_id'];
  73.  
  74.  
  75.         $_SESSION['score'] = (!$_SESSION['score']) ? 0 : $_SESSION['score'];
  76.  
  77.         foreach($_POST['choice'] as $qid => $answer)
  78.          if($_POST['rightanswer'][$qid] == $answer) {
  79.  
  80.         $_SESSION['score']++;
  81.  
  82.         }
  83.  
  84.  
  85.         echo " Score is " . $_SESSION['score'];
  86.         }
  87.  
  88.  
  89. ?>
  90.  
  91.  
Jan 26 '12 #1
Share this Question
Share on Google+
1 Reply


dlite922
Expert 100+
P: 1,584
I think the way you're going about it is wrong.

Here's how your architecture should look like:

Your "answer" table must hold the following info:
id, PK
quizID,
studentID,
questionID,
answerID or answerValue

When you present the questions, the question page, the URL will look like this:

quiz.php?quizID=1&questionID=34

studentID should already be in your session.

quiz.php will validate the student and make sure the student can take that test at the time. Then validate the question and present the current answerValue (if any) if it's blank, obviously the student hasn't answered it.

The next button should point to the next question in the sequence for that quiz. (depending on your DB, the questionID may not be the exact next ID 35, so you must also get this "nextQuestionID" value and the next button should go to a URL like:

quiz.php?quizID=1&questionID=74

If you want to have a "previous" button, then you need the prevQuestionID for that quiz as well and assign it o the this button, if not the student can simply click the browser's Back button.

the nextbutton should submit the form using POST. The form will contain the answerValue and questionID and quiz.php should look for these values in POST and insert them along with quizID and studentID into the answer table then present the questionID that's in the URL ($_GET['questionID']) and present the next question.

Then repeat the above.

Hope this helps you,

Dan
Jan 28 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.