using array_intersect
-
22-06-2021 - |
Question
I have 2 tables. One with a list of businesses and their addresses the other with a list of zip codes and their longitude and latitude. I have a form that accepts a zip code and a number of miles to check radius.
all the radius stuff works from a script i found online. what I need is to echo the entries of businesses that are within the radius.
So far i have 2 queries. one gets all entries zip codes inside the db the other get all zipcodes within the range i put in the form
i'm using array_intersect to find the matching ones. my problem is it only returns the first entry found. I'm SURE i need a while loop to do this but i have no idea how to do it.
code snippet:
$insideRadius = array_intersect($data, $zipArray);
The $data is all the zip codes from the business table. the $zip array is all the zip codes within range.
Solution
Can't you just select the businesses that have a zipcode listed in the $zipArray?
"SELECT * FROM business WHERE zipcode IN (" . implode(",", $zipArray) . ")"
In PHP you could run it like this:
$result = mysql_query("SELECT * FROM business WHERE zipcode IN (" . implode(",", $zipArray) . ")");
while ($row = mysql_fetch_assoc($result))
{
echo $row["name"] . "<br />";
echo $row["zipcode"] . "<br />";
}