halosinfire Posted February 18, 2013 Share Posted February 18, 2013 Hi everyone, this is my first post here. I need some help with a PHP/MySQL multiple choice quiz that I'm making for a website. I have succesfully created a CMS to create new quizzes and then add questions and answers for it to a MySQL database table. I have written the code to loop through and display the questions and possible answers with radio buttons. I'm having trouble with the logic of correcting the quiz however. I've been searching online and trying different things for 2 days, but nothing I've come up across has helped me. I don't want to just download someone else's multiple choice quiz package, I want to be able to figure this out on my own (well, with your help!) because once I get the logic down, that's only half of what I need to do. No one else's pre-written package suits my needs. Well here goes. I have a CMS that creates a quiz and then adds questions and answers. The database table has the following fields: [id] [quizName] [question] [answerA] [answerB] [answerC] [answerD] [correctAnswer] On my quiz page, I have a dropdown box to select which quiz you want to take. Then I query the database looking for questions which have that quiz name. Here's my code: <form method="post" action="quizzes.php"> <?php $questionNumber = 1; $query = mysql_query("SELECT * FROM quizzes WHERE quizName = '$quizName'"); while ($result = mysql_fetch_array($query)) { ?> <p> <?php echo $questionNumber . ") " . $result['question'] . "<br>"; ?> <input type="radio" name="answer<?php echo $result['id'] ?>" value="A"> <?php echo $result['answerA']; ?> <br> <input type="radio" name="answer<?php echo $result['id'] ?>" value="B"> <?php echo $result['answerB']; ?> <br> <input type="radio" name="answer<?php echo $result['id'] ?>" value="C"> <?php echo $result['answerC']; ?> <br> <input type="radio" name="answer<?php echo $result['id'] ?>" value="D"> <?php echo $result['answerD']; ?> <br> </p> <?php $questionNumber +=1; } ?> <input type="submit" name="checkQuiz" value="Check Quiz"> </form> This displays the questions and answers nicely. I know I need some kind of unique identifier for each question as I'm displaying them all from a single while loop. Then when the submit button is clicked, I need to get the answer selected for each of the questions, and compare it to the correctAnswer field in the database for that question. Nothing I've tried has worked. Can anyone help me out. Even if you give me some pseudo code, I'll code it up myself and see if it works. If it doesn't, I'll paste it here to show you what I've done. Any help would be greatly appreciated! I spent about 20 hours on this to no avail and nothing I've found searching Google has helped me either. Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/ Share on other sites More sharing options...
computermax2328 Posted February 18, 2013 Share Posted February 18, 2013 Hey! Tough struggles man. Alright I am a little busy right now because I am a student in college, but I have a possible solution for you. First question, do you want all the questions on the same page or do you want a dynamic page for each question? I will be back later to see your answer. This will be a good challenge. Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413147 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 I know I need some kind of unique identifier for each question as I'm displaying them all from a single while loop. Isn't that what the id is for name="answer[<?php echo $result['id']?>]" Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413160 Share on other sites More sharing options...
halosinfire Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) Hey computermax, I would like to display all of the questions on the same page. The way I have the loop set up right now, they all display on the same page. What I can't get to work is the logic when the submit button is pushed. Any way you can help me would be greatly appreciated! Barand - Yes, that's what the ID is for. I just don't know how to loop each question once the submit button is pushed. For the logic, I've tried lots of different things. Right now what I have, which doesn't work, is the code below. I'm starting off small, just counting how many correct and incorrect answers there are. Later on I will expand that to show the questions again and display either a check mark if the answer was right, or an X if the answer was wrong. But that's getting ahead of myself. Right now I just want to be able to count the number of correct and incorrect answers. Small steps first! if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; foreach ($_POST['answer'] as $answerID) { $query = ("select correctAnswer from quizzes where id = '$answerID'"); while ($result = mysql_fetch_array($query)) $ans = $result['correctAnswer']; // get the correct answer A, B, C, or D if ($_POST['answerID'] == $ans) $correctAnswers +=1; if ($_POST['answerID'] != $ans) { $wrongAnswers +=1; } } } And in the body: <form method="post" action="quizzes.php"> <?php $questionNumber = 1; $query = mysql_query("SELECT * FROM quizzes WHERE quizName = '$quizName'"); while ($result = mysql_fetch_array($query)) { ?> <p> <?php echo $questionNumber . ") " . $result['question'] . "<br>"; ?> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="A"> <?php echo $result['answerA']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="B"> <?php echo $result['answerB']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="C"> <?php echo $result['answerC']; ?> <br> <input type="radio" name="answer[<?php echo $result['id'] ?>]" value="D"> <?php echo $result['answerD']; ?> <br> </p> <?php $questionNumber +=1; } ?> <input type="submit" name="checkQuiz" value="Check Quiz"> </form> The flawed logic above shows me getting all the questions right, even when I purposely choose the wrong answers! Again, any help anyone could give me would be greatly appreciated! Edited February 18, 2013 by halosinfire Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413171 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 (edited) If you use my version of the code you will get something like this posted $_POST['answer'] = array ( 1 => 'A', 2 => 'C', 3 => 'D', ); showing Q ids and given answer for each edit so foreach ($_POST['answer'] as $qid = $ans { // process it } Edited February 18, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413172 Share on other sites More sharing options...
halosinfire Posted February 18, 2013 Author Share Posted February 18, 2013 If you use my version of the code you will get something like this posted $_POST['answer'] = array ( 1 => 'A', 2 => 'C', 3 => 'D', ); showing Q ids and given answer for each Hi Barand, I have changed that portion of the code as shown in the second post. How do I pull them out of that array one by one and check them against the correct answer in the database? By the way, the IDs aren't necessarily in order like 1, 2, 3, as the CMS allows questions to be added and deleted to each quiz arbitrarily. For example, the ID of question number one could be 6, the ID for question number 2 might be 10, and so on. I'm not sure if that makes a difference. Again, many thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413173 Share on other sites More sharing options...
halosinfire Posted February 18, 2013 Author Share Posted February 18, 2013 Well, i noticed one thing, I forgot to put mysql_query in this line of the logic: $query = mysql_query("select correctAnswer from quizzes where id = '$answerID'"); However, it still doesn't work. Gives me a 100 every time! Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413176 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 (edited) $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT id, correctAnswer FROM quizzes WHERE id IN ($idList) "; $res = mysql_query($sql) ; while (list($id, $correct) = mysql_fetch_row($res)) { if ($correct == $_POST['answer'][$id]) { echo "$id correct<br>"; } else { echo "$id wrong<br>"; } } Edited February 18, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413181 Share on other sites More sharing options...
halosinfire Posted February 18, 2013 Author Share Posted February 18, 2013 Hey Barand - Wow that definitely worked. I'm going to edit it a little and post up what I got. I'm still only about halfway to what I want the final result to look like! I'll post again in a couple of minutes! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413184 Share on other sites More sharing options...
halosinfire Posted February 18, 2013 Author Share Posted February 18, 2013 (edited) Okay, the first step is working. Thank you barand. I changed your code to count the right and wrong answers instead of echoing "correct" and "wrong". Here's what I got: if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT id, correctAnswer FROM quizzes WHERE id IN ($idList) "; $res = mysql_query($sql) ; while (list($id, $correct) = mysql_fetch_row($res)) { if ($correct == $_POST['answer'][$id]) { $correctAnswers +=1; } else { $wrongAnswers +=1; } } } Then in the body I display the score like this: <p> <?php $numberOfQs = $correctAnswers + $wrongAnswers; $score = round(($correctAnswers / $numberOfQs) * 100); ?> Correct Answers: <?php echo $correctAnswers; ?> <br> Wrong Answers: <?php echo $wrongAnswers; ?> <br> Score: <?php echo $score; ?> </p> Now the next step, which seems quite difficult to me, is to redisplay the quiz, this time without the radio buttons. I want to display a check mark image next to the questions that were answered correctly, and an X image next to the questions that were incorrect. I know I would need to do something like have a PHP variable holding the URL for the images. I.E $img = "correct.gif"; for a correct answer and $img = "incorrect.gif"; for incorrect answers. Then when I redisplay the quiz, I would do something like this: <?php $questionNumber = 1; $query = mysql_query("SELECT * FROM quizzes WHERE quizName = '$quizName'"); while ($result = mysql_fetch_array($query)) { ?> <p> <?php echo $questionNumber . ") " . $result['question'] . "<img src = '" . $img . "'><br>"; ?> <?php echo $result['answerA']; ?> <br> <?php echo $result['answerB']; ?> <br> <?php echo $result['answerC']; ?> <br> <?php echo $result['answerD']; ?> <br> </p> <?php $questionNumber +=1; } ?> How can I pass the $img variable down to each of the corresponding questions when redisplaying the quiz? Edited February 18, 2013 by halosinfire Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413188 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 perhaps <?php if(isset($_POST['checkQuiz'])) { $correctAnswers = 0; $wrongAnswers = 0; $questions = ''; $idList = join (',', array_map('intval', array_keys($_POST['answer']))); $sql = "SELECT id, question, answerA, answerB, answerC, answerD, correctAnswer FROM quizzes WHERE id IN ($idList) "; $res = mysql_query($sql) ; $qno = 1; while (list($id, $q, $a, $b, $c, $d, $correct) = mysql_fetch_row($res)) { if ($correct == $_POST['answer'][$id]) { $correctAnswers +=1; $img = 'correct.gif'; } else { $wrongAnswers +=1; $img = 'incorrect.gif'; } $questions .= "<tr valign='top'> <td>$qno</td> <td>$q<ul><li>$a</li><li>$b</li><li>$c</li><li>$d</li></ul></td> <td><img src='$img'></td> </tr>\n"; } } $numberOfQs = $correctAnswers + $wrongAnswers; $score = round(($correctAnswers / $numberOfQs) * 100); ?> <p> Correct Answers: <?php echo $correctAnswers; ?> <br> Wrong Answers: <?php echo $wrongAnswers; ?> <br> Score: <?php echo $score; ?> </p> <table> <?php echo $questions?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413194 Share on other sites More sharing options...
Barand Posted February 18, 2013 Share Posted February 18, 2013 Oops! Forgot to to increment $qno inside the while() loop :-\ Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413196 Share on other sites More sharing options...
halosinfire Posted February 19, 2013 Author Share Posted February 19, 2013 Barand - You are awesome. I just woke up (i'm on Asian time). I'll give this a try now and see how it goes! Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413235 Share on other sites More sharing options...
Barand Posted February 19, 2013 Share Posted February 19, 2013 I'm just off to bed. Until tomorrow.. Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413236 Share on other sites More sharing options...
halosinfire Posted February 19, 2013 Author Share Posted February 19, 2013 It works! Barand you are truly a guru! I can't tell you how much I appreciate your time and your help! I have some other stuf to do with it now but I'm sure I can manage on my own. Thank you again for you help! Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1413237 Share on other sites More sharing options...
designandcode Posted December 2, 2013 Share Posted December 2, 2013 Hi halosinfire were you able to complete the code. if possible can you post it ? I have been banging my head against the wall in dealing with this. Quote Link to comment https://forums.phpfreaks.com/topic/274627-phpmysql-multiple-choice-quiz-logic/#findComment-1460962 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.