PDA

View Full Version : invalid argument


AN-net
04-12-2004, 03:59 PM
im getting invalid argument for some reason:
heres the error

Warning: Invalid argument supplied for foreach() in /home/blah/blah/blah/quiz.php on line 140



137: $fquestions= $DB_site->query("SELECT quiz_questions.question,quiz_questions.question_id ,quiz_answers.answer,quiz_answers.correct FROM ".TABLE_PREFIX."quiz_questions LEFT JOIN quiz_answers ON (quiz_questions.question_id=quiz_answers.question_ id) WHERE quiz_questions.quiz_id='".$_REQUEST['q']."' AND quiz_questions.active='1' AND quiz_answers.active='1'");
138: while($question= $DB_site->fetch_array($fquestions))
139: {
140: foreach($question['answer'] as $answer)
141: {
142: exec_switch_bg();
143: eval('$ansbits .= "' . fetch_template('quiz_correctansbits') . '";');
144: }

Xenon
04-12-2004, 04:07 PM
are you sure the variable $question['answer'] is an array?
it has to be one :)

AN-net
04-12-2004, 04:37 PM
well isnt fetch_array making the results into an array?

Xenon
04-12-2004, 04:44 PM
$question is an array, but you are using $question['answer'] as argument

Xenon
04-12-2004, 04:44 PM
it seems like you should just don't use foreach at all, and just use the fetch array loop :)

AN-net
04-12-2004, 08:14 PM
but it will only display one answer for each question even though there might be for five or six of them.

Xenon
04-12-2004, 08:51 PM
as i said, you have to make sure what type of variable is $question['answer']
if it's an array you can use foreach, if not, then you have tu use something else to spread it on your board.

I can just explain what lead to the error, not what exactly the vars are :)

AN-net
04-12-2004, 11:15 PM
could u give me a suggestion on how to make it that for each answer?
plz oh mighty php intelligent one:)

Xenon
04-13-2004, 12:39 PM
well, personally i'd put the answers into an array, and then serialize that array when storing into the db.

later when you catch it, you have to unserialize it again.

or you can use the way vb devs did it with poll questions by seperating the strings via ||| and then using implode and explode functions.

the serialize function should be explained in the tips and trix forums here, the implode version is used often in vb and should be quite selfexplaining

you may also look up php.net for infos about.