Page 1 of 1

what's that error

Posted: Mon Feb 10, 2003 11:24 am
by forgun

Code: Select all

Warning: Invalid argument supplied for foreach() in /home/virtual/site13/fst/var/www/html/update.php on line 20
line:

Code: Select all

foreach ($id as $kay => $val) {

Posted: Mon Feb 10, 2003 11:51 am
by Stoker
$id is not an array...

always do something like

Code: Select all

if (is_array($myarray)) foreach ($myarray as $idx => $value) {
  # do stuff
} 
else {
  die ('You motherless son of a goat, that''s not an array!');
  # PS: Above Phrase is copyright Monty Python :)
}

Posted: Mon Feb 10, 2003 12:31 pm
by forgun
so what here not array

Code: Select all

<?PHP
include("includes/dbs.php");
$lk = mysql_connect($serv,$user,$pass) or die (mysql_error());
mysql_select_db($dbname[0],$lk);
$qur = "select * from News order by id desc limit 4";
$res = mysql_query($qur) or die (mysql_error());
while ($row = mysql_fetch_assoc($res)) {
   $id[] = $row["id"];
   $tit[] = $row["title"];
   $bod[] = $row["body"];
   $time[] = $row["time"];
   $date[] = $row["date"];
   $by[] = $row["byhow"];
}
?>
<html>
<body bgcolor="#C0C0C0"
style="color: #FFFFFF; text-align: right; font-weight: bold; margin-top: 0; margin-bottom: 0">
<?PHP
foreach ($id as $kay => $val) {
if ($by[$kay] == "forgun") {
?>
<a href="mailto:webmaster@il-vortex.net">
<img border="0"
src="images/staff/forgun.gif" style="border: 3px double #0000FF"></a>
<?
}
if ($by[$kay] == "chef") {
?>
<a href="mailto:webmaster@il-vortex.net">
<img border="0"
src="images/staff/chef.gif" style="border: 3px double #0000FF"></a>
<?
}
?>

Re: Invalid argument supplied for foreach()

Posted: Mon Feb 10, 2003 12:57 pm
by volka
while ($row = mysql_fetch_assoc($res)) {
probably the condition is never fulfilled and therefor the array is undefined when it comes to foreach. You can avoid that (and a warning message) by defining all arrays before the first use

Code: Select all

$id = $tit = $bod = $time = $date = $by = array();
$qur = "select * from News order by id desc limit 4";
$res = mysql_query($qur) or die (mysql_error());
while ($row = mysql_fetch_assoc($res)) {
   $id[] = $row["id"];
   $tit[] = $row["title"];
   $bod[] = $row["body"];
   $time[] = $row["time"];
   $date[] = $row["date"];
   $by[] = $row["byhow"];
}
so all variables are defined as empty arrays if the loop-body isn't executed at least once.

Posted: Mon Feb 10, 2003 2:38 pm
by forgun
it's now work befor he did