Jump to content

How to generate answers from a multiple choice quiz using PHP/MySQL


dave378

Recommended Posts

I'm working on a project that users can enter their name to complete a multiple choice quiz, while administrators can log in to add new questions and edit and delete existing questions.

 

I've created a multiple choice quiz in PHP and MySQL that loops questions on the same page, instead of multiple pages. Basically, you click on the submit button to go to the next set of questions, but on the same page. The questions are data retrieved from a database I created in PhpMyAdmin. The problem now is I have no idea what code to refer to when trying to calculate answers in the final results page, so I was wondering if someone could help me out.

 

Underneath is the code I have for my quiz.php page, and now I want to calculate how many questions I have answered correctly. I'd really appreciate the help. :)

<?php
//CODE FOR QUIZ.PHP
//Code retrieves the name that the user typed in the text-field.
session_start();
require 'includes/connection.php';
$name = $_REQUEST['name'];
$_SESSION['name'] = $name;
//User's name is displayed above the quiz.
echo 'Welcome, '.$name;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>World War II Quiz</title>
</head>
<body>


<?php
//Start with question ID
if (isset($_GET['id']))
{
	
	
	//get question id from user and store
	$question_id = $_GET['id'];
}
else
{
	$question_id = 1;
	//no question id submitted
	
	//unset session data
	unset($_SESSION['questions']);
	unset($_SESSION['total']);
	
}

//If the questions array has been built
if (isset($_SESSION['questions']))
{
	//has the user selected a radio button
	if (isset($_REQUEST['value']))
	{
		//check for correct
		if ($_SESSION['answer'] == $_REQUEST['value'])
		{
			//correct answer	
			$_SESSION['total']++;			
			
		}	
	}
	else
	{
		//No value inputted from the user 
		echo "hey bozo click a button";
		$question_id--;
	}
}
//Questions array is not been built
else
{
	//get a list of question id's
	$sql = "SELECT question_id FROM questions ORDER BY question_id ASC";
	$results = mysql_query($sql,$conn);

	$stringtemp = "";
	$maxtemp = 1;

	while ($row = mysql_fetch_assoc($results))  //More results
	{
		
		$stringtemp = $stringtemp." ".$row['question_id'];
		$maxtemp++;
	}
	
	//Create question id array pulled from database
	$_SESSION['questions'] = explode(" ",$stringtemp);
	
	//Max value of the array
	$_SESSION['max']=$maxtemp;

}





$sql = "select * from questions where question_id =".$_SESSION['questions'][$question_id];
$results = mysql_query($sql,$conn);
$row = mysql_fetch_array($results); 

//Store correct answer for next pageload	
$_SESSION['answer'] = $row['correct'];


$nextid=$question_id+1;

if ($nextid == $_SESSION['max'])
{
	//Final question
	$next = "results.php";
	
}
else
{
$_SESSION['name'] = $name;
	//Not the final question
	$next = "quiz.php?id=".$nextid;
	
}


echo "<form action=\"".$next."\" method=\"post\" name=\"form\" >

<p>".$row['question_text']."</p>
<input type=\"radio\" name=\"value\" value=\"1\">".$row['answer1']."<br>
<input type=\"radio\" name=\"value\" value=\"2\">".$row['answer2']."<br>
<input type=\"radio\" name=\"value\" value=\"3\">".$row['answer3']."<br>
<input type=\"radio\" name=\"value\" value=\"4\">".$row['answer4']."<br>
<input class=\"submit\" type=\"submit\" value=\"Submit\"/>
</form>
</body>
</html>
";

?>
Link to comment
Share on other sites

 


I want to calculate how many questions I have answered correctly.

 

It looks like you are storing the given answers in a session. Don't do that, put them in the database (after all, you want to know what people filled in, sessions are deleted after a while)

When the give answers are in the database you can let the database do pretty much all of the work.

Selecting the next question can be done by doing a JOIN between your questions table and user-answer table, the first question that has no answer by the current user is  the next question to ask.

 

Counting the total correct answers can also be done by joining your questiontable (or answers table, depending on your datamodel) with the user-answers and counting the number of answers that match, like:

SELECT COUNT(*) AS correctanswers

FROM answers

INNER JOIN user_answers ON answers.question_id = user_answers.question_id AND answers.thisisthecorrectanswer = true

WHERE quiz_id = 'wwII'

AND username = $username; // dont forget to escape the username.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.