Thursday, September 17, 2009

ASP/PHP: Make a List of U.S. State Abbreviations

I’ll be digging through my old projects to find anything interesting enough to bore you to absolute death (or gouge your own eyes out with a fork in order to stop that burning feeling experienced from looking at shlock like this).  Here’s an example for populating an HTML [select] form object list (aka “listbox” or “drop-down list”, etc.) with 2-character abbreviations for U.S. states.  Enjoy…

PHP version:

function StateCodes($default) {
$delim = ',';
$clist = "AL,AK,AS,AZ,AR,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,NY,"
. "OH,OK,OR,PA,RI,SC,SD,TN,TX,UT,VA,VT,WA,WI,WV,WY";
$tok = strtok($clist, $delim);
while ($tok != false) {
if ($default == $tok) {
echo "\n";
}
else {
echo "\n";
}
$tok = strtok($delim);
}
}


ASP version:



Sub StatesList(default)
Dim lst, x
lst = "AL,AK,AZ,AR,CA,CO,CT,DE,DC,FL," & _
"GA,HI,ID,IL,IN,IA,KS,KY,LA,ME," & _
"MD,MA,MI,MN,MS,MO,MT,NE,NV,NH," & _
"NJ,NM,NY,NC,ND,OH,OK,OR,PA,RI," & _
"SC,SD,TN,TX,UT,VT,VA,WA,WV,WI,WY"
If default = "" Then
Response.Write "" & vbCRLF
End If
For each x in Split(lst, ",")
If Ucase(x) = Ucase(default) Then
Response.Write "" & vbCRLF
Else
Response.Write "" & vbCRLF
End If
Next
End Sub

No comments: