0

i am writing this web app to calculate the air travel distance between two cities, if i fill the departure airport and destination airport it will calculate directly. If i filled 'via' input field then i have to take it as a middle city to calculate the travel distance. I don't know how to set condition depends on if via is filled. where's wrong with my code? i have the input field connected to my database and retrieve information. This is my html part:

  <div>
        From
        <div class="textinput">
            <input type="text" id="dept" name="departure" placeholder="City name or aiport code" >
        </div>
    </div>

    <div>
        To
        <div>
            <input type="text" id="dest" placeholder="City name or airport code" >
        </div>
    </div>

    <div>
        Via
        <div>
            <input type="text" id="via" placeholder="City name or airport code" >
        </div>
    </div>

here is the php:

       if(isset($_POST['dept'], $_POST['dest'])){
      $dept=$_POST['dept'];
      $dest=$_POST['dest'];

   }

mysql_connect("localhost","ccc","aaa") or die (mysql_error());
    mysql_select_db("ccc") or die(mysql_error());

    $miles = 0;
    $co2 = 0;

    if(isset($_POST['via'])){
        indirect();

    }
    else{
        direct();

    }


    function direct(){

    $strSQL1 = "SELECT display, lat, longi FROM airport WHERE display = '$dept'";
    $rs1 = mysql_query($strSQL1);
    $row1 = mysql_fetch_array($rs1);
    $lat1= $row1['lat'];
    $long1= $row1['longi'];

    $strSQL2 = "SELECT display, lat, longi FROM airport WHERE display = '$dest'";
    $rs2 = mysql_query($strSQL2);
    $row2 = mysql_fetch_array($rs2);
    $lat2= $row2['lat'];
    $long2= $row2['longi'];

    $earthradius = 6366.707;    
    $km_to_miles = 1/1.609344;

    $dlat = ($lat2-$lat1) * (M_PI / 180);
    $dlon = ($long2-$long1) * (M_PI / 180);

    $a = sin($dlat / 2) * sin($dlat / 2) + sin($dlon / 2) * sin($dlon / 2) * cos($lat1 * (M_PI / 180)) * cos($lat2 * (M_PI / 180));
    $c = 2 * atan2(sqrt($a), sqrt(1-$a));
    $d = $c * $earthradius;

    $miles = $d * $km_to_miles;

    $co2 = (($miles / 41.986) * 20.88 * 1.9) / 2204.6; 

    }

    function indirect(){
    $strSQL1 = "SELECT display, lat, longi FROM airport WHERE display = '$dept'";
    $rs1 = mysql_query($strSQL1);
    $row1 = mysql_fetch_array($rs1);
    $lat1= $row1['lat'];
    $long1= $row1['longi'];

    $strSQL2 = "SELECT display, lat, longi FROM airport WHERE display = '$dest'";
    $rs2 = mysql_query($strSQL2);
    $row2 = mysql_fetch_array($rs2);
    $lat2= $row2['lat'];
    $long2= $row2['longi'];

    $strSQL3 = "SELECT display, lat, longi FROM airport WHERE display = '$via'";
    $rs3 = mysql_query($strSQL3);
    $row3 = mysql_fetch_array($rs3);
    $lat3= $row3['lat'];
    $long3= $row3['longi'];

    $earthradius = 6366.707;    
    $km_to_miles = 1/1.609344;

    $dlat1 = ($lat3-$lat1) * (M_PI / 180);
    $dlon1 = ($long3-$long1) * (M_PI / 180);

    $a1 = sin($dlat1 / 2) * sin($dlat1 / 2) + sin($dlon1 / 2) * sin($dlon1 / 2) * cos($lat1 * (M_PI / 180)) * cos($lat3 * (M_PI / 180));
    $c1 = 2 * atan2(sqrt($a1), sqrt(1-$a1));
    $d1 = $c1 * $earthradius;

    $miles1 = $d1 * $km_to_miles;

    $dlat2 = ($lat3-$lat2) * (M_PI / 180);
    $dlon2 = ($long3-$long2) * (M_PI / 180);

    $a2 = sin($dlat2 / 2) * sin($dlat2 / 2) + sin($dlon2 / 2) * sin($dlon2 / 2) * cos($lat2 * (M_PI / 180)) * cos($lat3 * (M_PI / 180));
    $c2 = 2 * atan2(sqrt($a2), sqrt(1-$a2));
    $d2 = $c2 * $earthradius;

    $miles2 = $d2 * $km_to_miles;

    $miles = $miles1 + $miles2;
    $co2 = (($miles / 41.986) * 20.88 * 1.9) / 2204.6; 
    }



    echo json_encode(array('co2' => $co2,'miles'=>$miles));



  ?>
  • [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Joseph Silber Mar 24 '14 at 03:29

2 Answers2

0

I think you're having trouble with the scope of variables $co2 and $miles.

Put your echo json_encode(array('co2' => $co2,'miles'=>$miles)); inside of both direct() and indirect().

if(isset($_POST['via'])){
        indirect($dept, $dest);

    }
    else{
        direct($dept, $dest);

    }


    function direct($dept, $dest){


//do some stuff here

}

    function indirect($dept, $dest){

//do some stuff here

}
Lee Salminen
  • 900
  • 8
  • 18
  • Thanks, but it still doesn't work. the result is 0.00 – user3371790 Mar 24 '14 at 03:32
  • ah yes, also you need to pass in `$dest` and `$dept` into your functions. They are also out of scope. See my updated answer above. – Lee Salminen Mar 24 '14 at 03:33
  • Unfortunately I'm not a much of a math guy, so I couldn't help you with calculating the distance around a sphere. That's probably going to be another question entirely. Try posting a new question with a different tag. If you don't mind, please mark my answer as accepted :). – Lee Salminen Mar 24 '14 at 04:04
0

You have to return something from your functions, like this:

function direct($dept, $dest)
{
    // ...
    return [$co2, $miles];
}

function indirect($dept, $dest, $via)
{
    // ...
    return [$co2, $miles];
}

It returns an array with $co2 and $miles being the first and second element respectively. Then, in your code:

if (isset($_POST['via'])) {
    list($co2, $miles) = indirect($dept, $dest, $_POST['via']);
} else {
    list($co2, $miles) = direct($dept, $dest);
}

That said, you should really consider using PDO / mysqli and learn about prepared statements; alternatively, at the very least you should escape variables inside SQL statements using mysql_real_escape_string(), e.g.:

$strSQL1 = sprintf("SELECT display,lat,longi FROM airport WHERE display='%s'", 
    mysql_real_escape_string($dept)
);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309