Forum Moderators: coopster

Message Too Old, No Replies

zipcode range from closest to furthest

         

sfast

7:39 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



I am learning PHP and am trying to get Zipcode Locator working.

I am using the script by Micah Carrick.

I could get it working with the zipcodes printed within a certain range. But am unable to provide the functionality for range from closest to furthest.

The distance is obviously not stored in database.

Any ideas/help?

sfast

10:33 pm on Mar 9, 2007 (gmt 0)

10+ Year Member



Can I use temporary table to store nearest zipcodes obtained from the query and the respective distances?
Will there be any problem if two persons try to attempt to search the zipcodes table?

Is there any other solution?

coopster

11:24 pm on Mar 9, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, sfast.

You could use the same calculated distance as your sort order in the ORDER BY clause of your query statement.

sfast

5:03 am on Mar 10, 2007 (gmt 0)

10+ Year Member



echo " Print the nearest locations <br>";

foreach ($zips as $key => $value) {

$sql = "SELECT loc_name, zip_code_loc , state_prefix from locations WHERE zip_code_loc = $key order by zip_code_loc";

$sql = mysql_query($sql);

$miles = $z->get_distance( $zipcode, $key);

while ($row1 = mysql_fetch_array($sql)) { //2

echo "Zipcode : {$row1['zip_code_loc']} <br>";
echo "Printing the miles ";
echo "$miles <br>";

} //2

I was doing with "order by" zipcodes where the locations are.
But the result shows -

Results

Zipcode : 70062
Printing the miles 2.99

Zipcode : 70062
Printing the miles 2.99

Zipcode : 70094
Printing the miles 2.09

Zipcode : 70121
Printing the miles 2.98

[/code]

My problem, I want 70094 with miles 2.09 to be printed at first, followed by 70121 with 2.98 miles and so on ....

coopster

9:40 pm on Mar 11, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm sorry, I thought you were calculating the distance in the sql, not from the result set. You could load an array instead of outputting your data. Sort the array, then output.

sfast

9:27 pm on Mar 13, 2007 (gmt 0)

10+ Year Member



Thanks for the advice.

One more doubt -

what is the way to calculate the nearest locations when somebody enters his/her address for the nearest locations and not entering just the zipcode?

coopster

10:56 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Close to impossible. I say close because you might get close if you had every address for every city for every state for every country in your database tables. Highly unlikely. Most folks know their zip code, if they don't you can provide a lookup by city/state or something along those lines.