Calling a Function resultset as a variable...
Posted: Thu Oct 13, 2005 11:03 pm
Hi all.
I've been stumped on this problem for a while now. I have a function that calculates distance in between two points using latitude and longitude. The code itself works just fine... however, I need to be able to take the results of the function and use them as a variable in a loop. I have had absolutely no luck in doing this.
What I'm attempting to accomplish here is the random generation of a departure airport (using RAND, or POST), and the random generation of an arrival airport (again using RAND). Then the DISTANCE function calculates the distance in nautical miles between the two airports. Once that are established then an aircraft is selected (again with RAND or POST), and it's maximum range is selected (the $ac_rng variable). To finish the code I need to be able to run a loop that takes the aircraft's max range ($ac_rng) variable and compares it to the results of the DISTANCE function. If the DISTANCE function results are LESS THAN the max aircraft range, then the loop breaks. Otherwise I need it to continue to run the loop until a LESS THAN result is acheived.
The problem I'm having is that I cannot figure out how to get the results of the DISTANCE function into a variable I can use in the "IF" loop!
My code is below :
Does anyone have any ideas at all? I'm stumped... and could really use some help!
I've been stumped on this problem for a while now. I have a function that calculates distance in between two points using latitude and longitude. The code itself works just fine... however, I need to be able to take the results of the function and use them as a variable in a loop. I have had absolutely no luck in doing this.
What I'm attempting to accomplish here is the random generation of a departure airport (using RAND, or POST), and the random generation of an arrival airport (again using RAND). Then the DISTANCE function calculates the distance in nautical miles between the two airports. Once that are established then an aircraft is selected (again with RAND or POST), and it's maximum range is selected (the $ac_rng variable). To finish the code I need to be able to run a loop that takes the aircraft's max range ($ac_rng) variable and compares it to the results of the DISTANCE function. If the DISTANCE function results are LESS THAN the max aircraft range, then the loop breaks. Otherwise I need it to continue to run the loop until a LESS THAN result is acheived.
The problem I'm having is that I cannot figure out how to get the results of the DISTANCE function into a variable I can use in the "IF" loop!
My code is below :
Code: Select all
include('dbconnect.php');
// Create Distance Function //
function distance($departure_lat, $departure_long, $destination_lat, $destination_long) {
$dist = acos(sin(deg2rad($departure_lat)) // Calculates distance from latitude and longitude.
* sin(deg2rad($destination_lat))
+ cos(deg2rad($departure_lat))
* cos(deg2rad($destination_lat))
* cos(deg2rad($departure_long - $destination_long)));
$dist = rad2deg($dist);
$miles = (float) $dist * 69;
$nm = $miles * 0.8696;
$display = sprintf("%0.2f",$nm).' Nautical Miles' ;
$distance = sprintf("%0.2f", $nm);
return $distance ;
}
// Create Aircraft Query //
$ac = "SELECT * FROM aircraft WHERE aircraft_id = '3'";
$ac_result = @mysql_query($ac, $connect) or die(mysql_error());
while ($acrow = mysql_fetch_array($ac_result)) {
$ac_id = $acrow['aircraft_id'];
$ac_type = $acrow['aircraft_type'];
$ac_name = $acrow['aircraft_name'];
$ac_rng = $acrow['aircraft_rng'];
}
// Create Departure Query //
$dep_rand = rand(1, 14);
$dep = "SELECT * FROM ref_airport WHERE ap_id = '$dep_rand'";
$dep_result = @mysql_query($dep, $connect) or die(mysql_error());
while ($dprow = mysql_fetch_array($dep_result)) {
$departure_id = $dprow['ap_id'];
$departure_icao = $dprow['ap_icao'];
$departure_name = $dprow['ap_name'];
$departure_lat = $dprow['ap_lat'];
$departure_long = $dprow['ap_long'];
}
// Create Destination Query //
$dest_rand = rand(1, 14);
$destin = "SELECT * FROM ref_airport WHERE ap_id = '$dest_rand' AND ap_id != '$departure_id'";
$destin_result = @mysql_query($destin, $connect) or die(mysql_error());
while ($dsrow = mysql_fetch_array($destin_result)) {
$destination_id = $dsrow['ap_id'];
$destination_icao = $dsrow['ap_icao'];
$destination_name = $dsrow['ap_name'];
$destination_lat = $dsrow['ap_lat'];
$destination_long = $dsrow['ap_long'];
$distance = distance($departure_long['ap_long'], $departure_lat['ap_lat'], $destination_long['ap_long'], $destination_lat['ap_lat']);
if ($distance < $ac_rng) {
echo "Distance from $departure_icao ($departure_name) is ". distance($departure_lat, $departure_long, $destination_lat, $destination_long) ." : Aircraft Maximum range is $ac_rng.<br><br>";
break;
}
}