jessicabo Posted July 31, 2007 Share Posted July 31, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62682-quiz-with-mysql-and-php-help/ Share on other sites More sharing options...
deadimp Posted July 31, 2007 Share Posted July 31, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62682-quiz-with-mysql-and-php-help/#findComment-312101 Share on other sites More sharing options...
jessicabo Posted July 31, 2007 Author Share Posted July 31, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/62682-quiz-with-mysql-and-php-help/#findComment-312120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.