Ok, I have the following formula that shows all the zip codes that fall within a certain distance. (it is actually a query)
function inradius($zip,$radius)
{
$query= "SELECT * FROM zipcode WHERE zipcode='$zip'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$lat=$row["lat"];
$lon=$row["lon"];
$query = "SELECT * FROM zipcode WHERE (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+POW((69.1*(lat-\"$lat\")),\"2\"))<($radius*$radius) ";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$zipArray[]=$row;
}
return $zipArray;
}
} else {
return "Zip Code not found";
}
}
Ok, so that works great and shows all zipcodes in that table that is within a certain radius that user specifies.
However, I also have a table that has dealership info plus their zipcodes. I want the query to get all dealerships (with their zipcodes) that fall within the radius.. So far i have this but it does not seem to be working or puting out anything...
function inradius($zip,$radius)
{
$query = "SELECT * FROM zipcode WHERE zipcode='$zip'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$lat=$row["lat"];
$lon=$row["lon"];
$query = "SELECT * .products FROM zipcode, products WHERE (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+POW((69.1*(lat-\"$lat\")),\"2\"))<($radius*$radius) AND zip.products = zipcode.zipcode ";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$zipArray[]=$row;
}
return $zipArray;
}
} else {
echo "Zip Code not found";
}
}
I have this finish both (know it works)
$zipCode = $_POST["zipCode"];
$radius = $_POST["radius"];
$zipArray = inRadius($zipCode,$radius);
What is wrong here?? The table is called 'products' and the column that holds the zip is called 'zip'. The zipcode table is called 'zipcode' and the column is 'zipcode' that holds the zips. I need the zip table because it holds all zip codes in US and has latitude longitude coordinates.
What am I doing wrong?
Thanks
Ryan