mardi 17 février 2015

PHP - Trying to get a function to convert state names to the corresponding abbreviations.


Vote count:

0




I apologize if this question has been answered but I have searched high and low and haven't been able to figure this out. I have a MySQL database that stores customer addresses and I would like to be able to pull the addresses from the table and print them for mailing from the webpage. The problem is that the state names are not in their abbreviated form in the table so I have been trying to convert them. When I pull the data I store it to an array. The array that holds the states is called $state and I use a counter ($data) to pull each set of values from each array. Here is the code I have for the function:


function convertState($name){ switch($name){ case "Alabama": return "AL"; break; case "Alaska": return "AK"; break; case "Arizona": return "AZ"; break; case "Arkansas": return "AR"; break; case "California": return "CA"; break; case "Colorado": return "CO"; break; case "Connecticut": return "CT"; break; case "Delaware": return "DE"; break; case "Florida": return "FL"; break; case "Georgia": return "GA"; break; case "Hawaii": return "HI"; break; case "Idaho": return "ID"; break; case "Illinois": return "IL"; break; case "Indiana": return "IN"; break; case "Iowa": return "IA"; break; case "Kansas": return "KS"; break; case "Kentucky": return "KY"; break; case "Louisiana": return "LA"; break; case "Maine": return "ME"; break; case "Maryland": return "MD"; break; case "Massachusettes": return "MA"; break; case "Michigan": return "MI"; break; case "Minnesota": return "MN"; break; case "Mississippi": return "MS"; break; case "Missouri": return "MO"; break; case "Montana": return "MT"; break; case "Nebraska": return "NE"; break; case "Nevada": return "NV"; break; case "New Hampshire": return "NH"; break; case "New Jersey": return "NJ"; break; case "New Mexico": return "NM"; break; case "New York": return "AL"; break; case "North Carolina": return "NC"; break; case "North Dakota": return "ND"; break; case "Ohio": return "OH"; break; case "Oklahoma": return "OK"; break; case "Oregon": return "OR"; break; case "Pennsylvania": return "PA"; break; case "Rhode Island": return "RI"; break; case "South Carolina": return "SC"; break; case "South Dakota": return "SD"; break; case "Tennessee": return "TN"; break; case "Texas": return "TX"; break; case "Utah": return 'UT'; break; case "Vermont": return "VT"; break; case "Virginia": return "VA"; break; case "Washington": return "WA"; break; case "West Virginia": return "WV"; break; case "Wisconsin": return "WI"; break; case "Wyoming": return "WY"; break; case "Other" || "other": return $province; break; default: return; }


It looks to me like it should work but when I try to pass it like this: convertState($state[$data]); Nothing happens all the state names are still spelled out. I would like it to convert each state to it's abbreviation as it is printed.



asked 28 secs ago







PHP - Trying to get a function to convert state names to the corresponding abbreviations.

Aucun commentaire:

Enregistrer un commentaire