Page 1 of 1

zip code zone using switch case ???

Posted: Mon Jul 21, 2008 9:10 pm
by elegantlather
Okay, new to PHP. Have used a few other programming languages in the past, usually basic type languages. So far, PHP is similar enough that I can get along with what I need to do. Anyway, ...

I've been racking :banghead: my brain the last few days. I am trying to figure out how to set up shipping fees for our online store. I don't want to use a 3rd-party software (free or otherwise). I want to use the Post Office (USPS) since they have free boxes, their fees are usually cheaper (and, we're not shipping anything hazardous). And, I've gotten set up with Paypal. Everything is working so far ... except Paypal can only seperate shipping fees based upon State. That won't work for us because we live in Texas, so shipping to El Paso versus Houston versus Dallas would require different shipping costs, different increments. In fact, shipping to El Paso costs about the same as shipping to Kansas City, but I can't seperate cities like El Paso from Paypal's setup.

That's the background. I've found sort of a solution, but I just realised a few minutes ago how large this is going to make my program ... and very probably ... SLOW. 8O Paypal offers a couple of variables that I thought I could use. I've tested a simple version of the system, and it works. You see (for those who aren't familiar with it), the post office has seperated the country into 8 zones, and the zone boundaries are based upon where you are shipping from. Zone 1 is your local area. Zone 2 is a little farther away. With each zone, the distance is greater. Thus, the shipping costs go up accordingly. Finally, zone 8 (for us anyway) is like shipping to the tip of Maine. Anyway, to find out what zone (and thus, what shipping rate to charge), I need to know the customer's shipping zip code ... then, cross reference it with a list I printed from the USPS website.

Here's is a simple script that does that:

Code: Select all

 <?php
$handling_cart_value = "4.00";
 
/*User typed in zipcode from previous page
I just need the first 3 digits of the zip code.*/
 
$zipcode = substr($_POST["zipcode"],0,3); 
 
switch ($zipcode) {
case 750:
case 751:
    $zone=1;
    break;
case 734:
case 735:
    $zone=2;
    break;
case 706:
    $zone=3;
    break;
default:
    $zone=8;
    break;
endswitch;
}
 
switch ($zone) {
case 1:
    $shipping_value = "1.00";
    $shipping2_value = "0.25";
    break;
case 2:
    $shipping_value = "1.25";
    $shipping2_value = "0.25";
    break;
case 3:
    $shipping_value = "1.25";
    $shipping2_value = "0.50";
    break;
default:
    $shipping_value = "2";
    $shipping2_value = "0.75";
    break;
endswitch;
}
 
?> 
This is a very simple example. Doesn't include all the zones and definitely doesn't include all the 3-digit zip codes, either. Our local zone includes nine 3-digit zipcode regions. That's not bad. But, zone 3 has 54 3-digit zipcode regions. Plus, zones 4-6 have more than 3. That is a lot of switch case statements. I fear that it will bog down my website. A few seconds is okay. But, what type of slow down are we talking about for a server to go through a couple hundred (maybe even like 500) switch case statements? Is there a better way without getting into a full blown shopping cart? I've heard of using an array. Would it be faster in this case? Would it be easy to edit? (For example, if USPS decides to re-zone ... ugh!)

Any ideas would be helpful.

Mark

P.S. - The reason I don't want to go with a full blown shopping cart is I want a system that I can get my brain around. I've looked at shopping carts and they have too much. I would rather build it myself from the ground up. That way, I understand what's going on.

Re: zip code zone using switch case ???

Posted: Mon Jul 21, 2008 10:21 pm
by Buddy
could you try this:

Code: Select all

 
$zipcode = substr($_POST["zipcode"],0,3); 
 
$xx=array(  1=>array(750,751,752,753,), 
            2=>array(734,735,736,737,),
            3=>array(706,707,708,709,), 
          );
 
$zone=8;
foreach($xx as $zz=>$cc )
{
    if(in_array($zipcode,$cc))   {  $zone=$zz;  break;    }
}
 
I don't know if i helped you.

Re: zip code zone using switch case ???

Posted: Tue Jul 22, 2008 12:43 pm
by elegantlather
I'll give it try. It should work better than switch case, but I under estimated how many 3-digit zip codes there are. After going through them, I think there may be more than nine hundred of them. An array may be just as slow. We shall see. Thanks for the help.

Mark

Re: zip code zone using switch case ???

Posted: Tue Jul 22, 2008 12:51 pm
by Eran
A better way to do it would be to create a database table of zipcodes with their matching zone and state, and query against it when you need to know the state / zone for a particular zipcode.
I worked with a zipcode based shipping address once (actually for moving), and the site owner bought a zipcode database from the post office. I'm not sure how much it cost, but it was pretty extensive, with over 70,000 different zipcodes.

Re: zip code zone using switch case ???

Posted: Tue Jul 22, 2008 1:45 pm
by elegantlather
pytrin wrote:A better way to do it would be to create a database table of zipcodes with their matching zone and state, and query against it when you need to know the state / zone for a particular zipcode.
I worked with a zipcode based shipping address once (actually for moving), and the site owner bought a zipcode database from the post office. I'm not sure how much it cost, but it was pretty extensive, with over 70,000 different zipcodes.
That sounds nice, but I don't think I need something that extensive. Besides, I don't want to mess with a database. That would mean maintaining 70,000 entries. The way I'm going to do it means maintaining only 900 3-digit zipcodes in an array. I've built the entire array, and it works fine. I don't see a speed problem, either. It was pretty quick when I tested it.

In the future, we may use other solutions such as a full fledge shopping cart or whatever. Right now, I am trying to achieve certain goals without database programming.

Mark

Re: zip code zone using switch case ???

Posted: Tue Jul 22, 2008 2:20 pm
by Sephirangel
If you dont want a database solution to this problem then the array solution, as suggested, should work just fine!
Although if you wanted to extend your post codes to other regions a database would be more suitable. It all depends on the scope of what your building it for. 8)

Re: zip code zone using switch case ???

Posted: Tue Jul 22, 2008 6:45 pm
by Buddy
yes,if your zipcodes is limited,and it don't need to change often,you don't use the database.