Reputation: 65
I have a simple quiz which I searched for on GitHub to learn for myself how a quiz works. As this quiz works only with one correct answer per question, I have tried to see if I can make it work with multiple correct answers per question but I had no luck.
I have searched google and what I found didn't help me at all.
This is the code that holds the question with the answers
//Get total number of questions
$query = "SELECT * FROM questions";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total=$results->num_rows;
// Get Question
$number = (int) $_GET['n'];
$query = "SELECT * FROM questions WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$question = $result->fetch_assoc();
// Get Choices
$query = "SELECT * FROM choices WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);
<div class="container">
<div class="current">Question <?php echo $number; ?> of <?php echo $total; ?> and correct <?=$_SESSION['score']?></div>
<p class="question">
<?php echo $question['question']?>
</p>
<form method="post" action="process.php">
<ul class="choices">
<?php foreach($choices as $row){ ?>
<li>
<input name="choice" type="radio" value="<?php echo $row['id']; ?>" />
<?php echo $row['text']; ?>
</li>
<?php } ?>
</ul>
<input type="submit" value="submit" />
<input type="hidden" name="number" value="<?php echo $number; ?>" />
</form>
</div>
And here is the code which verifies the answers
if($_POST){
$number = $_POST['number'];
$selected_choice = $_POST['choice'];
$next = $number + 1;
$total = 4;
//Get total number of questions
$query = "SELECT * FROM questions";
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;
//Get correct choice
$q = "SELECT * FROM choices WHERE question_number = $number AND is_correct = 1";
$result = $mysqli->query($q) or die($mysqli->error.__LINE__);
$row = $result->fetch_assoc();
$correct_choice = $row['id'];
//compare answer with result
if($correct_choice == $selected_choice) {
//$_SESSION['score']++;
$_SESSION['score'] = $_SESSION['score'] + 1;
}
if($correct_choice !== $selected_choice) {
//$_SESSION['score']++;
$_SESSION['score1'] = $_SESSION['score1'] - 1;
}
if($number == $total) {
header("Location: final.php");
exit();
} else {
header("Location: question.php?n=".$next);
}
}
Any help would be much appreciated.
Upvotes: 0
Views: 1047
Reputation: 26450
First of all, you need to make the choice
inputs checkboxes instead of radio buttons. Radio-buttons can only be selected once, while you can have multiple checkboxes. You also need to be able to submit more than one at once with the same name, so it must be an array of checkboxes. So the selector should be
<input name="choice[]" type="checkbox" value="<?php echo $row['id']; ?>" />
instead of
<input name="choice" type="radio" value="<?php echo $row['id']; ?>" />
Then when you receive it, $_POST['choice']
in PHP is an array and not a single value.
You can perform a COUNT()
query to see how many correct answers there are, then see if you fetched them all by checking the number of rows returned by the number of the count - if they are equal, they got them all right!
I'll also use a prepared statement for this (which you should be using for all your querieis).
We can create a dynamically amount of placeholders using rtrim(str_repeat("?,", count($a)), ",");
, and pass values using the splat-operator ...
.
//Get correct choice
$q = "SELECT id, COUNT(id) as cnt
FROM choices c
WHERE question_number = ?
AND is_correct = 1
AND id IN (".rtrim(str_repeat("?,", count($_POST['choice'])), ",").")
GROUP BY question_number";
$stmt = $mysqli->prepare($q);
if ($stmt) {
$stmt->bind_param("s".str_repeat("s", count($_POST['choice']), $number, ...$_POST['choice']);
$stmt->execute();
$stmt->bind_result($id, $count);
$result = $stmt->fetch();
if ($stmt->num_rows == $count) {
echo "You got all the right answers!";
} else {
echo "At least one answer was incorrect.";
}
} else {
echo "Error performing query";
trigger_error($stmt->error);
trigger_error($mysqli->error);
}
Upvotes: 1
Reputation: 48
SELECT * FROM choices WHERE question_number = $number AND is_correct = 1
That line, tells us that in your choices table, their is a column, is_correct that if set 1, then it marks the choice as a valid answer.
I don't understand, why you cant have multiple of choices, set to have the value 1.
Since you are going to have more than 1 answer,then you could have 1 or more choices. So the returned result is going to be an array.
$boolAnsCorrect = false;
if ($row) {
while($srow = mysqli_fetch_array($row)) {
$correct_choice = $srow['id'];
if($correct_choice == $selected_choice) {
$boolAnsCorrect = true;
break;
}
}
if ($boolAnsCorrect) {
$_SESSION['score'] = $_SESSION['score'] + 1;
} else {
$_SESSION['score1'] = $_SESSION['score1'] - 1;
}
}
else {
// handle error, assuming that a quiz, MUST have ATLEAST 1 answer.
}
Upvotes: 0