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.

	if(isset($_POST['next'])) {
		if($_POST['a']==25) {
			echo "There are no more questions in this part";
	$sql1="SELECT * FROM questionpart2 ORDER by qid LIMIT 1 OFFSET $a";
	$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 '<p>'.$x.'.&nbsp;&nbsp;&nbsp;' . $questions . '</p>
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt1 .'"/>A.' . $opt1 . '
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt2 .'"/>B.' . $opt2 . '
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt3 .'"/>C.' . $opt3 . '
               <input type="radio" name="choice[' . $qid . ']" value="' . $opt4 .'"/>D.' . $opt4 . '
			   <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>";
	echo "<input type='hidden' value='$b' name='a'>";
	echo "<input type='hidden' value='count' name='count'>";
	echo "</form>";

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

Recommended Answers

All 3 Replies

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?

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.

Member Avatar

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.