if you folks can help me out with this problem. im trying to build a questionere with php.
table:
id_quiz question answer
82 q1 1
83 q2 4
84 q3 1
85 q4 4
ive got two arrays;
1. $all_ids = [82,83,84,85]
2. $all_answers = [1,4,1,4] -> if the answer is correct then count them.
my question is how do compare those two array with database?
- '$all_ids' is the id for database table.
- '$all_answers' is the answers.
$all_ids[82] == ($all_answer[1] -> compare answer with database for id 82)
$all_ids[83] == ($all_answer[4] -> compare answer with database for id 83)
$all_ids[84] == ($all_answer[1] -> compare answer with database for id 84)
my current code seem not working:
$total_correct = 0;
foreach ($all_ids as $ids){
$check = $db->query("SELECT * FROM quiz WHERE id_quiz='$ids' ");
$row = $check->fetch_assoc();
foreach($all_answers as $answers) {
if($row['answer'] == $answers) {
$total_correct++;
}
}
}
i hope my question makes any sense :)
SELECT * FROM quiz WHERE id_quiz IN (1,2,3,4)($all_ids) and iterate through the results, like Matt explained.