Re: automatically selecting HTML drop down
| From: | Robin Vickery | Date: | Tue, 15 Aug 2000 10:58:42 +0000 |
| Subject: | Re: automatically selecting HTML drop down | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-11775@lists.php.net to get a copy of this message | ||
raguzman@netraiser.com ("Ralph Guzman") writes:
> There are several ways you can do this. I prefer to use an include file
> where I list all states in array then I use a function to call out the
> states and check to see if any of them should be selected. This I think is a
> cleaner way of doing it. If you are interested in this method let me know.
>
> Here is another way of doing it. Not as clean, but it will work.
>
> Assuming $state has been assigned from your Database query:
>
> <SELECT NAME="state">
> <OPTION VALUE="Alabama" <? if($state == "Alabama"){ echo
> "SELECTED"; }
> ?>>Alabama</OPTION>
> <OPTION VALUE="Arizona" <? if($state == "Arizona"){ echo
> "SELECTED"; }
> ?>>Arizona</OPTION>
> <OPTION VALUE="California" <? if($state == "California"){ echo
> "SELECTED"; }
> ?>>California</OPTION>
> etc...
> etc....
> </SELECT>
>
> As I said, this is not the cleanest way to do it since you will have to do
> this for every single state. vs just calling out a function on your page
> that looks like this:
>
> stateSelectMenu(state, $state);
>
> Just to give you a heads up, this is what the include file with the array
> and funtion method would look like.
>
> $States = array(
> "Alabama"=>"Alabama",
> "Arizona"=>"Arizona",
> "California"=>"California"
> );
>
> function stateSelectMenu($name, $selected)
> {
> global $States;
> echo "<SELECT NAME=\"$name\" SIZE=\"1\">";
> echo "<OPTION VALUE=\"\">- Select State -</OPTION>\";
>
> for(reset($States); $key = key($States); next($States))
> {
> echo "<OPTION VALUE=\"$key\"";
> if($key == $selected)
> {
> echo " SELECTED";
> }
> echo ">$StatesProvince[$key]</OPTION>";
> }
>
> echo "</SELECT>";
> }
I tend to do it like this, which has the advantage of not needing
any conditionals at all:
$villages = array(
'Loxbeare', 'Rackenford', 'Calverleigh', 'Lurley',
'Washfield', 'Stoodleigh', 'Oakford', 'Frogwell',
'Nomansland', 'Black Dog', 'Cruwys Morchard'
);
$selected = array(
'Loxbeare' => ' selected',
'Stoodleigh' => ' selected'
);
print "<select name='chooseVillage' multiple size='5'>\n";
foreach ($villages as $village)
{
print "<option$selected[$village]> $village </option>\n";
}
print "</select>\n";
--
Robin Vickery...............................................
Planet-Three, 3A West Point, Warple Way, London, W3 0RG, UK
Email: robin@planet-three.net Phone: +44 (0)794 670 6395