How to generate drop down of US states with PHP MySQL

Posted on Jun 15, 2010 | 1 Comment

php-mysqlIn this article I’ll show you how to create a drop down having US state list quickly. This is a quick solution when you need to generate drop down list having all US states using PHP and MySQL. Just copy the below code in your php file and execute the SQL to populate the state data and you sre done. Since the state list will be coming from database so we can change the database at any point of time if we need any change. The PHP function which will create the drop down having state list takes two parameters- Drop down name and user selected value. Initially when user has not selected anything the default value will be blank.

<?
function genStateDropDown($selnm, $selectdSt="")        
{
      	$sqlSt = "select `statecd`, `statename` from `usstate` order by `statecd`";
       	$resSt = mysql_query($sqlSt);
       	echo "<select name=\"$selnm\">";
       	while($rowSt = mysql_fetch_array($resSt))     
       {
           if($selectdSt==$rowSt[statecd])
                 echo "<option value=\"$rowSt[statecd]\" selected>$rowSt[statename]</option>";
           else echo "<option value=\"$rowSt[statecd]\">$rowSt[statename]</option>";
       }
      	echo "</select>";
}
?>


The MySQL SQL to do this-

 
CREATE TABLE `usstate` (
  `statecd` VARCHAR(2) COLLATE latin1_general_ci NOT NULL,
  `statename` VARCHAR(50) COLLATE latin1_general_ci NOT NULL,
  PRIMARY KEY  (`statecd`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
 
--
-- Dumping data for table `usstate`
--
 
INSERT INTO `usstate` (`statecd`, `statename`) VALUES
('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');

TechcubeTalk Staff

This post/article/tutorial is written and published by TechCubeTalk.com staff (Blog editors, paid contributors or a guest contributors). TechCubeTalk.com reserves all rights and no part of this article can be published anywhere without prior written permission.

1 comment

  1. girl nextdoor says:

    Great detailed information, I just saved you on my google reader.

    Sent via Blackberry

Leave a comment


+ 6 = fourteen

Advertisement

Subscription

You can subscribe by e-mail to receive news updates and breaking stories.