Page 1 of 1

Abbreviate State Names (Without Hacking the Database)

Posted: Sun Sep 06, 2009 3:46 am
by CharlieFoxtrot
Here's a code snippet that will easily convert US state names (and Canadian provinces) into their standard two-letter abbreviations.

This does not modify the database... it only makes the substitution whenever you're viewing orders on the "Admin:View Orders" page, or whenever you print out a Packing List or Invoice.

This has been tested and is working on ISC 4.07.

As always... make backups, test before going live.

1) Open the file admin\includes\classes\class.orders.php

2) Scroll to the bottom of the page and find the closing tag

FIND THIS:

Code: Select all

?>
3) Just above the closing tag... insert the following code

INSERT THIS ABOVE:

Code: Select all

// =====================================================
// Convert state / province names to two letter abbreviations
// =====================================================
function state_to_twoletter( $state_name, $country_name )  {
	if ($country_name == "United States" || $country_name == "Canada") {
		$state = array();
		$state['ALABAMA']='AL'; $state['ALASKA']='AK'; $state['AMERICAN SAMOA']='AS'; 
		$state['ARIZONA']='AZ'; $state['ARKANSAS']='AR'; $state['CALIFORNIA']='CA'; 
		$state['COLORADO']='CO'; $state['CONNECTICUT']='CT'; $state['DELAWARE']='DE'; 
		$state['DISTRICT OF COLUMBIA']='DC'; $state['FEDERATED STATES OF MICRONESIA']='FM'; 
		$state['FLORIDA']='FL'; $state['GEORGIA']='GA'; $state['GUAM']='GU'; $state['HAWAII']='HI'; 	
		$state['IDAHO']='ID'; $state['ILLINOIS']='IL'; $state['INDIANA']='IN'; $state['IOWA']='IA'; 
		$state['KANSAS']='KS'; $state['KENTUCKY']='KY'; $state['LOUISIANA']='LA'; $state['MAINE']='ME'; 
		$state['MARSHALL ISLANDS']='MH'; $state['MARYLAND']='MD'; $state['MASSACHUSETTS']='MA'; 
		$state['MICHIGAN']='MI'; $state['MINNESOTA']='MN'; $state['MISSISSIPPI']='MS'; 
		$state['MISSOURI']='MO'; $state['MONTANA']='MT'; $state['NEBRASKA']='NE'; 
		$state['NEVADA']='NV'; $state['NEW HAMPSHIRE']='NH'; $state['NEW JERSEY']='NJ'; 
		$state['NEW MEXICO']='NM'; $state['NEW YORK']='NY'; $state['NORTH CAROLINA']='NC'; 
		$state['NORTH DAKOTA']='ND'; $state['NORTHERN MARIANA ISLANDS']='MP'; $state['OHIO']='OH'; 
		$state['OKLAHOMA']='OK'; $state['OREGON']='OR'; $state['PALAU']='PW'; $state['PENNSYLVANIA']='PA'; 
		$state['PUERTO RICO']='PR'; $state['RHODE ISLAND']='RI'; $state['SOUTH CAROLINA']='SC'; 
		$state['SOUTH DAKOTA']='SD'; $state['TENNESSEE']='TN'; $state['TEXAS']='TX'; $state['UTAH']='UT'; 
		$state['VERMONT']='VT'; $state['VIRGIN ISLANDS']='VI'; $state['VIRGINIA']='VA'; 
		$state['WASHINGTON']='WA'; $state['WEST VIRGINIA']='WV'; $state['WISCONSIN']='WI'; 
		$state['WYOMING']='WY'; $state['ALBERTA']='AB'; $state['BRITISH COLUMBIA']='BC'; 
		$state['MANITOBA']='MB'; $state['NEW BRUNSWICK']='NB'; $state['LABRADOR']='NL'; 
		$state['NEWFOUNDLAND']='NL'; $state['NORTHWEST TERRITORIES']='NT'; $state['NOVA SCOTIA']='NS'; 
		$state['NUNAVUT']='NU'; $state['ONTARIO']='ON'; $state['PRINCE EDWARD ISLAND']='PE'; 
		$state['QUEBEC']='QC'; $state['SASKATCHEWAN']='SK'; $state['YUKON']='YT';
		return $state[strtoupper( $state_name )]; 
		}
	else {
		return $state_name;
		}
	}
4) Next... scroll up (just above the code you added) and find this:

FIND THIS:

Code: Select all

$addressPieces = array(
	isc_html_escape($address['shipfirstname']).' '.isc_html_escape($address['shiplastname']),
	isc_html_escape($address['shipcompany']),
	isc_html_escape($address['shipaddress1']),
	isc_html_escape($address['shipaddress2']),
	trim(isc_html_escape($address['shipcity'].', '.$address['shipstate'].' '.$address['shipzip']), ', '),
	isc_html_escape($address['shipcountry']).$countryFlag
);
5) We'll be working with the line that reads: trim(isc_html_escape($address['shipcity'].', '.$address['shipstate'].' '.$address['shipzip']), ', '),

The "shipstate" needs to be passed through our converter, but we ALSO need to let the converter check to confirm that the country is "United States" or "Canada". So... back to the code:

LOCATE THIS:

Code: Select all

trim(isc_html_escape($address['shipcity'].', '.$address['shipstate'].' '.$address['shipzip']), ', '),
REPLACE WITH:

Code: Select all

trim(isc_html_escape($address['shipcity'].', '.state_to_twoletter($address['shipstate'],$address['shipcountry']).' '.$address['shipzip']), ', '),
6) Save, Upload, and TEST!!

This could be easily modified to include any standard postal abbreviations for other countries as well.

Enjoy! 8-) Buy me a beer: paypal to... charlie.foxtrot.ebay [at] xemaps.com 8-)

Re: Abbreviate State Names (Without Hacking the Database)

Posted: Sun Sep 06, 2009 10:17 pm
by Martin
Just a thought but you could save yourself a little code and work by using this format...

Code: Select all

function state_to_twoletter( $state_name, $country_name )  {
	if ($country_name == "United States" || $country_name == "Canada") {
		$state = array(
			'ALABAMA' => 'AL',
			'ALASKA' => 'AK', 
			'AMERICAN SAMOA' => 'AS', 
.... 
			'QUEBEC' => 'QC',
			'SASKATCHEWAN'] => 'SK',
			'YUKON']='YT'
		);
		return $state[strtoupper( $state_name )]; 
		}
	else {
		return $state_name;
		}
	}
Just a suggestion for ease of reading and also optimisation... :)

Re: Abbreviate State Names (Without Hacking the Database)

Posted: Mon Sep 07, 2009 3:03 pm
by CharlieFoxtrot
Martin wrote:Just a thought but you could save yourself a little code and work by using this format...

Code: Select all

(...)
'ALABAMA' => 'AL',
'ALASKA' => 'AK', 
'AMERICAN SAMOA' => 'AS', 
(...)
Just a suggestion for ease of reading and also optimisation... :)
I like! ~ Thank you!! :D