what's that error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
forgun
Forum Commoner
Posts: 61
Joined: Wed Jan 29, 2003 6:05 am
Contact:

what's that error

Post 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) {
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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 :)
}
User avatar
forgun
Forum Commoner
Posts: 61
Joined: Wed Jan 29, 2003 6:05 am
Contact:

Post 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>
<?
}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Invalid argument supplied for foreach()

Post 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.
User avatar
forgun
Forum Commoner
Posts: 61
Joined: Wed Jan 29, 2003 6:05 am
Contact:

Post by forgun »

it's now work befor he did
Post Reply