Jump to content

Zip Code Function


b00gz

Recommended Posts

I have a zip code function that returns a zipcode and if the zipcode starts with a 0 it chops the 0 off of the front. I integrated this into someone else's system and am not sure how to force the 0 on the front to stay I am using number ranges when I do this for example...

If your zip code is between 00000 and 00300 then your cost is $20 and if it is between 00301 and 03000 then its $30. If anyone knows how to force the 0 please let me know.

Thanks,
Eric
Link to comment
Share on other sites

For comparison purposes, it's best to treat the zip code as a numeric value. So, that means leading zeros are irrelevant (00300 and 300 are the same thing).

You could use an array to hold all your ranges. See example code below. I used another array to hold the actual dollar amount. This makes it easier if there's zip ranges that will end up having the same dollar amount. You could simply replace the 'CA' and 'NV' values in the $zip_rates with the dollar amounts (and change the code accordingly). I used state codes, but it can be any alphanumeric value as long as it corresponds to the associative array index in the $zip_charges array.

This approach is not bad to implement because changes to zip ranges or charges can be done without having to change the main lookup code logic.

You could save the array part in an include file or even put that data in a file (or DB) and have it be read in instead.
[code]
<?PHP

$zip_rates = array(array('min' => 0, 'max' => 300, 'rate' => 'CA'),
                   array('min' => 301, 'max' => 3000, 'rate' => 'NV')
                  );

$zip_charges = array('CA' => 20.00,
                     'NV' => 30.00
                    );

$zip = (int) 500;  // from DB or form input - just an example

$charge = (float) 0;  // Initialize
for ($i = 0, $cnt = count($zip_rates); $i < $cnt; $i++) {

    if (($zip >= $zip_rates[$i]['min']) &&
        ($zip <= $zip_rates[$i]['max'])) {
        $charge = $zip_charges[$zip_rates[$i]['rate']];
        break;   // found rate so get out of loop
    }
}

echo "For zip $zip the charge is \$$charge <br/>";

?>
[/code]
[quote]
For zip 500 the charge is $30
[/quote]
FYI:

intval() function:
[a href=\"http://us2.php.net/manual/en/function.intval.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.intval.php[/a]

Type casting:
[a href=\"http://us2.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting\" target=\"_blank\"]http://us2.php.net/manual/en/language.type...pes.typecasting[/a]

str_pad() function:
[a href=\"http://us2.php.net/manual/en/function.str-pad.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.str-pad.php[/a]


hth.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.