Page 1 of 1

Calling a Function resultset as a variable...

Posted: Thu Oct 13, 2005 11:03 pm
by Mr_Mako
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 :

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;
    }
}
Does anyone have any ideas at all? I'm stumped... and could really use some help!

Posted: Thu Oct 13, 2005 11:18 pm
by neophyte

Code: Select all

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']);
I don't understand why the function call has an array variable $departure_long['ap_long'] when $dsrow['ap_long'] is assigned to $destination_long. I bet this produces a warning if you put error_reporting(E_ALL); at the top of your PHP code.

Hope that helps

Posted: Fri Oct 14, 2005 12:10 am
by Mr_Mako
Actually... it doesn't. I don't get any warnings or anything.

As for the

Code: Select all

$distance = distance($departure_long['ap_long'], $departure_lat['ap_lat'], $destination_long['ap_long'], $destination_lat['ap_lat']);
in the loop, the only reason that's there is my attempt to try to force the function result into the $distance variable so I could use it in the "IF" statement below it.

Is there perhaps a better way to code this? I'm still relatively new to functions...

Posted: Fri Oct 14, 2005 8:47 am
by neophyte
See what this does:

Code: Select all

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, $departure_lat, $destination_long, $destination_lat);
    
    if ($distance < $ac_rng) {
       var_dump($distance);
    }
}

Posted: Fri Oct 14, 2005 1:11 pm
by Mr_Mako
If I put that in there, then I get weird outputs like :

Code: Select all

string(5) "30.14"
or

Code: Select all

string(6) "107.83"
Also, the distance calculation using

Code: Select all

$distance = distance($departure_long, $departure_lat, $destination_long, $destination_lat);
produces odd results that are off by hundreds of miles sometimes, and are never correct.

Posted: Sat Oct 15, 2005 6:39 pm
by Mr_Mako
Is it even possible to pass the FUNCTION results to a variable, as I'm attempting to do?

Posted: Sat Oct 15, 2005 11:57 pm
by John Cartwright
I don't understand what your saying, it seems the script is working as you expected. Perhaps what is being returned is the cause of your confusion. Var dump is simply giving you the var type, character length and var value.

I think your output to be more along the lines of..

Code: Select all

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, $departure_lat, $destination_long, $destination_lat);
    
    if ($distance < $ac_rng) {
       echo 'The distance between '.$departure_long.'-'.$departure_lat.' and '.$destination_long.'-'.$desination_lat.' is '. $distance.' miles <br />';
    }
}
Mr_Mako wrote:Is it even possible to pass the FUNCTION results to a variable, as I'm attempting to do?
Yes it is possible.

Posted: Sun Oct 16, 2005 3:14 am
by Mr_Mako
Well, thanks for your reply... but that's not what I need to accomplish.

I need to somehow get the function result passed to a variable so it can be used in the "IF ($distance < $ac_rng)" loop. Nothing I've tried works so far.

Posted: Sun Oct 16, 2005 4:14 am
by mickd
you can do

Code: Select all

if(distance(blah, blah, blah, blah) < $ac_rng) {
//do
}
or

Code: Select all

$distance = distance(blah, blah, blah, blah);
if($distance < $ac_rng) {
//do
}

Posted: Sun Oct 16, 2005 10:09 am
by John Cartwright
Mr_Mako wrote:Well, thanks for your reply... but that's not what I need to accomplish.

I need to somehow get the function result passed to a variable so it can be used in the "IF ($distance < $ac_rng)" loop. Nothing I've tried works so far.

Code: Select all

$distance = distance($departure_long, $departure_lat, $destination_long, $destination_lat);
You already did it...

Posted: Sun Oct 16, 2005 8:49 pm
by Mr_Mako
Jcart wrote:

Code: Select all

$distance = distance($departure_long, $departure_lat, $destination_long, $destination_lat);
You already did it...
Trouble is... that doesn't work. If I call the variable with an "echo" so I can see what the output is, the output is 0.00. That was actually the first thing I tried to do because I figured it was that easy to do.