tags:

views:

48

answers:

4

Hello all.

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></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

+3  A: 

$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.

ceejayoz
Oh does format need to be a array?I will edit to show where $format comes from.
Luke
Yes, `foreach` only works on arrays.
ceejayoz
instead of doing $format = (array)$format; you can just instantly do foreach((array)$format as $form)
Xeross
@xeross Yeah, you can. I prefer to cast it separately for readability - it's easy to pass right over the casting if it's within the foreach call - but that's a matter of preference.
ceejayoz
A: 

are you sure $format is an array ? put an

<?php echo gettype($format); ?>

before the foreach loop

Xeross
A: 

What code is behind foreach($format as $form)? What type of $format? $format must be an array.

andser
A: 

$format is not array or not exists! Before foreach

if(is_array($format)){  
   foreach($format ...
}
turbod