PX : code

options_array by Chad Lieberman
Download this code




<?

///////////////////////////////////
// options_array
//
//      Chad Lieberman
//      mallmall@yactota.com
//
///////////////////////////////////

//      A simple function for converting multiple arrays into html SELECT menus.
//      Convienient for building menus with more than one list of options.
//
//
//      Pass options_array:
//
//      $select_name    the name of the options menu
//      $selected       a 'selected' or default key (in this example the
//                      'selected' option is the option previously submitted - //                      which may be a bit confusing)
//      $array          an array from which to get the values of the options
//                      menu. Out of convinience I defined the array within the
//                      function but it should be easy to put it wherever you
//                      like.
//
//      If you can make this simpler, let me know . . . 



function options_array ($select_name,$selected,$array)
{
        $colors         = array ("#FFFFFF" => "White",
                                 "#000000" => "Black",
                                 "#99CC66" => "Green",
                                 "#FF9999" => "Red",
                                 "#996633" => "Brown",
                                 "#FFFFCC" => "Yellow",
                                 "#CCFFFF" => "Blue");

        $friends        = array ("John Entwhistle"      =>      "Johnny",
                                 "Mary Tyler Moore"     =>      "Mary",
                                 "Charlie Parker"       =>      "Bird",
                                 "Merryl Stubing"       =>      "Captain");

        print "<SELECT NAME=\"$select_name\">\n";

        while (list ($key, $val) = each ($$array))
        {
                print "<option value= " . "\"$key\" " . ($key==$selected? " selected ":"").">".$val."\n";
        }

        print "</SELECT>\n";
}

?>


<HTML>
<HEAD>
</HEAD>
<BODY>
<H2>    Example:                        </H2><BR>
<H1>    Choose a color and a friend.    </H1>

<FORM TYPE = "POST" ACTION = "options_array.php">

<?
        options_array ("friends", $friends, "friends");
        options_array ("text_color", $text_color, "colors");
?>

<INPUT TYPE = "SUBMIT" VALUE = "SUBMIT">
</FORM>

<?
        print "Friend: $friends <BR> Color: $text_color\n";
?>

</BODY>
</HTML>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.