Jump to content

QUIZ with MySQL and PHP HELP!!!!


jessicabo

Recommended Posts

Here is the deal, I created an online quiz for a client using MySQL and PHP. The quiz is multiple choice and at the end displays the results. That part works. I have also created a simple registration system for the users (in this case employees). They register before taking the quiz. I don't need a login system.

 

What I need the whole thing to do is after the user registers and takes the quiz. The users information is combined with their score and is emailed to the client (in this case employer).

 

I created two tables. One for the quiz (holds all the questions and answers) and another for the users (holds all info as well as a foreign key (idquiz_user) to connect to the quiz table id (id).

 

Here is the code for the quiz:

 

<?php
include("contentdb.php");
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);

if (!$submit) {


echo "<form method=post action=$PHP_SELF>";

while ($row = mysql_fetch_array($display)) {

$id = $row["id"];
$question = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$answer = $row["answer"];

echo "<h1>$question</h3><br>";
echo "<p><input type=radio name=q$id value=\"$opt1\"> $opt1</p><p><input type=radio name=q$id value=\"$opt2\"> $opt2 </p><p><input type=radio name=q$id value=\"$opt3\"> $opt3 </p><p> <input type=radio name=q$id value=\"$opt4\"> $opt4 </p>";

}


echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";

}

elseif ($submit) 

{

$score = 0;
$total = mysql_num_rows($display);
	while ($result = mysql_fetch_array($display)) 


	{

		$answer = $result["answer"];
		$q = $result["q"];

	if ($$q == $answer) 
	{
	$score++; 
	}

}

echo "<h2>You scored $score out of $total</h2>";
echo "<h3>";

if   ($score == $total) {
echo "Congratulations! You got every question right!";
}
elseif ($score/$total < 0.34) {
echo "Oh dear. Not the best score, but don't worry, it's only a quiz.";
}
elseif ($score/$total > 0.67) {
echo "Well done! You certainly know your stuff.";
}
else {
echo "Not bad - but there were a few that caught you out!";
}

echo "</h3>";

echo "<p>Thank you for watching the video and taking the test. You can watch this video anytime.</p><p>If you have any questions about safety feel free to ask your supervisor or call 1.800.ARAMARK.</p><p><h1>Here are the answers:</h1>";

echo "<p>";
$display = mysql_query("SELECT * FROM $table ORDER BY id",$db);
while ($row = mysql_fetch_array($display)) {

$question = $row["question"];
$answer = $row["answer"];
$q = $row["q"];

echo "<h1>>$question</h1>";

if ($$q == $answer) 
	{
	echo "<p>»you answered ${$q}, which is correct</p><br>";
	}
elseif ($$q == "") {
echo "<p>»you didn't select an answer. The answer is $answer</p><br>";
}
else {
echo "<p>»you answered ${$q}. The answer is $answer</p><br>";
}

}
echo "</p>";
}
?>

 

As you can see the value of $score is the variable I need to pass on to the users table. How can I do this within the above code as well as send the data to the client?

 

The registration is on a separate page I assume I need to set some sort of session variable, how to do this as well?

 

Thanks

Link to comment
Share on other sites

Have a results table, where each row represents a taking of the quiz (er...), which contains the score (and possibly which answers were incorrect) and the id of the user. Whenever you update this information after the quiz is done, use some of the mail functions to send the results to the client.

Then again, if you want to minimize on your table usage, and the users are only going to take the test once, you can store that data along with the user information.

Link to comment
Share on other sites

What would be the necessary script to get the variable $score into the users table under 'score' while making sure it plugs it in after it's been calculated and keeping it with the same user. Any tutorials or example of code would be a big help thanks!

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.