如何通过php中的分支条件返回全局变量?

时间:2014-03-24 03:26:50

标签: php

我正在编写这个网络应用来计算两个城市之间的航空旅行距离,如果我填写出发机场和目的地机场,它将直接计算。 如果我填写“通过”输入字段,那么我必须将其作为中间城市来计算旅行距离。我不知道如何设置条件取决于via是否填充。我的代码哪里错了?我有输入字段连接到我的数据库并检索信息。 这是我的HTML部分:

  <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>

这是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));



  ?>

2 个答案:

答案 0 :(得分:0)

我认为您在变量范围$co2$miles方面遇到了问题。

将echo json_encode(array('co2' => $co2,'miles'=>$miles));放在direct()和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

}

答案 1 :(得分:0)

您必须从函数中返回一些内容,例如:

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

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

它返回一个数组,$co2$miles分别是第一个和第二个元素。然后,在您的代码中:

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

那就是说,你应该考虑使用PDO / mysqli来学习准备好的陈述;或者,至少应该使用mysql_real_escape_string()来转义SQL语句中的变量,例如:

$strSQL1 = sprintf("SELECT display,lat,longi FROM airport WHERE display='%s'", 
    mysql_real_escape_string($dept)
);