Distance search
Posted: Tue Sep 13, 2005 10:32 pm
feyd | Please use
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
andCode: 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
andCode: 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]