0

Hello,I'm doing a sample quiz for students.I have 25 questions displaying 1 question per page with a next button.I also manage to count its correct answers.And if all the questions have already shown from database,it prints that there are no more questions left.But I want to keep track of the questions which have no answers.So that if a student, wants to skip a question, he/she can go back to it.I hope you can help me.Here is my code.

<?php
session_start();
	include('config.php');
	
	if(!isset($a))
	{
		$a=0;
		//unset($_SESSION['score']);
	}
	
	if(isset($_POST['next'])) {
		$a=$_POST['a'];
		if($_POST['a']==25) {
			echo "There are no more questions in this part";
			}
	}
	
	$sql1="SELECT * FROM questionpart2 ORDER by qid LIMIT 1 OFFSET $a";
	$result=mysql_query($sql1);
	$num = mysql_num_rows($result);
	echo "<form method='post' action=''>";
	
	
	if($result) {
	 while ($row = mysql_fetch_array($result))
	{
	
	 $qid = $row["qid"];
    $questions = $row["question"];
    $opt1 = $row["choice1"];
    $opt2 = $row["choice2"];
    $opt3 = $row["choice3"];
    $opt4 = $row["choice4"];
    $answer = $row["answer"];
	
	
	
		echo"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
     echo '<p>'.$x.'.&nbsp;&nbsp;&nbsp;' . $questions . '</p>
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	
			   
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt1 .'"/>A.' . $opt1 . '
			   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	
			   
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt2 .'"/>B.' . $opt2 . '
			   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	
			   
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt3 .'"/>C.' . $opt3 . '
			   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	
			   
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt4 .'"/>D.' . $opt4 . '
			   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;	
			   <input type="hidden" name="question_id" value"'.$qid.'"/>
			   <input type="hidden" name="rightanswer[' . $qid . ']" value="' . $answer . '" />';
               echo "<input type='submit' name='next' value='next'><br><br>";
	}
	
	
	$b=$a+1;
	echo "<input type='hidden' value='$b' name='a'>";
	echo "<input type='hidden' value='count' name='count'>";
	
	echo "</form>";
	}
	
?>
<?php

	if(isset($_POST['choice']))
	{
		//$_SESSION['qid'] = $_POST['question_id'];
		
		$_SESSION['score'] = (!$_SESSION['score']) ? 0 : $_SESSION['score'];
		
		foreach($_POST['choice'] as $qid => $answer)
     	if($_POST['rightanswer'][$qid] == $answer) {
		
		$_SESSION['score']++;
		
		}
		
		
		echo " Score is " . $_SESSION['score'];
		}
?>

Edited by Ezzaral: Updated code tag.

4
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by diafol
0

I think this is more a question for the WebDevelopment fora (I think there is a PHP one).
never used PHP myself, but have you considered storing it in the Session, or keeping cookies?

0

The easiest way to 'feed forward' data is to put it in <input type='hidden'> elements. Then each succeeding page will have all the previous answers. Put the current andswer in a normal <input> and all the others in a hidden input. If you are persistent, you can figure out how to pass them back to the server in an array (but it can be a bear to get the syntax right). Essentially, the result would be that PHP would save $_POST to $answers, which is an array; you'd step through this array while generating the mostly hidden inputs and the one normal input. The name of each input would be an array reference instead of a plain scalar reference.

0

Why not store answers in a DB? When you go to a specific question, query the db for and answer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.