<?PHP
error_reporting(E_ALL ^ E_NOTICE);
include('addrformat.class.php');
$adf = new addrformat;
/*
We need to set up some nations (countries). For this example we will do it manually,
however you can easily get the data from a database. We need to supply the abbreviation
and the nation name directly to the class property - nation_array
*/
$adf->nation_array = array(
'US' => 'United States of America',
'SW' => 'Star Wars Universe',
);
//sort the class property - nation_array
//we are also setting the US key/value pair at the top of that order
$adf->sortNation('US');
//processing the posted data
if( !empty($_REQUEST['form_posted']) ){
//manage cascade
switch( $_REQUEST['nation_abbr'] ){
case 'US':
$adf->addProvince('OR','Oregon');
$adf->addProvince('CA','California');
$adf->addProvince('WA','Washington');
switch( $_REQUEST['province_abbr'] ){
case 'OR':
$adf->addLocality('1','Medford');
$adf->addLocality('2','Ashland');
$adf->addLocality('3','Eugene');
switch( $_REQUEST['locality_id'] ){
case '1':
$adf->addPostcode('1','97501');
$adf->addPostcode('2','97504');
break;
case '2':
$adf->addPostcode('3','97520');
break;
case '3':
$adf->addPostcode('4','97401');
$adf->addPostcode('5','97402');
$adf->addPostcode('6','97403');
$adf->addPostcode('7','97404');
$adf->addPostcode('8','97405');
$adf->addPostcode('9','97408');
$adf->addPostcode('10','97440');
break;
}
break;
case 'CA':
$adf->addLocality('4','Yreka');
$adf->addLocality('5','Redding');
$adf->addLocality('6','Ontario');
switch( $_REQUEST['locality_id'] ){
case '4':
$adf->addPostcode('11','96097');
break;
case '5':
$adf->addPostcode('12','96001');
$adf->addPostcode('13','96002');
$adf->addPostcode('14','96003');
$adf->addPostcode('15','96049');
$adf->addPostcode('16','96099');
break;
case '6':
$adf->addPostcode('17','91758');
$adf->addPostcode('18','91761');
$adf->addPostcode('19','91762');
$adf->addPostcode('20','91764');
break;
}
break;
case 'WA':
$adf->addLocality('7','Algona');
$adf->addLocality('8','Woodland');
$adf->addLocality('9','Quincy');
switch( $_REQUEST['locality_id'] ){
case '7':
$adf->addPostcode('21','98001');
break;
case '8':
$adf->addPostcode('22','98674');
break;
case '9':
$adf->addPostcode('23','98848');
break;
}
break;
}
break;
case 'SW':
$adf->addProvince('TAT','Tatooine');
$adf->addProvince('NAB','Naboo');
$adf->addProvince('KAM','Kamino');
switch( $_REQUEST['province_abbr'] ){
case 'TAT':
$adf->addLocality('10','Mos Eisley');
$adf->addLocality('11','Bestine');
$adf->addLocality('12','Anchorhead');
break;
case 'NAB':
$adf->addLocality('13','Theed');
$adf->addLocality('14','Deeja Peak');
$adf->addLocality('15','Otoh Gunga');
break;
case 'KAM':
$adf->addLocality('16','Tipoca City');
break;
}
break;
}
$adf->sortProvince();
$adf->sortLocality();
$adf->sortPostcode();
if( !empty($_REQUEST['form_submit']) ){
//submit button pressed so create an address array by assigning posted data to tags
$addr_array = array(
'recipient' => $_REQUEST['recipient'],
'building' => $_REQUEST['building'],
'addr_num' => $_REQUEST['addr_num'],
'street' => $_REQUEST['street'],
'unit' => $_REQUEST['unit'],
'locality' => $adf->locality_array[$_REQUEST['locality_id']],
'province' => $adf->province_array[$_REQUEST['province_abbr']],
'province_abbr' => $_REQUEST['province_abbr'],
'postcode' => $adf->postcode_array[$_REQUEST['postcode_id']],
'nation' => $adf->nation_array[$_REQUEST['nation_abbr']],
'nation_abbr' => $_REQUEST['nation_abbr']
);
//process the address array
//we are also including the nation in the full address
$adf->procAddrArray($addr_array,true);
}
}
?>
<html>
<head>
<title>Address Format Cascade Example</title>
</head>
<body>
<form method="post">
<table>
<tr>
<td><label for="nation_abbr">Nation/Country</label></td>
<?PHP
//only show if we have built the province array from the supplied nation
if( !empty($adf->province_array) ){
?>
<td><label for="province_abbr">Province/State</label></td>
<?PHP
}
//only show if we have built the locality array from the supplied province
if( !empty($adf->locality_array) ){
?>
<td><label for="locality_id">Locality/City</label></td>
<?PHP
}
//only show if we have built the postcode array from the supplied locality
if( !empty($adf->postcode_array) ){
?>
<td><label for="postcode_id">Postcode</label></td>
<?PHP
}
?>
</tr>
<tr>
<td>
<select name="nation_abbr" onChange="this.form.submit(); return false;">
<option value="">Select one...</option>
<?PHP
foreach( $adf->nation_array as $key=>$value ){
$selected = ( !empty($_REQUEST['nation_abbr']) AND $key == $_REQUEST['nation_abbr'] ) ? ' selected' : '';
?>
<option value="<?PHP echo $key;?>"<?PHP echo $selected;?>><?PHP echo $value;?></option>
<?PHP
}
?>
</select>
</td>
<?PHP
//only show if we have built the province array from the supplied nation
if( !empty($adf->province_array) ){
?>
<td>
<select name="province_abbr" onChange="this.form.submit(); return false;">
<option value="">Select one...</option>
<?PHP
foreach( $adf->province_array as $key=>$value ){
$selected = ( !empty($_REQUEST['province_abbr']) AND $key == $_REQUEST['province_abbr'] ) ? ' selected' : '';
?>
<option value="<?PHP echo $key;?>"<?PHP echo $selected;?>><?PHP echo $value;?></option>
<?PHP
}
?>
</select>
</td>
<?PHP
}
//only show if we have built the locality array from the supplied province
if( !empty($adf->locality_array) ){
?>
<td>
<select name="locality_id" onChange="this.form.submit(); return false;">
<option value="">Select one...</option>
<?PHP
foreach( $adf->locality_array as $key=>$value ){
$selected = ( !empty($_REQUEST['locality_id']) AND $key == $_REQUEST['locality_id'] ) ? ' selected' : '';
?>
<option value="<?PHP echo $key;?>"<?PHP echo $selected;?>><?PHP echo $value;?></option>
<?PHP
}
?>
</select>
</td>
<?PHP
}
//only show if we have built the postcode array from the supplied locality
if( !empty($adf->postcode_array) ){
?>
<td>
<select name="postcode_id" onChange="this.form.submit(); return false;">
<?PHP
foreach( $adf->postcode_array as $key=>$value ){
$selected = ( !empty($_REQUEST['postcode_id']) AND $key == $_REQUEST['postcode_id'] ) ? ' selected' : '';
?>
<option value="<?PHP echo $key;?>"<?PHP echo $selected;?>><?PHP echo $value;?></option>
<?PHP
}
?>
</select>
</td>
<?PHP
}
?>
</tr>
</table>
<table>
<tr>
<td><label for="recipient">Recipient</label><br><em>(John Smith)</em></td>
</tr>
<tr>
<td><input type="text" name="recipient" value="<?PHP echo ( empty($_REQUEST['recipient']) ) ? '' : $_REQUEST['recipient'];?>"></td>
</tr>
<tr>
<td><label for="building">Building</label><br><em>(Tower One)</em></td>
</tr>
<tr>
<td><input type="text" name="building" value="<?PHP echo ( empty($_REQUEST['building']) ) ? '' : $_REQUEST['building'];?>"></td>
</tr>
</table>
<table>
<tr>
<td><label for="addr_num">Address Number</label><br><em>(123 1/2)</em></td>
<td><label for="street">Street Name</label><br><em>(North Main St.)</em></td>
<td><label for="unit">Unit</label><br><em>(Apartment 101)</em></td>
</tr>
<tr>
<td><input type="text" name="addr_num" value="<?PHP echo ( empty($_REQUEST['addr_num']) ) ? '' : $_REQUEST['addr_num'];?>"></td>
<td><input type="text" name="street" value="<?PHP echo ( empty($_REQUEST['street']) ) ? '' : $_REQUEST['street'];?>"></td>
<td><input type="text" name="unit" value="<?PHP echo ( empty($_REQUEST['unit']) ) ? '' : $_REQUEST['unit'];?>"></td>
</tr>
</table>
<table>
<tr>
<td> </td>
</tr>
<tr>
<td>
<input type="hidden" name="form_posted" value="1">
<input type="submit" name="form_submit" value="Submit"> [<a href="example_cascade.php">Start over</a>]
</td>
</tr>
</table>
</form>
<?PHP
//only show this if the class has built an address array
if( !empty($adf->addr_array) ){
?>
<div><strong>Full Address</strong></div>
<div style="margin-top:10px;"><?PHP echo nl2br($adf->addr_array['full_addr']);?></div>
<div style="margin-top:10px;"><strong>Single Line Address</strong><br><em>(aka waypoint which can be used with mapping services)</em></div>
<div style="margin-top:10px;"><?PHP echo $adf->addr_array['single_line'];?></div>
<?PHP
}
?>
<div style="margin-top:10px;"><strong>Class Values</strong></div>
<div><?PHP var_dump($adf);?></div>
</body>
</html>
|