2016-10-10 8 views
2

Also die Frage ist verwirrend, ich weiß. Ich frage mich, wie ich das machen soll.In PHP, wie machst du eine if-Anweisung innerhalb der if einer if-Anweisung?

Ich habe nach dem if-Anweisung:

if(
    (isset($_POST['billing_company']) && $_POST['billing_company'] != "") && 
    (isset($_POST['billing_address']) && $_POST['billing_address'] != "") && 
    (isset($_POST['billing_city']) && $_POST['billing_city'] != "") && 
    (isset($_POST['billing_state']) && $_POST['billing_state'] != "") && 
    (isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") && 
    (isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") && 
    (isset($_POST['location_name']) && $_POST['location_name'] != "") && 
    (isset($_POST['location_address']) && $_POST['location_address'] != "") && 
    (isset($_POST['location_city']) && $_POST['location_city'] != "") && 
    (isset($_POST['location_state']) && $_POST['location_state'] != "") && 
    (isset($_POST['location_zip']) && $_POST['location_zip'] != "") && 
    (isset($_POST['location_phone']) && $_POST['location_phone'] != "") && 
    (isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") && 
    (isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") && 
    (isset($_POST['user_username']) && $_POST['user_username'] != "") && 
    (isset($_POST['user_email']) && $_POST['user_email'] != "") && 
    (isset($_POST['user_password']) && $_POST['user_password'] != "") && 
    (isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") && 
    (isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") && 
    (isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") && 
    (isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") && 
    (isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") && 
    (isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") && 
    (isset($_POST['terms']) && $_POST['terms'] != "") 
){ 

jedoch auf einige Variablen, kann ich nicht mehr benötigen, eine der Abrechnungsinformationen. Also ich frage mich, ob ich einen IF innerhalb der IF tun, so etwas wie diese:

if(
    ($billingRequired == 1){ 
     (isset($_POST['billing_company']) && $_POST['billing_company'] != "") && 
     (isset($_POST['billing_address']) && $_POST['billing_address'] != "") && 
     (isset($_POST['billing_city']) && $_POST['billing_city'] != "") && 
     (isset($_POST['billing_state']) && $_POST['billing_state'] != "") && 
     (isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") && 
     (isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") && 
    } 
    (isset($_POST['location_name']) && $_POST['location_name'] != "") && 
    (isset($_POST['location_address']) && $_POST['location_address'] != "") && 
    (isset($_POST['location_city']) && $_POST['location_city'] != "") && 
    (isset($_POST['location_state']) && $_POST['location_state'] != "") && 
    (isset($_POST['location_zip']) && $_POST['location_zip'] != "") && 
    (isset($_POST['location_phone']) && $_POST['location_phone'] != "") && 
    (isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") && 
    (isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") && 
    (isset($_POST['user_username']) && $_POST['user_username'] != "") && 
    (isset($_POST['user_email']) && $_POST['user_email'] != "") && 
    (isset($_POST['user_password']) && $_POST['user_password'] != "") && 
    (isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") && 
    (isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") && 
    (isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") && 
    (isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") && 
    (isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") && 
    (isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") && 
    (isset($_POST['terms']) && $_POST['terms'] != "") 
){ 

Ich bin mir ziemlich sicher, dass es nicht aber ich wollte mit Menschen überprüfen, klüger als ich. Ich weiß, dass ich eine Seite in der {} nisten kann, aber wollte nicht jede Variable in einem sehr tiefen Nest überprüfen müssen.

Danke,

+1

Haben Sie es tatsächlich versucht, um zu sehen, was passiert? – patricksweeney

+0

Ja. Die Seite wird aufgrund eines Fehlers nicht gerendert. – tvirelli

+0

Ich nehme an, Sie haben keine Fehler melden auf dann? – patricksweeney

Antwort

3

Fügen Sie einfach() um den Abschnitt, den Sie verbinden möchten, und es wird zu einem einfachen boolean lösen, die Ihre enthalten sein können, in if-Anweisung

Die Logik hier liest „if (Abrechnung ist nicht erforderlich oder Rechnungs alle Felder ausgefüllt werden) und alle nicht-Abrechnungs Felder ausgefüllt sind, dann ...“

if(
    (($billingRequired != 1) || (
     (isset($_POST['billing_company']) && $_POST['billing_company'] != "") && 
     (isset($_POST['billing_address']) && $_POST['billing_address'] != "") && 
     (isset($_POST['billing_city']) && $_POST['billing_city'] != "") && 
     (isset($_POST['billing_state']) && $_POST['billing_state'] != "") && 
     (isset($_POST['billing_zip']) && $_POST['billing_zip'] != "") && 
     (isset($_POST['billing_phone']) && $_POST['billing_phone'] != "") 
    )) 
    && 
    (
     (isset($_POST['location_name']) && $_POST['location_name'] != "") && 
     (isset($_POST['location_address']) && $_POST['location_address'] != "") && 
     (isset($_POST['location_city']) && $_POST['location_city'] != "") && 
     (isset($_POST['location_state']) && $_POST['location_state'] != "") && 
     (isset($_POST['location_zip']) && $_POST['location_zip'] != "") && 
     (isset($_POST['location_phone']) && $_POST['location_phone'] != "") && 
     (isset($_POST['user_firstname']) && $_POST['user_firstname'] != "") && 
     (isset($_POST['user_lastname']) && $_POST['user_lastname'] != "") && 
     (isset($_POST['user_username']) && $_POST['user_username'] != "") && 
     (isset($_POST['user_email']) && $_POST['user_email'] != "") && 
     (isset($_POST['user_password']) && $_POST['user_password'] != "") && 
     (isset($_POST['user_mobile']) && $_POST['user_mobile'] != "") && 
     (isset($_POST['payment_cc_name']) && $_POST['payment_cc_name'] != "") && 
     (isset($_POST['payment_cc_number']) && $_POST['payment_cc_number'] != "") && 
     (isset($_POST['payment_cc_expo_month']) && $_POST['payment_cc_expo_month'] != "") && 
     (isset($_POST['payment_cc_expo_year']) && $_POST['payment_cc_expo_year'] != "") && 
     (isset($_POST['payment_cc_code']) && $_POST['payment_cc_code'] != "") && 
     (isset($_POST['terms']) && $_POST['terms'] != "") 
    ) 
{ 
    //statements 
} 

Obwohl ich vermuten, dass dieses in einer Schleife besser gemacht werden könnte, vielleicht ist es persönliche Präferenz, aber ich wäre glücklicher mit etwas wie dies

$billingRequiredFields = array('billing_company','billing_address','billing_city','billing_state','billing_zip','billing_phone','location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms'); 
$billingNotRequiredFields = array('location_name','location_address','location_city','location_state','location_zip','location_phone','user_firstname','user_lastname','user_username','user_email','user_password','user_mobile','payment_cc_name','payment_cc_number','payment_cc_expo_month','payment_cc_expo_year','payment_cc_code','terms') 
$requiredFields = ($billingRequired == 1) ? $billingRequiredFields : $billingNotRequiredFields; 
$continue = true; 
foreach($requiredFields as $field) { 
    if (!isset($_POST[$field]) || $_POST[$field] == '') { 
     $continue = false; 
     break; 
    } 
} 

if ($continue) { 
    // statements 
} 
+0

Um die Lesbarkeit zu verbessern, sollten Sie die beiden Ausdrücke $ billingRequired in Klammern einschließen. – arkascha

+0

Das hat funktioniert. Ich musste das nachstehende '&&' nach 'billing_phone' entfernen, aber es funktionierte. Vielen Dank! Bei @KoenHollander Horror werde ich diese Antwort akzeptieren, nicht weil es Best Practices sind, sondern weil sie die ursprüngliche Frage beantwortet, die ich gestellt habe. – tvirelli

+0

Ich habe auch eine Alternative, die für Sie von Interesse sein könnte, und ja, @arkashca, die helfen kann, wenn das OP es leichter zu lesen, ich werde diese in den Code hinzufügen, um es klarer zu machen –

2

noch besser, stellen nicht so viele Tests im Primär bedingten, nur eine Funktion machen, es zu testen, und dann testen === wahr oder falsch ===

function validate_input($billingRequired=0){ 
    $b_valid = true; 
    if($billingRequired == 1){ 
     if (!isset($_POST['billing_company']) || $_POST['billing_company'] == ""){ 
      $b_valid = false; 
     } 
     elseif (!isset($_POST['billing_address']) || $_POST['billing_address'] == ""){ 
      $b_valid = false; 
     } 
     elseif (!isset($_POST['billing_city']) || $_POST['billing_city'] == ""){ 
      $b_valid = false; 
     } 
     elseif (!isset($_POST['billing_state']) || $_POST['billing_state'] == ""){ 
      $b_valid = false; 
     } 
     elseif (!isset($_POST['billing_zip']) || $_POST['billing_zip'] == ""){ 
      $b_valid = false; 
     } 
     elseif (!isset($_POST['billing_phone']) || $_POST['billing_phone'] == ""){ 
      $b_valid = false; 
     } 
    } 
    if (!isset($_POST['location_name']) || $_POST['location_name'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['location_address']) || $_POST['location_address'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['location_city']) || $_POST['location_city'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['location_state']) || $_POST['location_state'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['location_zip']) || $_POST['location_zip'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['location_phone']) || $_POST['location_phone'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['user_firstname']) || $_POST['user_firstname'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['user_lastname']) || $_POST['user_lastname'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['user_username']) || $_POST['user_username'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['user_email']) || $_POST['user_email'] != "") == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['user_password']) || $_POST['user_password'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['user_mobile']) || $_POST['user_mobile'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['payment_cc_name']) || $_POST['payment_cc_name'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['payment_cc_number']) || $_POST['payment_cc_number'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['payment_cc_expo_month']) || $_POST['payment_cc_expo_month'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['payment_cc_expo_year']) || $_POST['payment_cc_expo_year'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['payment_cc_code']) || $_POST['payment_cc_code'] == ""){ 
     $b_valid = false; 
    } 
    elseif (!isset($_POST['terms']) || $_POST['terms'] == "") 
     $b_valid = false; 
    } 
    return $b_valid; 
} 

Jetzt ist es einfach zu modifizieren/lesen usw. Weil leer ein wenig mehrdeutig sein kann, finde ich mich in der Regel zu vermeiden, trotz seiner stilistischen Eleganz.

Um dies noch sauberer zu machen, würde ich wahrscheinlich es so schreiben:

function validate_input($billingRequired=0){ 
    $b_valid = true; 
    $a_billing = array('billing_company','billing_address'...); 
    $a_main = array('billing_address','location_address'...); 
    if($billingRequired == 1){ 
     $a_main = array_merge($a_billing,$a_main); 
    } 
    foreach ($a_main as $test){ 
     if (!isset($_POST[$test]) || trim($_POST[$test]) == "") 
      $b_valid = false; 
      break; 
     } 
    } 
    return $b_valid; 
} 

Mit dem Vorbehalt, dass leere Werte sind „“ geht davon aus, die im Allgemeinen nicht der Fall für Auswahllisten usw. sein würde

+0

Dies ist bei weitem die beste Option, wie Sie damit umgehen und ich werde diese Idee in meinem tatsächlichen Code verwenden! Die andere Antwort akzeptierte ich bereits aufgrund der einfachen Tatsache, dass sie meine Frage "Ist das möglich" beantwortet hat. Ich werde abstimmen! – tvirelli

2

Was ich tun würde, ist ein Array an der Spitze des Skripts zu erstellen, das eine Liste aller POSTed-Variablen enthält, die erforderlich sind, dann würde ich nur durch sie durchlaufen. Ihr Code wäre viel kleiner. und fast so prägnant ....

//We NEED these fields for the script to work... 
$requiredFields = array(
    "fname", 
    "lname", 
    "phone", 
    "accredited", 
    "etc, etc" 
); 

//IF you require billing, 
if($billingRequired){ 
    // Define the billing fields that we expect to be POSTed... 
    $billingFields = array(
     "billing_compnay", 
     "billing_address", 
     "etc, etc" 
    ); 
    // Add the billing fields to the required fields 
    $requiredFields = array_merge($requiredFields, $billingFileds); 
} 

// Loop through required fields and check to see if they are all POSTed 
foreach($requiredFields as $fieldName){ 
    // IF a required field is not set... 
    if(empty($_POST[$fieldName])){ 
     // Do stuff, call a function, show an error, etc. 
     break; // Or redirect, or exit after a JSON response, whatever. Just be sure to end the loop here for efficiency. 
    } 
} 
+1

Wow, okay, nun, wirklich muss man auf Leute reagieren, da sie immer besser werden. Das ist viel weniger Code, um neue Felder hinzuzufügen und erlaubt viel mehr Freiheit vorwärts zu bewegen! Toller Code! – tvirelli

+0

Danke Kumpel. Ich benutze es in allen meinen Skripten und es funktioniert großartig. Hast du das Formular geändert? KEIN PROBLEM! Bearbeiten Sie einfach das Array, das sich am Anfang des Skripts und BOOM befindet. Problem gelöst. :) –

+1

Ich mag die Idee, diese Arrays zu verschmelzen, definitiv eine Verbesserung meiner Version. Obwohl mein innerer Pedant Ihre Rechtschreibung von 'etc' missbilligt –

Verwandte Themen