PHP State Drop Down Menu - Reusable Code!
In an effort to stop writing and re-writing code I'm going to start sharing some of my favorite functions that I use at home and at work.
I need to create a lot of drop down menus (or select boxes) for CMS' as well as lead gen forms and registration forms.
I work with one concise PHP USA states array. Everything is on one line to save space, which when it comes to obvious things that won't be edited often (like states, months, days of the week arrays) is perfect.
My Three Most Common PHP Arrays
PHP USA States Array
The states array uses the all capital version of the state's abbreviation as the key in this associative array.
PHP
<?php
$states_arr = array('AL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming");
?>
PHP Months Array
There's more than two ways to represent the English versions of the months of a Gregorian calendar.
For January there's:
- January
- Jan
- 1
So for the most reusable code we need to incorporate all of those versions. Since we will most likely key off of the numeric value of the month we'll use that as our key.
PHP
<?php
$month_arr = array(1=>array('Jan','January'),2=>array('Feb','February'),3=>array('Mar','March'),4=>array('Apr','April'),5=>array('May','May'),6=>array('June','June'),7=>array('July','July'),8=>array('Aug','August'),9=>array('Sept','September'),10=>array('Oct','October'),11=>array('Nov','November'),12=>array('Dec','December'));
?>
Not that in this example I stuck with the common 'shortened' versions. Later I'll show you how to only use the first three characters (the shorter abbreviation) without changing the array, making it super reusable.
PHP Day of the Week Array
Days of the week is another one where you ought to use both a numeric value as well as an abbreviation and a full version of the name, much like months.
PHP
<?php
$weekday_arr = array(0=>array('Sun','Sunday'),1=>array('Mon','Monday'),2=>array('Tues','Tuesday'),3=>array('Wed','Wednesday'),4=>array('Thurs','Thursday'),5=>array('Fri','Friday'),6=>array('Sat','Saturday'));
?>
Not that in this example I stuck with the common 'shortened' versions. Later I'll show you how to only use the first three characters (the shorter abbreviation) without changing the array, making it super reusable.
Making Drop Down Menu's with an Array in PHP
Now that we have out common array's all neat and tidy we'll create a simple function ot loop through them and create a <select> menu out of 'em.
This should do the job:
PHP
<?php
function showOptionsDrop($array){
$string = '';
foreach($array as $k => $v){
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
}
return $string;
}
?>
Simply use this snippet to display a menu of months where the option will be formatted much like <option value="AL">Alabama</option>
HTML/PHP
<select name="states">
<option value="0">Choose a state</option>
<?php echo showOptionsDrop($states_arr); ?>
</select>
Now keep in mind this works great for the simple key=>value pairs in the states array, but not yet for the more complex, multi-dimensional array for months and days of the week.
Let's make that function more advanced before we start working with the multi-dimensional arrays. We need this function to also add the select="select" attribute if that is the item that needs to be selected. This way we can predefine a drop down. this is perfect for server side validation, editing existing content in a CMS as well as setting a date in a drop down menu when the page is loading.
Another parameter I am now in the habit of adding as an optional parameter to my functions is a boolean decision to either echo or return the values.
PHP & HTML
<?php
function showOptionsDrop($array, $active, $echo=true){
$string = '';
foreach($array as $k => $v){
$s = ($active == $k)? ' selected="selected"' : '';
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
}
if($echo) echo $string;
else return $string;
}
?>
<select name="states">
<option value="0">Choose a state</option>
<?php showOptionsDrop($states_arr, null, true); ?>
</select>
Continue on to page 2: using intermediary functions for the multidimentional array drop down menus
Popularity: 86% [?]
the newest discoveries, stories and shared tips!Come on, all the cool kids are doing it ;)



Nice code. Your formatting could use some work though.