I need help suppressing a warning that I am getting from a foreach()
Warning: Invalid argument supplied for foreach() in
/sitename/pages/admin/questions/index.php
I am getting the result I want. All I need is to remove the warning…
This is my code:
for($i = 1; $i <= $RESULTS_count; $i++){
$rawResult = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM ".WPSQT_TABLE_RESULTS." WHERE id = $i"),ARRAY_A);
$rawResult['sections'] = unserialize($rawResult['sections']);
foreach($rawResult['sections'] as $result_sections){
if($result_sections['answers'][1]['mark'] == 'correct') $correct_answer++;
}
}
echo $correct_answer/$RESULTS_count;
If I take the code out of the FOR loop and replace WHERE id = $i with WHERE id = 1 it will work… Could the problem be FOREACH does not like being in FOR loop?
What do you think I should do?
EDIT: I think the warning I was getting was caused by the fact that I deleted the result with ID 2, so when loop over the table rows one of them is empty and that is way I am getting the Warning…
You should ensure the data is an array first or typecast it to an array.
Is array:
Typecast: