Jump to content

Archived

This topic is now archived and is closed to further replies.

halosinfire

PHP/MySQL Multiple Choice Quiz Logic

Recommended Posts

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. :(

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

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']?>]"

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
$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>";
}
}

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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>

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

×
×
  • 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.