RE: [PHP] Remembering which option in a SELECT tag the user had chosen

From: Date: Wed, 29 Jan 2003 17:35:44 +0000
Subject: RE: [PHP] Remembering which option in a SELECT tag the user had chosen
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-133532@lists.php.net to get a copy of this message
> I was thinking of inserting a check on each option, but I thought there > had to be a different way. > I've received ideas of storing the option values in an array and > creating a function to generate that dropdown by grabbing values out of > the array, so I want to go with that because that is more elegant, which > what I was after. Yes, certainly that's a better method. You'll still be doing the same thing, though, basically. You'll still do a comparison on every <option> you create and if it matches, then insert the SELECTED text. It will just be neater because it's all in a nice loop inside of a function. Here are two functions I find useful. One is to make a <SELECT> with various options from a global $_CONF array. The other is used to add a new <option> to the <SELECT> (so you can add options when sometimes needed, without modifying the $_CONF array). Usage: $_CONF['day'] = array('Monday','Tuesday','Wednesday'); $select = conf_select('day','Tuesday'); echo $select; $new_select = add_option($select,'Any'); echo $new_select; //$select and $new_select will contain the //HTML to create the drop down select boxes Code: //creates a html select drop down box from a //$_CONF array matching the passed key //if $make_array == 1, then an [] will be appended //onto the select name so that the results are an array //if $make_multi is set to something greater than zero, //the select will be made MULTIPLE and the SIZE will //be set to the value of $make_multi. //If $name is passed, that value will be used as the NAME //of the select, instead of the "key" of the $_CONF array function conf_select($conf_key, $selected = "", $make_array=0, $make_multi=0, $name="") { global $_CONF; $retval = ""; if(isset($_CONF[$conf_key])) { if(strlen($name) == 0) { $name = $conf_key; } $retval .= "<select name='" . $name; if($make_array == 1) { $retval .= "[]"; } $retval .= "' class='text_box'"; if($make_multi > 0) { $retval .= " multiple size='$make_multi'>\n"; } else { $retval .= " size='1'>\n"; } foreach($_CONF[$conf_key] as $value) { $retval .= "<option value='" . $value . "'"; if(strcasecmp($value,$selected)==0) { $retval .= " selected"; } $retval .= ">" . $value . "</option>\n"; } $retval .= "</select>\n"; } else { $retval = "Invalid Conf Key"; } return $retval; } //Adds in an <option> to the <select> box //in the $select_text function add_option($select_text, $option) { $retval = preg_replace("/>/",">\n<option value=\"$option\">$option</option>\n",$select_text,1); return $retval; } ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/

« previous php.general (#133532) next »