2016-04-25 9 views
0

Ich verwende Google Distance Matrix, um die Entfernung zwischen zwei Standorten für eine Taxi-Website zu berechnen. Ich muss eine Bedingung prüfen, um festzustellen, ob der eine der Standorte ein Londoner Flughafen ist, und wenn ja, eine Nachricht zurückgeben, anstatt die Kosten der Reise zu berechnen.Verschachtelte PHP bedingte Statement-Struktur Problem

Es funktionierte perfekt gestern und dann heute ... der Code ist biserk gegangen. Es gibt nur die Nachricht zurück, wenn Abhol- und Abladestellen beide Flughäfen sind, aber wenn nur eine ist, berechnet es die Kosten für die volle Entfernung, die meinen Zweck verfehlt. Im Folgenden ist die Struktur Code und ich frage mich, ob ich die falsche verschachtelte bedingte Syntax in Zeile bin mit 39, als ich die strpos() Test zu starten ....

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

Warum muss das letzte 'strpos()' in Ihrer if-Anweisung '=== false' sein und die anderen nicht? Fehle ich etwas ...? Wenn die Zeichenkette in Position 0 Ihrer Zeichenkette gefunden wird, dann wird 'if (0)' nie wahr sein. – Henders

+0

Sicherlich möchten Sie nach Flughäfen suchen, BEVOR Sie eine minimale Distanzberechnung machen – RiggsFolly

Antwort

0

Ich schlage vor, die Prüfung als eine Funktion zu implementieren. Zum Beispiel

/** 
* 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; 
} 

Bitte beachte, dass wir strpos Ergebnis mit false mit !== Operator vergleichen, weil strpos Null zurückkehren könnte, die ==-false ist.

Als nächstes könnte interne Prüfung so aussehen

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

Beachten Sie, dass wir prüfen, ob entweder $toairport sind $ fromairport` Variablen sind Flughafenstandorte. Ich denke, das ist das, was das Skript tun soll.

Wenn wir Funktionen wie diese verwenden, ist es sehr einfach, das Skript zu lesen und zu verstehen, was es macht. Außerdem erleichtert es das Ändern des Skripts, indem ein neuer Speicherort hinzugefügt oder die Logik verbessert wird. Zum Beispiel möchten wir vielleicht, dass die Groß-/Kleinschreibung nicht beachtet wird.

+0

Victor, das ist absolut brillant, einfach, sauber, einfach zu verstehen und funktioniert perfekt! – TerryAlly