Invalid argument supplied for foreach! [duplicate]
Possible Duplicate:
Invalid argument supplied for foreach()
I have the following code:
<?
foreach($format as $form)
{
echo $form;
?>
<ul>
<?
$s = $database->onlineFormatUsers($form);
while($row=mysql_fetch_assoc($s))
{
$username=$row['username'];
$id=$row['id'];?>
<li><a href="../userprofile.php?id=<?echo $id?>"><?echo "$username";?></a&g开发者_JAVA百科t;</li>
<?
}
?>
</ul>
<?
}
?>
<?
//the active formats
$f = $database->activeFormats();
while($row=mysql_fetch_assoc($f))
{
$format=$row['name'];
}
?>
It is saying its an invalid argument? Any reason why? Thanks
$format
is probably not an array.
Wrap the foreach
block in an if(is_array($format)) { }
block or cast it to an array by doing $format = (array)$format
.
are you sure $format is an array ? put an
<?php echo gettype($format); ?>
before the foreach loop
$format is not array or not exists! Before foreach
if(is_array($format)){
foreach($format ...
}
精彩评论