Forum Moderators: coopster

Message Too Old, No Replies

zip code search/redirect script

         

alivemedia

4:31 am on Sep 23, 2005 (gmt 0)

10+ Year Member



Hi,

I'm fairly new to the php/mysql world. I need some help with my first assignment doing some php scripting and was going to see if there was a resource of scripts for this.

Here's what I need to do: Setup a simple zip code search form that will execute a script. A user enters the zip code and it redirects them to one of four pages which is associated with different zip codes (in the state of Georgia). If they enter a zip code that's not on the list, it would redirect them to a page that says something like "this zip code is not a part of the area....etc.". I know how to set up the form to execute the script, just don't know what the script itself should be. I'm assuming I won't need to use a DB, and that the zips would be in the script itself. Any ideas to help? Thanks.

coopster

2:34 pm on Sep 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, alivemedia.

This seems pretty straightforward. Are you familiar with PHP's control structures [php.net] for handling logic?

alivemedia

3:00 pm on Sep 23, 2005 (gmt 0)

10+ Year Member



I'm afraid I'm not. I was thinking that I could use several if else statements and try to figure it out from that end, but I'm not sure. With there being 4 location/pages that the zip codes are associated with, I'm not sure how to do multiple if else statements either. Been doing web design for a while but am just getting my feet wet in php and eventually CFM.

jatar_k

3:55 pm on Sep 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld alivemedia,

looks like you need a switch [php.net]

alivemedia

4:49 pm on Sep 23, 2005 (gmt 0)

10+ Year Member



Man, you guys rock. Thanks for the welcome and the info. One question. In the switch example given, it uses an echo command. How do you get the script to send the user/redirect to a new url?

Thanks.

jatar_k

5:00 pm on Sep 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



use header [php.net]

but also think this way the switch could just assign the url to a variable and then you could use a single header call at the end

$url = ''; 
switch ($i) {
case 0:
$url = '/page1.html';
break;
case 1:
$url = '/page2.html';
break;
case 2:
$url = '/page3.html';
break;
}
header("Location:" . $url);

alivemedia

3:52 am on Sep 24, 2005 (gmt 0)

10+ Year Member



question about the switch. When you identify what the variable should equal in the case, what if I want to use like more than one zip code per case instead of having to use one for each zip code (there are a bunch of zip codes). Does that make sense? Thanks for the help.

jatar_k

4:39 am on Sep 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could use drop through cases

$url = ''; 
switch ($i) {
case 0:
case 4:
case 6:
$url = '/page1.html';
break;
case 1:
$url = '/page2.html';
break;
case 2:
case 5:
$url = '/page3.html';
break;
}
header("Location:" . $url);

they dont have to be numbers like that either, they could also be strings

case 'zip1':

etc

alivemedia

12:11 pm on Sep 24, 2005 (gmt 0)

10+ Year Member



So, could it be something like this:

<?php
switch ($zip) {
case "30901, 30905":
header('Location: http://www.example.com/');
break;
}
?>

ergophobe

4:38 pm on Sep 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No, that would literally look for the 12-digit zip "30901, 30905"

You need to do it as in Jatar_K's last example.

How many zip codes total are you looking at? Beyond a certain number, you really would want to use a database. That number is partly dependent on how comfortable you are with databases, of course.

alivemedia

5:07 pm on Sep 24, 2005 (gmt 0)

10+ Year Member



I've got about 400 zip codes to point to the 4 different locations...it's a bunch. I've used databases some, but not a whole lot. Either way, I guess i have to enter the data so I may have to just include them in the script itself since I'm new to this. Any advice beyond that?

alivemedia

6:47 pm on Sep 24, 2005 (gmt 0)

10+ Year Member



One question:

what would be the code in the switch be if the the zip entered is not in any of the cases. this would be where they would be redirected to a page that says something like "We're sorry, that zip code is not a part of the network....etc."

jatar_k

9:25 pm on Sep 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you add a default case at the end

default:
$url = '/nomatch.html';
break;

ergophobe

4:36 pm on Sep 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



With 400 zip codes, I don't think I would do switch statements unless I was sure this was never going to change. I would probably do it with a database, but then I do everything with a database. Another method that would be simple to program, run faster than using a DB and be easier to maintain than switch statements, would be to do it like this.

Put all your zip codes in four arrays.
Use the in_array() function to test

$loc1 = array (53701, 53703, 53705);
$loc2 = array (53700, 53702, 53706);
$loc3 = array (53709);

// I don't know where you're info is coming from, but assume it's sent
// to the page as a GET param, which is a string no matter what (even if numeric)
// We grab just the first five chars and cast as an integer.
// We could test that it is numeric, but casting as an integer will strip
// out anything harmful and we'll test later to see whether it's one of our
// locations, which is a more restrictive test anyway.

$search_zip = (int) substr($_GET['location'], 0, 5);

// - next check to see which location array has this zip code
// if none, then location = 0 - not in our service area

if (in_array($search_zip, $loc1))
{
$location = 1;
}
elseif (in_array($search_zip, $loc2))
{
$location = 2;
}
elseif (in_array($search_zip, $loc3))
{
$location = 3;
}
else
{
$location = 0;
}

// Now redirect to the correct page or to the "Sorry no service page"

if ($location)
{
header("Location: http://example.com/locations/location" . $location . ".php");
}
else
{
header("Location: http://example.com/locations/noservice.php");
}

alivemedia

5:42 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



so, if I use arrays, I would use GET instea of POST for the form?

alivemedia

5:47 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



Also, in the arrays example, how would you redirect to the 3 different locations in addition to the no service page. I see the bottom portion, but it looks like it is sending the user to either one location or no service link?

alivemedia

7:41 pm on Sep 25, 2005 (gmt 0)

10+ Year Member



This is what I have so far. It just keeps going to the default 'out-network.php' however. Any idea what I'm doing wrong?

Here is the form code to call up the script:

<form name="" method="post" action="redirect.php">
<input name="zip" type="text" id="zip" size="10">
<input type="submit" name="Submit" value="Go">
<br>
</form>

redirect.php is the script file of course. Contains th e following code:

<?php

$url = '';
switch ($zip) {
case "30002":
case "30012":
case "30013":
case "30014":
case "30015":
case "30016":
case "30017":
case "30018":
case "30021":
case "30025":
case "30030":
case "30031":
case "30032":
case "30033":
case "30034":
case "30035":
case "30036":
case "30037":
case "30038":
case "30039":
case "30047":
case "30048":
case "30052":
case "30054":
case "30055":
$url = '/scottdale.php';
break;
default:
$url = '/out_network.php';
break;
}
header("Location:" . $url);

?>

jatar_k

4:38 pm on Sep 26, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are the four categories sequential?

you may be able to make ranges which could shorten your comparisons.

alivemedia

6:01 pm on Sep 26, 2005 (gmt 0)

10+ Year Member



the zips are not sequential, if that's what you mean. The 4 categories are 3 city names and 1 out of network name so I'm not sure if that's what you mean.

Honestly, i'm just trying to get this to work and for some reason cannot. It just keeps going to out of network link and i'm not sure what I've done wrong. I've tried to Debug it, but can't figure it out. :(