foreach quirk

They have: 461 posts

Joined: Jul 2003

i've used it in other places with the (array as value) style, so i am missing the issue here. it's saying

Quote:
Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.funcs.php on line 90

which is the foreachloop below. the thing is that $ims is an array of "instant" message id numbers (for a site messaging system) so even if there's only one, i should need an array to access it. i figure since it's variable (0,1,2,3,4,5,or as many as are waiting) for the max, instead of a for loop it's mmore efficient to use foreach
yet it's not working. this is in the bgnpg function. when somoene is logged in it'll check a cookie to fill it (if the person accepts ims), otherwise it's going to be an empty array, thus the if statementto test if it should go into the for loop.

<?php
 
if(count($ims)){ # if there's any ims
   
echo \"\n      &lt;script language=\\"javascript\\"&gt;\n\";
    foreach(
$ims as $mid){ # foreach im to display
      echo \"    window.open('http://24.91.157.113/findyourdesire/message.php?mid=
$mid'
, '
$mid', 'height=250,width=514,scrollbars=auto,resizable=yes');\n\";
    }
    echo \"      &lt;/script&gt;\n\";
  }
?>
(note: code edited to prevent scrolling)

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Are you sure $ims is an array? If you do count() on a scalar variable, it will return 1.

They have: 461 posts

Joined: Jul 2003

the first three lines of the function:

<?php
function bgnpg($title){ # begins all pages
 
include(\"/home/joshua/includes/fyd.altincs.php\"); # includes file
 
$ims=array();
?>
the loop that adds to it
<?php
     
if($accepts){ # person accepts ims
   
if($accepts>5){ # the user wants them ALL
     
$fims=mysql_query(\"SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0'\", $db);
     
$amtims=mysql_num_rows($fims);
      if(
$amtims){ # we have ims
        for(
$i=0;$i<$amtims;$i++){ # for each im
         
$gimid=mysql_fetch_array($fims); $ims=$gimid['msg_id']; # record the msg_id
        }
      }
    }else{ # user wants
$accepts amount
     
$fims=mysql_query(\"SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0' ORDER BY msg_id ASC LIMIT $accepts\", $db);
     
$amtims=mysql_num_rows($fims);
      if(
$amtims){ # we have ims
        for(
$i=0;$i<$amtims;$i++){ # for each im
         
$gimid=mysql_fetch_array($fims); $ims=$gimid['msg_id']; # record the msg_id
        }
      }
    }
      }
?>

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

They have: 461 posts

Joined: Jul 2003

i see it. the missing [] is changing it from an array to a scalar.

They have: 461 posts

Joined: Jul 2003

new WORKING code:

<?php
      
if($accepts){ # person accepts ims
   
if($accepts>5){ # the user wants them ALL
     
$fims=mysql_query(\"SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0'\", $db);
      while(
$gimid=mysql_fetch_array($fims)){ # while there's ims
       
$ims[]=$gimid['msg_id']; # record the msg_id
      }
    }else{ # user wants
$accepts amount
     
$fims=mysql_query(\"SELECT msg_id FROM msgs WHERE to_id='$uid' AND viewed='0' ORDER BY msg_id ASC LIMIT $accepts\", $db);
      while(
$gimid=mysql_fetch_array($fims)){ # while there's ims
       
$ims[]=$gimid['msg_id']; # record the msg_id
      }
    }
      }
?>

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.