Distance search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Daley
Forum Newbie
Posts: 3
Joined: Tue Sep 13, 2005 10:12 pm
Location: Houston, TX

Distance search

Post by Daley »

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hey all, I'm developing a site that's got one of those "nearest store to me" thingy's in it, and I'm not sure how to make it do what I want. Right now, I've got a list of stores with their locations, a table of zips and their respective latitude/longitude so I can display distance from a given zip, but I'm not sure how to sort by distance. Heck, maybe I'm going about it all wrong, but here's how I get to where I'm at now:

sql returns a result based on a keyword search, where one of the array elements is the zip.
I take that resulting array, iterate through it with a while() statement, and look up lat/lon for each line in the array. Each iteration performs another query to look up lat/lon and does the math for the distance from the given zip. So in essence, if my result returned 1000 results, I'd be doing 1000 queries for every keyword search a user performs. I have however added the limit 0, 30 to the end of my sql statement to avoid this.

ultimately, I want to sort by zip, obviously in ascending order. 

I know that I could stuff all of this back into a temporary table and sort from there, but there's gotta be a better way. 

Here's part of the code I'm using:

Code: Select all

function results_table($result)
	{
	// the results table
	include "functions/function.distance.php";  // this function is what actually does the math for the distance calc
	while ($get_info = mysql_fetch_array($result)){ 
	// if we're authorized, make the store names into links so admin can edit 'em
		if($_SESSION['level'] > 2) { 
			$link_start = "<a href=\"scripts/edit.php?id=" . $get_info[6] . "\" target=\"_new\">";
			$link_end = "</a>";
		} else { 
			$link_start = ""; $link_end = ""; 
		} // end of if($_SESSION['level'] > 2

	$post_zip = $_POST['zip'];
	$zip2 = $get_info[3];

		// finally spit out the table with the requested info in it
		$distance = round(distance($post_zip, $zip2), 2);
		if ($distance == "0") { $distance = "<1"; } elseif (!$post_zip) { $distance = " "; }
		print "<hr width=\"95%\">";
		print "<table border=\"0\" width=\"100%\">\n";
		print "<tr>\n"; // row 1
                ..........  and of course the rest of the result table.........

Now, I'll admit that I'm not at all a coder - I'm really just fumbling through this in an effort to learn, so if I'm way off base please feel free to set me straight. I will say however that I'm not the type to register just to ask a question that I haven't researched - I *have* done my homework, and have resorted to asking for help because I've come up with nothing. I'd really appreciate anything in the form of constructive criticizm or guidance. If flaming is necessary to help me out, then I guess that's a pain of learning I'll have to endure..

Thanks in advance!!


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Daley
Forum Newbie
Posts: 3
Joined: Tue Sep 13, 2005 10:12 pm
Location: Houston, TX

Post by Daley »

feyd is absolutely correct - I should have read the sticky FIRST, and THEN posted. Please accept my apologies, as well as my gratitude for fixing my mistake.
Daley
Forum Newbie
Posts: 3
Joined: Tue Sep 13, 2005 10:12 pm
Location: Houston, TX

Post by Daley »

I don't mean to be a pest, but does anyone have any advice for me?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Check out http://www.planet-source-code.com and search for ZIP in PHP codes. There's a nice search by distance class in there.. code of the year winner I believe.

You can pick apart his code.. or just use the class :-P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply