Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 6th, 2003, 10:30 AM
Authorized User
 
Join Date: Jul 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default type issue

the error:
Code:
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:
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:
# 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');
internships are for the inexperienced, yet corproations award them only to those with experience. then corporations complain about the lack of experience in the emerging workforce.

corporations need to get logical.
__________________
internships are for the inexperienced, yet corproations award them only to those with experience. then corporations complain about the lack of experience in the emerging workforce.

corporations need to get logical.
 
Old August 6th, 2003, 03:35 PM
Authorized User
 
Join Date: Jul 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well. i know it works because i was calling it from a test file to test it initially. this is the testfile:
Code:
<?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


NOTE 2: THE ARRAYS ARE DEFINED BELOW THE FUNCTION IN THE SAME FILE, THUS IF THE FUNCTION IS BEING INCLUDED, SO ARE THEY, AND TWO OF THEM ARE IN THE INITAL POST. THE CALLS IN THE TWO FILES THAT USE THEM ARE LIKE THIS: $eyecoloroptlist=array_to_options($eyecolor, $_POST['eye']);

internships are for the inexperienced, yet corproations award them only to those with experience. then corporations complain about the lack of experience in the emerging workforce.

corporations need to get logical.
 
Old August 6th, 2003, 04:31 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You should include as much built in error finding as possible.

For instance:

Code:
function array_to_options($array, $selected) {

    $optlist=''; # make an empty string

    if (empty($array)) {

        echo "Fatal Error: array does not contain a value.";

    }

    if (is_array($array)) {

        foreach($array as $key => $value){

            if($selected == $key) { 

              $optlist=$optlist."<option value=\"$key\" selected>$value</option>";

            } else {

              $optlist=$optlist."<option value=\"$key\">$value</option>";

            }

          }

    } else {

        # if this is always supposed to be an array return false, otherwise execute the 'non-array'
        # version

        echo "Fatal Error: Variable passed to foreach is not an array.<br /><br />";
        echo $array;





        return false;

    }

    return $optlist;

}
Since PHP might not always find all your errors for you and given all the possible typos that can happen this makes good programming practice and helps to narrow things down. Another possibility is a namespace issue, which can be very hard to track down and cause strange errors. I find it helps when developing and encountering a seamingly unrealistic error, when all else fails try changing variable names. This can especially be a nuisance if register_globals is activated.

You can also try printing out variable values on the screen... this helps to narrow down when and where something is going awry.

The worst thing you can do is approach an error as insurmountable!

Otherwise everything seems intact syntactically. IMO.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old August 6th, 2003, 05:19 PM
Authorized User
 
Join Date: Jul 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it deals with wehre the array is declared and the fact i needed to include it in the function creating the approptriate page.
i made a file for variables used by multiple pages, included that as a include in the function. it works fine.

internships are for the inexperienced, yet corproations award them only to those with experience. then corporations complain about the lack of experience in the emerging workforce.

corporations need to get logical.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Guidelines Item 3: Replace System.Type with Type Ixtlia BOOK: Professional .NET 2.0 Generics 0 August 19th, 2007 04:09 AM
Issue with SqlData Type in ASP.NET snejsnej ASP.NET 1.0 and 1.1 Professional 1 August 30th, 2006 04:47 PM
Convert "String" type to "Control" type ? kishore_peddi C# 4 January 11th, 2006 01:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.