Create a select input box in PHP

Description

  • Many times in PHP you find yourself creating a <select> input box. You might need some default option already selected, or you might want to populate the options with values from an array. These instructions will show you an easy way how to create and populate the list on-the-fly.

Terms of Use

  • All versions of this script, description, and documentation have been released under GNU General Public License. Basically this means you are free to use the script, modify it, and even redistribute versions of your own under the same license.

Instructions

The function

The first thing you'll need is the function that will automatically create the <select> and the <option> values you need:

<?php
#-#############################################
# desc: creates an <select> box
# param: name, array['val']="display" of data, default selected, extra parameters
# returns: html of box and options
function create_selectbox($name, $data, $default='', $param=''){
   
$out='<select name="'.$name.'"'. (!empty($param)?' '.$param:'') .">\n";

    foreach(
$data as $key=>$val) {
       
$out.='<option value="' .$key. '"'. ($default==$key?' selected="selected"':'') .'>';
       
$out.=$val;
       
$out.="</option>\n";
    }
   
$out.="</select>\n";

    return
$out;
}
#-# create_selectbox()
?>


How to use and call it

The function has 2 required parameters and 2 additional optional parameters

How it works:

  • $name (required) is the name value for the select box
  • $data (required) is an associative array with the array keys becoming the values and the array values becoming the visible text in the options
  • $default (optional) is the value of the default option you want to be selected
  • $param (optional) is any additional parameters you want the select box to have (example: style="" or size="")
<?php
// basic list of months
// for $month_text_array values, see bottom of page
echo create_selectbox("month", $month_text_array);
?>
Creates the HTML:
<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>


Another example:
<?php
// list of days of week, default to Tuesday, make the menu background green
// for $week_zdigit_array values, see bottom of page
echo create_selectbox("week", $week_zdigit_array, '2', 'style="background-color:green;"');
?>
Creates the HTML:
<select name="week" style="background-color:green;">
<option value="0">Sunday</option>
<option value="1">Monday</option>
<option value="2" selected="selected">Tuesday</option>
<option value="3">Wednesday</option>
<option value="4">Thursday</option>
<option value="5">Friday</option>
<option value="6">Saturday</option>
</select>


List of common arrays of values

A big part of the time you spend is creating a list of values. So I'll pass along a list of the common ones I use. You are also welcome to share your own array in the comments sections at the bottom.

I apologize for the horrible wrapping/overflow I'm pretty sure occurs in most browsers.

<?php
// value is the full state name
$state_name_array = array("Alabama"=>"Alabama","Alaska"=>"Alaska","Arizona"=>"Arizona","Arkansas"=>"Arkansas","California"=>"California","Colorado"=>"Colorado","Connecticut"=>"Connecticut","Delaware"=>"Delaware","Florida"=>"Florida","Georgia"=>"Georgia","Hawaii"=>"Hawaii","Idaho"=>"Idaho","Illinois"=>"Illinois","Indiana"=>"Indiana","Iowa"=>"Iowa","Kansas"=>"Kansas","Kentucky"=>"Kentucky","Louisiana"=>"Louisiana","Maine"=>"Maine","Maryland"=>"Maryland","Massachusetts"=>"Massachusetts","Michigan"=>"Michigan","Minnesota"=>"Minnesota","Mississippi"=>"Mississippi","Missouri"=>"Missouri","Montana"=>"Montana","Nebraska"=>"Nebraska","Nevada"=>"Nevada","New Hampshire"=>"New Hampshire","New Jersey"=>"New Jersey","New Mexico"=>"New Mexico","New York"=>"New York","North Carolina"=>"North Carolina","North Dakota"=>"North Dakota","Ohio"=>"Ohio","Oklahoma"=>"Oklahoma","Oregon"=>"Oregon","Pennsylvania"=>"Pennsylvania","Rhode Island"=>"Rhode Island","South Carolina"=>"South Carolina","South Dakota"=>"South Dakota","Tennessee"=>"Tennessee","Texas"=>"Texas","Utah"=>"Utah","Vermont"=>"Vermont","Virginia"=>"Virginia","Washington"=>"Washington","West Virginia"=>"West Virginia","Wisconsin"=>"Wisconsin","Wyoming"=>"Wyoming");

// value is the 2-char state code
$state_char_array = array("AK"=>"Alaska","AL"=>"Alabama","AR"=>"Arkansas","AZ"=>"Arizona","CA"=>"California","CO"=>"Colorado","CT"=>"Connecticut","DE"=>"Delaware","FL"=>"Florida","GA"=>"Georgia","HI"=>"Hawaii","IA"=>"Iowa","ID"=>"Idaho","IL"=>"Illinois","IN"=>"Indiana","KS"=>"Kansas","KY"=>"Kentucky","LA"=>"Louisiana","MA"=>"Massachusetts","MD"=>"Maryland","ME"=>"Maine","MI"=>"Michigan","MN"=>"Minnesota","MO"=>"Missouri","MS"=>"Mississippi","MT"=>"Montana","NC"=>"North Carolina","ND"=>"North Dakota","NE"=>"Nebraska","NH"=>"New Hampshire","NJ"=>"New Jersey","NM"=>"New Mexico","NV"=>"Nevada","NY"=>"New York","OH"=>"Ohio","OK"=>"Oklahoma","OR"=>"Oregon","PA"=>"Pennsylvania","RI"=>"Rhode Island","SC"=>"South Carolina","SD"=>"South Dakota","TN"=>"Tennessee","TX"=>"Texas","UT"=>"Utah","VA"=>"Virginia","VT"=>"Vermont","WA"=>"Washington","WI"=>"Wisconsin","WV"=>"West Virginia","WY"=>"Wyoming");

// value is month digit
$month_digit_array = array("1"=>"January","2"=>"February","3"=>"March","4"=>"April","5"=>"May","6"=>"June","7"=>"July","8"=>"August","9"=>"September","10"=>"October","11"=>"November","12"=>"December");
// value is month digit zero padded
$month_ddigit_array = array("01"=>"January","02"=>"February","03"=>"March","04"=>"April","05"=>"May","06"=>"June","07"=>"July","08"=>"August","09"=>"September","10"=>"October","11"=>"November","12"=>"December");

// value is month text name
$month_text_array = array("January"=>"January","February"=>"February","March"=>"March","April"=>"April","May"=>"May","June"=>"June","July"=>"July","August"=>"August","September"=>"September","October"=>"October","November"=>"November","December"=>"December");

// value is week text name
$week_text_array = array("Sunday"=>"Sunday","Monday"=>"Monday","Tuesday"=>"Tuesday","Wednesday"=>"Wednesday","Thursday"=>"Thursday","Friday"=>"Friday","Saturday"=>"Saturday");

// value is day of week digit 1 base
$week_digit_array = array("1"=>"Sunday","2"=>"Monday","3"=>"Tuesday","4"=>"Wednesday","5"=>"Thursday","6"=>"Friday","7"=>"Saturday");
// value is day of week digit zero-base
$week_zdigit_array = array("0"=>"Sunday","1"=>"Monday","2"=>"Tuesday","3"=>"Wednesday","4"=>"Thursday","5"=>"Friday","6"=>"Saturday");

// day of month
$day_digit_array = array("1"=>"1","2"=>"2","3"=>"3","4"=>"4","5"=>"5","6"=>"6","7"=>"7","8"=>"8","9"=>"9","10"=>"10","11"=>"11","12"=>"12","13"=>"13","14"=>"14","15"=>"15","16"=>"16","17"=>"17","18"=>"18","19"=>"19","20"=>"20","21"=>"21","22"=>"22","23"=>"23","24"=>"24","25"=>"25","26"=>"26","27"=>"27","28"=>"28","29"=>"29","30"=>"30","31"=>"31");
// day of month zero padded
$day_ddigit_array = array("01"=>"01","02"=>"02","03"=>"03","04"=>"04","05"=>"05","06"=>"06","07"=>"07","08"=>"08","09"=>"09","10"=>"10","11"=>"11","12"=>"12","13"=>"13","14"=>"14","15"=>"15","16"=>"16","17"=>"17","18"=>"18","19"=>"19","20"=>"20","21"=>"21","22"=>"22","23"=>"23","24"=>"24","25"=>"25","26"=>"26","27"=>"27","28"=>"28","29"=>"29","30"=>"30","31"=>"31");

// months as digit zero padded
$month_digit_array = array("01"=>"01","02"=>"02","03"=>"03","04"=>"04","05"=>"05","06"=>"06","07"=>"07","08"=>"08","09"=>"09","10"=>"10","11"=>"11","12"=>"12");

// array of years from current year up to ten years from now
$year_digit_array=array();
for (
$i=date("Y"), $s=$i+10; $i<$s; ++$i){
   
$year_digit_array[$i]=$i;
}
?>