1

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?

  1. '$all_ids' is the id for database table.
  2. '$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 :)

8
  • 2
    I'm somewhat confused. You are querying the database over and over when you shouldn't need to. You should be able to just get the entire table and create an array out of it. Then loop through the ids checking the answers. The other thing you can do is build the query to include all the questions you need so you only query once and then compare arrays using php array_diff probably. Commented Sep 14, 2011 at 12:07
  • Query the database ONCE. SELECT * FROM quiz WHERE id_quiz IN (1,2,3,4) ($all_ids) and iterate through the results, like Matt explained. Commented Sep 14, 2011 at 12:11
  • @cularis: I'm kind of confused as to why your comments aren't answers. :) Commented Sep 14, 2011 at 12:18
  • @cularis thanks. bit confused. can you write the short answer please :). dont quite get what u meant Commented Sep 14, 2011 at 12:20
  • give us the table definition and I'll write the php/sql for you Commented Sep 14, 2011 at 12:29

2 Answers 2

1

Change your two answers arrays into one like this:

$answers[82] = 1
$answers[83] = 4

foreach($answers as $id => $ans)
{
$sql = "select * from quiz where id_quiz = $id"
db->query($sql)
$row = $check->fetch_assoc();
if($row['answer'] == $ans)
{
  totalcorrect++
}

}

Thats a quick answer, not sure if you need more detail?

Sign up to request clarification or add additional context in comments.

3 Comments

thanks alot :). but how do i split these two array $all_ids = [82,83,84,85], $all_answers = [1,4,1,4], into $ids[82] = 1 ?? or do i need to merge them together??
thanks :) i sorted it out. i used array_combine.. thanks for the idea tho.. appriciated ;)
No Problem, sorry didn't see the comment until now!
1

Use php's implode function to glue the ID's together: implode(",", $all_ids); Then u can use that to create one DB query (as cularis said) and iterate through those results to check (like matt said)

more info:

1 Comment

so i tried: '$check = $db->query("SELECT * FROM quiz WHERE id_quiz IN ($all_ids) ");' and use 'while loop'??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.