File: example.php

Recommend this page to a friend!
  Classes of Dave Smith  >  PHP Address Formatter  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example usage
Class: PHP Address Formatter
Format postal address according to country rules
Author: By
Last change:
Date: 6 years ago
Size: 4,986 bytes
 

Contents

Class file image Download
<?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 using the class method - addNation
*/
$adf->addNation('US','United States of America');
$adf->addNation('EN','England');

//or assigning directly to the class property - nation_array
$adf->nation_array['CA'] = 'Canada';
$adf->nation_array['PT'] = 'Portugal';

//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']) ){
   
   
//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' => $_REQUEST['locality'],
       
'province' => $_REQUEST['province'],
       
'province_abbr' => $_REQUEST['province'],
       
'postcode' => $_REQUEST['postcode'],
       
'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 Manager Example</title>
    </head>
    <body>
        <form method="post">
            <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><label for="locality">Locality/City</label><br><em>(Boston)</em></td>
                    <td><label for="province">Province/State</label><br><em>(MA or Massachusetts)</em></td>
                    <td><label for="postcode">Postcode</label><br><em>(02130)</em></td>
                </tr>
                <tr>
                    <td><input type="text" name="locality" value="<?PHP echo ( empty($_REQUEST['locality']) ) ? '' : $_REQUEST['locality'];?>"></td>
                    <td><input type="text" name="province" value="<?PHP echo ( empty($_REQUEST['province']) ) ? '' : $_REQUEST['province'];?>"></td>
                    <td><input type="text" name="postcode" value="<?PHP echo ( empty($_REQUEST['postcode']) ) ? '' : $_REQUEST['postcode'];?>"></td>
                </tr>
            </table>
            <table>
                <tr>
                    <td><label for="nation">Nation/Country</label></td>
                </tr>
                <tr>
                    <td>
                        <select name="nation_abbr">
                            <option value="">Select one...</option>
<?PHP
//get the nation options from the class property nation_array
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>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>
                        <input type="hidden" name="form_posted" value="1">
                        <input type="submit" name="form_submit" value="Submit"> [<a href="example.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>

For more information send a message to info at phpclasses dot org.