Hi...
I've been stuck on this for the last few weeks, and am no closer to a solution.
I have code where a user can select (through a POST menu) a departure airport, and then the code randomly generates a arrival airport. The database table that the airports reside in also includes latitude and longitude data for each airport. A function then calculates the distance between the two airports utilizing the latitude and longitude of each.
Included also is the code that randomly (or selectively by POST) grabs the data from the aircraft table for the aircraft selected, and this includes the maximum range of the aircraft.
What I need to accomplish is to run a query / statement that will take the aircraft range data, and then loop through the departure / arrival function until two airports are selected where the distance between them is LESS THAN the maximum range of the aircraft.
I have no idea how to code this to accomplish what I'm attempting to do.
I know that's odd based on my code below but I've never used or had the need to learn Loops or the like before... and I'm just not fully understanding how to implement or code one to do what I need. The possibility also exists that I may not even need a loop... it's been suggested to just run the whole thing through a While statement... would that work? I haven't tried that because it's my understanding that a While statement (like, "WHERE $range < '$ac_rng'") won't loop through the results the way I need them to.
My code for each is listed below :
// Create Aircraft Query //
$ac = "SELECT * FROM aircraft WHERE aircraft_id = '$_POST[aircraft]'";
$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_range'];
}
// Create Departure Query //
$dep = "SELECT * FROM ref_airport WHERE ap_id = '$_POST[departure]'";
$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 //
$destrand = rand(1, $destnum);
$destin = "SELECT * FROM ref_airport WHERE ap_id = '$destrand'";
$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'];
}
// Create Distance Function //
distance($departure_long['longitude'], $departure_lat['latitude'], $destination_long['longitude'], $destination_lat['latitude']);
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' ;
return $display ;
}
I'd really appreciate any suggestions on how I can accomplish this. This one bit of code is hanging up the whole project, and I'd really like to resolve this.