I am trying pull off a mysql subquery here that pulls data from the Member table based on output from the zip_codes table. But because the variable in the loop is an array, it doesn't seem to work. Not sure how to fix it although I'm assduming (hoping) that it is a simple fix.
Below is the code. Thanks so much for the help.
$link = mysql_connect("localhost", "", "");
if (!$link) {
die('Could not connect: ' . mysql_error());}
mysql_select_db("KeyEstateSales");
function close_zipcodes($zipcode, $distance, $unit){
// SWITCH BETWEEN KILOMETERS AND MILES
if($unit=="K" || $unit=="k" ){
$unit1 = 6378.39;
$unit2 = 1;
}
else{
$unit1 = 6378.39;
$unit2 = 1.609344;
}
$search= "SELECT * FROM zip_codes WHERE zip='$zipcode'";
$result = mysql_query($search) or die('Error: ' . mysql_error());
$row = mysql_fetch_assoc($result);
$lng = $row["longitude"];
$lat = $row["latitude"];
mysql_free_result($result);
$search2 = "SELECT * FROM Members WHERE Zip IN (SELECT zip, (ACOS((SIN(RADIANS(" .$lat."))*SIN(RADIANS(latitude))) + (COS(RADIANS(" .$lat."))*COS(RADIANS(latitude))*COS(RADIANS(longitude)-RADIANS(" .$lng. ")))) * ".$unit1.") AS distance FROM zip_codes WHERE (ACOS((SIN(RADIANS(" .$lat. "))*SIN(RADIANS(latitude))) + (COS(RADIANS(" .$lat. "))*COS(RADIANS(latitude))*COS(RADIANS(longitude)-RADIANS(" .$lng. ")))) * ".$unit1.") <= " .$distance*$unit2. " ORDER BY distance ASC");
$result = mysql_query($search2) or die('Error: ' . mysql_error());
$x=-1;
while ($row2 = mysql_fetch_array($result, MYSQL_ASSOC)) {
if($x==-1){}
else{
$array[$x][0]= $row2["zip"];
$array[$x][1]= $row2["distance"]*.621371192;
}
$x++;
}
mysql_free_result($result);
return $array;
}
function distance55($lat1, $lon1, $lat2, $lon2, $unit9) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 69.09;
$unit9 = strtoupper($unit9);
if ($unit9 == "K") {
return ($miles * 1.609344);
} else if ($unit9 == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
$zip1 = $HTTP_GET_VARS['zip1'];
$radius = $HTTP_GET_VARS['radius'];
if($radius !=""){
$array = close_zipcodes($zip1,$radius,"M");
$x=0;
while($x<count($array)) {
$thezip = $array[$x][0];
echo $row_result['City'];
echo " ";
echo $array[$x][0];
echo " ";
echo round($array[$x][1], 1);
echo"<br>";
$x++;
}
}