0

I'm having a few problems with a function that I would like to call from within a while loop. Obviously I can't include the function itself within the loop but it seems to me that the function requires variables created by the loop so calling the function from within the loop becomes a problem as well (at least for me; I get no results).

Here is the function:

function distance($lat1, $lon1, $lat2, $lon2, $unit) { 

  $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 * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344); 
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}
$miles =  round(distance($lat1, $lon1, $lat2, $lon2, $unit));

So I'm looking to do something like this :

while($r=mysql_fetch_array($res)){
$lat1=$r['lat1'];
$lon1=$r['lon1'];

///// Function goes here ///// 

echo ' '.$results.' '; }

I get no results when I call the function from within the loop where I know there are results to be had. I tried to include just the calculation the function performs but that didn't seem to work either. Any ideas on how I can get this to work?

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by CFROG
0

Sorry, I got it. As is usually the case I was overlooking something basic.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.