Jump to content


Photo

Invalid argument supplied for foreach()


  • Please log in to reply
2 replies to this topic

#1 kutuitem

kutuitem
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 March 2006 - 09:13 AM

Hi All,

I get this warning (Invalid argument supplied for foreach()). Following is my code:

foreach (sql_get('vms_enrollments', 'student_id', $_SESSION['id']) as $k1 => $v1)
{
$db = new DbConn();

foreach (sql_get('vms_tests', 'tr_mod_id', $v1['tr_mod_id']) as $k2 => $v2) <-- this line triggers the error
{
$sql = "select count(*) ".
" from vms_test_results".
" where test_id = ".$v2['test_id'].
" and student_id = ".$_SESSION['id'];

$res = $db->query($sql) or
die('Could not execute:<br /><br />'.$sql);

$row = mysql_fetch_row($res);

if ($row[0] <= 0)
$tests[] = $v2;
}
}


If I change $v1['tr_mod_id'] to a number. Then no warning is issued. I'm really confused. Is this PHP bug?

#2 deonh

deonh
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 March 2006 - 09:27 AM

foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

So make sure the function sql_get returns an array.

#3 kutuitem

kutuitem
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 March 2006 - 04:28 PM

[!--quoteo(post=354436:date=Mar 13 2006, 05:27 PM:name=deonh)--][div class=\'quotetop\']QUOTE(deonh @ Mar 13 2006, 05:27 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
foreach works only on arrays, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

So make sure the function sql_get returns an array.
[/quote]

Thx for the response. This is the result of print_r on sql_get:

Array ( [0] => Array ( [test_id] => 1 [test_number] => 210 [test_topic] => Marketing 2 [test_name] => Marketing Sem2 2006 [test_data] => ##M::asdf::b::bb::aa::dfdff##E::ho ho ho ho [test_date] => 1141029115 [tr_mod_id] => 1 ) [1] => Array ( [test_id] => 2 [test_number] => 1212 [test_topic] => 1212 [test_name] => 121212 [test_data] => ##E::asdfasf##E::asdfdasfsafsadf [test_date] => 1141116140 [tr_mod_id] => 1 ) )

Isn't this a valid array? This is really strange.... Please note that if I change v1['tr_mod_id'] to 1 or any number, I get no warning!!!!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users