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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.