2016-04-25 19 views
0

Google Distance Matrixを使用して、タクシーのウェブサイトの2つの場所間の距離を計算しています。私は、場所の1つがロンドン空港であるかどうかを確かめるために条件をテストし、もしそうなら旅行の費用を計算するのではなくメッセージを返す必要があります。入れ子のPHP条件文の構造の問題

これは完全に昨日働いていましたが、今日は...コードは二重になりました。ピックアップとドロップオフの場所が両方の空港である場合にのみメッセージを返しますが、1つだけであれば、私の目的を凌駕する完全な距離のコストを計算しています。以下のコード構造であると私はstrpos()テストを開始するとき、私は39行で間違ったネストされた条件の構文を使用していますかどうかを疑問に思って....

if (isset($_POST['submitted'])): 
     $origin = urlencode($_POST['origin']); 
     $destination = urlencode($_POST['destination']); 

     // Insert encoded url variables 
     $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=$origin&destinations=$destination&mode=driving&keyy={API KEY}"; 
     $json = file_get_contents($url); // get the data from Google Maps API 

     $status = $result['rows'][0]['elements'][0]['status']; 

    if (status is OK): 
     // Calculate the distance 
     $status = ...; 
     $DistanceMetres = .....; 
     $Distance = .....; // converted to yards 

     // Calculate the Day Rate 
     .... 

     // Calculate the Night Rate 
     .... 

     // Calculate the Sunday Rate 
     .... 

     // Calculate the Christmas & NYE Rate 
     .... 

     // Set up variables for pick-up & drop-off locations 
     $toairport = $result['destination_addresses'][0]; 
     $fromairport= $result['origin_addresses'][0]; 

     if (distance is the minimum distance) { 
     echo the minimum cost of the trip 

     } else { //ie if the distance is more than the minimum distance 

      // Check to see if pick-up or drop-off destination is an airport 
      if (strpos($toairport, 'Heathrow') || strpos($toairport, 'Gatwick') || strpos($toairport, 'London Luton') || strpos($toairport, 'London City Airport') || strpos($fromairport, 'Heathrow') || strpos($fromairport, 'Gatwick') || strpos($fromairport, 'London Luton') || strpos($fromairport, 'London City Airport') === false) { 
       echo the cost of the trip 

      // But if at least one location is an airport 
      } else { 
      echo a message saying a special flat rate is available for airport transfers 
      } 
     } 

    else: 
    echo that status is not okay 
    endif; 

else: 

display input form 

endif; 
+0

if文の最後の 'strpos()'は '=== false'でなければならないのはなぜですか?私は何かを逃していますか? また、文字列の位置0に文字列がある場合、 'if(0)'は決してtrueになりません。 – Henders

+0

最低距離計算を行う前に必ず空港を確認してください – RiggsFolly

答えて

0

私は機能としてチェックを実装することをお勧めします。例えばstrposfalseから==であるゼロを返すことがありますので、我々は!==演算子を使用してfalsestrpos結果を比較

/** 
* Check if the location is an airport. 
* 
* @param string $location 
* @return bool 
*/ 
function isAirport($location) 
{ 
    $airportList = ['Heathrow', 'Gatwick', 'London Luton', 'London City Airport']; 
    foreach ($airportList as $airport) { 
     if (strpos($location, $airport) !== false) { 
      return true; 
     } 
    } 

    return false; 
} 

注意。

次に、内部チェックは、我々はどちらか$toairportは$ fromairport`変数は空港の場所ですしていることを確認し、この

if (isAirport($toairport) || isAirport($fromairport)) { 
    echo 'a message saying a special flat rate is available for airport transfers'.PHP_EOL; 
} else { 
    echo 'the cost of the trip'.PHP_EOL; 
} 

ノートのように見えるかもしれません。私はこれがスクリプトがしたいものだと思います。

このような関数を使用すると、スクリプトを読みやすくすることができます。また、新しい場所を追加したり、ロジックを改善したりすることで、スクリプトを簡単に変更できます。たとえば、小文字を区別しないようにすることができます。

+0

Victor、それは絶対にです鮮やかで、シンプルで、きれいで、理解しやすく、完璧に動作します! – TerryAlly