Zend - The PHP Company




Forms

Add Code


Option Generator  

Type: code fragment
Added by: ralexander
Entered: 01/03/2001
Last modified: 03/12/2000
Rating: - (fewer than 3 votes)
Views: 5921
This function takes three arguments and returns your option code. All you have to do is provide the form and select code as in the example. ;-]


<?php
//FUNCTION TO GENERATE OPTION FROM ARRAY
//
//The $options variable is an array where the key is the value 
//of the option and the value is the displayed part of the option.
//
//The $selected variable is the item you want preselected and must
//match the value.
//
//The $var is the name of the option which will also be your 
//variable in the script that uses your form data.
//
//Randall Augustus Alexander
//zonezero@zonedzero.net
//http://www.zonedzero.net ,.com, .org

function make_option($options$selected ""$var)
{
echo 
"<SELECT NAME='$var'>n";

while (list (
$value$display) = each ($options))
     {
     echo 
"<OPTION VALUE='$value' ";
     if (
$value == $selected){$select "SELECTED";}
        else {
$select "";}
     echo 
$select>$display</OPTION>n";
     }

echo 
"</SELECT>n";
}
?>


Usage Example


<?php
//PREPARE ARRAYS
$options_gender = array("m"=>"Male""f"=>"Female");

//SETUP HTML AND GENERATE OPTION CODE
echo "<html>
<head>
<title>Your Title</title>
</head>"
;
echo 
"<FORM ACTION='action.php' METHOD='post'>n";

make_option($options_genderFemalegender);

echo 
"<p></p><INPUT TYPE='Submit' NAME='Submit'>n";
echo 
"<p>$selected - $var n";
echo 
"</html>";
?>


Rate This Script





Search



This Category All Categories