Page 1 of 1

array type issue

Posted: Wed Aug 06, 2003 12:15 pm
by m3rajk
the error:
Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294

Warning: Invalid argument supplied for foreach() in /home/joshua/includes/fyd.unifunc.php on line 294
the function:

Code: Select all

function array_to_options($array, $selected){
  $optlist=''; # make an empty string
  foreach($array as $key=>$value){
    if($selected==$key){ # if this is the first time it wont match, so we don't need to care if it's set
      $optlist=$optlist."<option value="$key" selected>$value</option>";
    }else{
      $optlist=$optlist."<option value="$key">$value</option>";
    }
  }
  return $optlist;
}
some example arrays that are used:

Code: Select all

# eye color
$eyecolor=array('0'=>'Blue', '1'=>'Brown', '2'=>'Green', '3'=>'Grey', '4'=>'Hazel');
# hair color
$haircolor=array('0'=>'Black', '1'=>'Blonde', '2'=>'Brown', '3'=>'Dyed', '4'=>'Red');

Posted: Wed Aug 06, 2003 3:10 pm
by Stoker
debug with a print_r($array) inside the function..
to prevent/halt on such error add an is_array($array) before foreach

Posted: Wed Aug 06, 2003 3:13 pm
by m3rajk
well. i know it works because i was calling it from a test file to test it initially. this is the testfile:

Code: Select all

<?php
include("/home/joshua/includes/fyd.incs.php"); # includes file
$month=$_POST['month'];
if(!isset($month)){
  echo <<<END
    <html><head></head><body>
    <form action="$_SERVER[PHP_SELF]" method="POST">
    <input type="text" name="month">
    <input type="submit" value="check">
    </form>
    </body></html>
END;
}else{
  $array=array('00'=>'Month', '01'=>'January', '02'=>'February', '03'=>'March', '04'=>'April', '05'=>'May', '06'=>'June', '07'=>'July', '08'=>'August', '09'=>'September', '10'=>'October', '11'=>'November', '12'=>'December');
  $selected=$_POST['month'];
  $list=array_to_options($array, $_POST['month']);
  echo '<select name="test" size="1">'.$list.'</select>';
}
?>
note: the testfile NEVER genereated an error of ANY kind

Posted: Wed Aug 06, 2003 3:55 pm
by Stoker
the point is that foreach complains it is not an array, I suspect it is NULL, and the easiest way to find out is to debug in your function print_r or var_dump

Posted: Wed Aug 06, 2003 4:35 pm
by m3rajk
well... i don't think so. i tested the test file with a switch to the included file's $months array. this is the test file:

Code: Select all

<?php
include("/home/joshua/includes/fyd.incs.php"); # includes file
$month=$_POST['month'];
if(!isset($month)){
  echo <<<END
    <html><head></head><body>
    <form action="$_SERVER[PHP_SELF]" method="POST">
    <input type="text" name="month">
    <input type="submit" value="check">
    </form>
    </body></html>
END;
}else{
  $array=array('00'=>'Month', '01'=>'January', '02'=>'February', '03'=>'March', '04'=>'April', '05'=>'May', '06'=>'June', '07'=>'July', '08'=>'August', '09'=>'September', '10'=>'October', '11'=>'November', '12'=>'December');
  $selected=$_POST['month'];
  $list=array_to_options($months, $_POST['month']);
  echo '<select name="test" size="1">'.$list.'</select>';
}
?>
and this is the end of the included file

Code: Select all

# arrays used in search/join and possibly stats

# months
$months=array('00'=>'Month', '01'=>'January', '02'=>'February', '03'=>'March', '04'=>'April', '05'=>'May', '06'=>'June', '07'=>'July', '08'=>'August', '09'=>'September', '10'=>'October', '11'=>'November', '12'=>'December'); 
# sexual preference
$sexprefs=array('0'=>'Decline to Answer', '1'=>'Bisexual', '2'=>'Homosexual', '3'=>'Straight', '4'=>'Transgender', '5'=>'Transsexual');
# marital statuses
$marstats=array('0'=>'Decline to Answer', '1'=>'Single', '2'=>'Going Steady', '3'=>'Engaged', '4'=>'Married', '5'=>'Seperated', '6'=>'Divorced', '7'=>'Widow/Widower');
# countries
$countries=array('0'=>'Australia', '1'=>'Canada', '2'=>'USA', '3'=>'Other');
# eye color
$eyecolor=array('0'=>'Blue', '1'=>'Brown', '2'=>'Green', '3'=>'Grey', '4'=>'Hazel');
# hair color
$haircolor=array('0'=>'Black', '1'=>'Blonde', '2'=>'Brown', '3'=>'Dyed', '4'=>'Red');
# body type
$bodytypes=array('0'=>'Decline to Answer', '1'=>'Thin', '2'=>'Athletic & not Musclebound', '3'=>'Athletic & Musclebound', '4'=>'Heavyset', '5'=>'Overweight');
# education level
$el=array('0'=>'High School', '1'=>'Associate''s (2-yr)', '2'=>'Bachelor''s (4-yr)', '3'=>'Master''s', '4'=>'Doctorate');
# employment status
$emp=array('0'=>'Full Time', '1'=>'Part Time', '2'=>'Retired', '3'=>'Self Employed', '4'=>'Student', '5'=>'Unemployed');
# states/provinces/territories
$spts=array('0'=>'Alabama', '1'=>'Alaska', '2'=>'Alberta', '3'=>'American Samoa', '4'=>'Arizona', '5'=>'Arkansas', '6'=>'British Columbia', '7'=>'California', '8'=>'Colorado', '9'=>'Connecticut', '10'=>'Delaware', '11'=>'Federated States Of Micronesia', '12'=>'Florida', '13'=>'Georgia', '14'=>'Guam', '15'=>'Hawaii', '16'=>'Idaho', '17'=>'Illinois', '18'=>'Indiana', '19'=>'Iowa', '20'=>'Kansas', '21'=>'Kentucky', '22'=>'Louisiana', '23'=>'Maine', '24'=>'Manitoba', '25'=>'Marshall Islands', '26'=>'Maryland', '27'=>'Massachusetts', '28'=>'Michigan', '29'=>'Minnesota', '30'=>'Mississippi', '31'=>'Missouri', '32'=>'Montana', '33'=>'Nebraska', '34'=>'Nevada', '35'=>'New Brunswick', '36'=>'New Hampshire', '37'=>'New Jersey', '38'=>'New Mexico', '39'=>'New South Wales', '40'=>'New York', '41'=>'Newfoundland', '42'=>'North Carolina', '43'=>'North Dakota', '44'=>'Northern Mariana Islands', '45'=>'Northern Territory', '46'=>'Northwest Territories', '47'=>'Nova Scotia', '48'=>'Ohio', '49'=>'Oklahoma', '50'=>'Ontario', '51'=>'Oregon', '52'=>'Palau', '53'=>'Pennsylvania', '54'=>'Prince Edward Island', '55'=>'Puerto Rico', '56'=>'Quebec', '57'=>'Rhode Island', '58'=>'Saskatchewan', '59'=>'South Australia', '60'=>'South Carolina', '61'=>'South Dakota', '62'=>'Tasmania', '63'=>'Tennessee', '64'=>'Texas', '65'=>'Utah', '66'=>'Vermont', '67'=>'Victoria', '68'=>'Virgin Islands', '69'=>'Virginia', '70'=>'Washington', '71'=>'Washington, DC', '72'=>'West Virginia', '73'=>'Western Australia', '74'=>'Wisconsin', '75'=>'Wyoming', '76'=>'Yukon', '77'=>'Not From A Listed Country');
# self catagorization
$selfcat=array('0'=>'Decline to Answer', '1'=>'Gangsta', '2'=>'Goth', '3'=>'Hippy', '4'=>'Jock', '5'=>'Nerd/Geek', '6'=>'Preppy', '7'=>'Skater');
and slightly lower in line numbers is this (same include files as tose arrays)

Code: Select all

function array_to_options($array, $selected){
  $optlist=''; # make an empty string
  foreach($array as $key=>$value){
    if($selected==$key){ # if this is the first time it wont match, so we don't need to care if it's set
      $optlist=$optlist."<option value="$key" selected>$value</option>";
    }else{
      $optlist=$optlist."<option value="$key">$value</option>";
    }
  }
  return $optlist;
}

so, how can they be null when ALL the arrays i use are defined IN the include file that MUST be parsed for the function to be useable?

Posted: Wed Aug 06, 2003 5:21 pm
by Monk
Perhaps it is a problem with varible scope? How do you include the arrays and call the function?

Posted: Wed Aug 06, 2003 5:31 pm
by patrikG
The point Stoker is making is that while your code may be correct and work, the error probably lies in that in some instance the array $array is not propriated (hence the error-message). Hence use his advise and var_dump within the function.

Posted: Wed Aug 06, 2003 5:44 pm
by m3rajk
update:

the pages are called by functions so that i can have an if/elseif block to find which is approriate. as soon as i added an include lin in the function it worked.
obviously the problem was where they were being declared

stoker, sorry about reacting by blowing you off. it's actually what you said that made me think to try adding include lines in the functions.

looking at what's been posted since...ummm... yeah, that's exactly the issue monk. and patrick, you're right, i do owe him an apology, while my code is right, i needed to do the error debugging BEFORE calling that functions, which would have shown me exactly what i figured out after trying the include