I have something similar I use because I have a program that has several forms that ask for US State, and/or country. It's much easier to to read code that has a single include file or a function then it is to list 50 states or 100+ countries. In my case my include file has all the states as select options (the person on the project before me listed every state and country at each instance it was needed in the program). If you include a function with this snippet it can be very useful.
// I just threw this together real quick to give a general idea of how it could be used
// insert the array of states here... i'm not going to repost it for space sake
function displayStates($format = StatesText){ global$states;
foreach($statesas$abbr => $stateName){ switch($format){ case StatesText: // insert state as text only not in a form input echo"$abbr - $stateName<br>"; break;
case StatesSelect: // insert states as select options echo"<option value=\"$abbr\">$stateName</option>"; break;
case StatesRadio: // insert states as radio input type echo"<input type=\"radio\" name=\"states\" value=\"$abbr\"> $stateName"; break; } }
I suppose there are a few specific situations where having a PHP array of state abbreviations might be a useful timesaver (such as converting user input from the full name to the abbreviation or vice versa).
I know there have been plenty of times when I need to create a state/province drop-down for a form (although not with PHP).
// I just threw this together real quick to give a general idea of how it could be used
define('StatesText',1);
define('StatesSelect',2);
define('StatesRadio',3);
// insert the array of states here... i'm not going to repost it for space sake
function displayStates($format = StatesText){
global $states;
foreach ($states as $abbr => $stateName){
switch ($format){
case StatesText: // insert state as text only not in a form input
echo "$abbr - $stateName<br>";
break;
case StatesSelect: // insert states as select options
echo "<option value=\"$abbr\">$stateName</option>";
break;
case StatesRadio: // insert states as radio input type
echo "<input type=\"radio\" name=\"states\" value=\"$abbr\"> $stateName";
break;
}
}
}
I know there have been plenty of times when I need to create a state/province drop-down for a form (although not with PHP).
--
Chris Gmyr
www.chrisgmyr.com
www.syracusecs.com
www.syracusebands.net
Here is a more complete tutorial on how to get the box working as well.