Jump to content

PHP/MySQL Multiple Choice Quiz Logic


halosinfire

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

Link to comment
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.

Link to comment
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!

Edited by halosinfire
Link to comment
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
}

Edited by Barand
Link to comment
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.

Link to comment
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>";
}
}

Edited by Barand
Link to comment
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?

Edited by halosinfire
Link to comment
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>

Link to comment
Share on other sites

  • 9 months later...
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.