Re: <select name=....

From: Date: Fri, 23 Feb 2001 05:33:24 +0000
Subject: Re: <select name=....
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-41263@lists.php.net to get a copy of this message
From: "Jonathan Sharp" <webdev@flyerware.com> > well...i just finished some code for dealing with an option box that has all > the states listed... > > <select name="state" size="1"> > <option value="">Select State</option> > <? > $ST[] = 'AL'; $ST[] = 'AK'; $ST[] = 'AR'; $ST[] = 'AZ'; > $ST[] = 'CA'; $ST[] = 'CO'; $ST[] = 'CT'; $ST[] = 'DC'; > $ST[] = 'DE'; $ST[] = 'FL'; $ST[] = 'GA'; $ST[] = 'HI'; > $ST[] = 'IA'; $ST[] = 'ID'; $ST[] = 'IL'; $ST[] = 'IN'; > $ST[] = 'KS'; $ST[] = 'KY'; $ST[] = 'LA'; $ST[] = 'MA'; > $ST[] = 'MD'; $ST[] = 'ME'; $ST[] = 'MI'; $ST[] = 'MN'; > $ST[] = 'MO'; $ST[] = 'MS'; $ST[] = 'MT'; $ST[] = 'NC'; > $ST[] = 'ND'; $ST[] = 'NE'; $ST[] = 'NH'; $ST[] = 'NJ'; > $ST[] = 'NM'; $ST[] = 'NV'; $ST[] = 'NY'; $ST[] = 'OH'; > $ST[] = 'OK'; $ST[] = 'OR'; $ST[] = 'PA'; $ST[] = 'PR'; > $ST[] = 'RI'; $ST[] = 'SC'; $ST[] = 'SD'; $ST[] = 'TN'; > $ST[] = 'TX'; $ST[] = 'UT'; $ST[] = 'VA'; $ST[] = 'VT'; > $ST[] = 'WA'; $ST[] = 'WV'; $ST[] = 'WI'; $ST[] = 'WY'; > for($i = 0; $i < count($ST); $i++) > { > echo '<option value="'.$ST[$i].'"'; > if($state == $ST[$i]) > { > echo ' selected'; > } > echo '>'.$ST[$i]."</option>\n"; > }//For > ?> > </select> > > First it creates an array $ST that contains a list of all the states > (includes PR and DC) > Next it loops through that array and if $state has the same value as the > state, it selects it... > > You could probably turn this into a function or something... > > =codeboy > Or better would be: <?php $states = array('AL', 'AK', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'PR', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WV', 'WI', 'WY'); foreach ($states as $s) { echo "<option value=\"$s\""; if ($state == $s) echo " selected"; echo ">$s</option>"; } ?> PHP has many syntax features and language constructs which can significantly improve code performance and manageability -- use them! Now this code could easily be extended to give each state a descriptive name, and to create a generic listbox-drawing function which you can then re-use elsewhere in your program (or as a code snippet which you can use in other programs in the future!): <?php function drawListBox ($name, $data, $selectedvalue="") { echo "<select name=\"$name\">\n"; foreach ($data as $key => $value) { echo "<option value=\"$key\""; if ($selectedvalue == $key) echo " selected"; echo ">$value</option>\n"; } echo "</select>"; } $states = array( "AL" => "Alabama", "AK" => "Arkansas", "AZ" => "Arizona", ... etc "WY" => "Wyoming" ); drawListBox("state", $states, $state); ?> When it comes to programming, in many ways "laziness is a virtue". Instead of writing the same code 10 times over in subtly different ways, write it once and make it reusable! Not only does this save you time, it can make your program less buggy and easier to upgrade. </lecture> :) Cheers Simon Garner

« previous php.general (#41263) next »