2016-05-03 4 views
0

Ich habe 3 SELECT Elemente wie "Provinz", "Bezirk" und "Markt" in meinem HTML-Code. Ich brauche einen Beispielalgorithmus, um die Kombination zu erkennen, auf der Formularelemente eine Auswahl getroffen wurde oder nicht. Ich denke über 6 Kombinationen von Wenn-Bedingungen nach.Wie ermittle ich Multi-Select-Kombinationen in PHP

Wie kann ich die Kombination der gefüllten SELECT Elemente mit einer minimalen Anzahl von Bedingungen bestimmen?

Mein Code bisher zeigen soll, was ich zu erreichen versuchen:

<select name="a"> 
    <option value="acity">a city</option> 
</select> 
<select name="b"> 
    <option value="bdistrict">b district</option> 
</select> 
<select name="c"> 
    <option value="cmarket">c market</option> 
</select> 
<?php 
if($_POST["a"] != null and $_POST["b"] != null and $_POST["c"] != null) 
    echo "aaaa"; 
elseif($_POST["a"] != null and $_POST["b"] != null) 
    echo "bbb"; 
/*elseif ...*/ 
?> 

Danke für Ihre Antwort.

+0

Wo ist Ihre eigentliche Code? – Random

+0

Bitte seien Sie genauer. Wenn möglich, teilen Sie uns –

Antwort

0

Sie können die Antworten in einem Array speichern. Der folgende Code trifft zwei Entscheidungen: Wie viele aufeinander folgende Optionen wurden ausgewählt, und jede Kombination einschließlich keine. Anstatt Werte zu speichern, könnten Sie sogar Funktionen speichern.

$combination = [ 'abc' => function() {return 'foo bar';} /* , ... */ ]; 
$combination['abc']();` 

 

<!DOCTYPE html> 
<html> 
    <body> 
    <form method="post"> 
     <select name="a"> 
     <option></option> 
     <option value="acity">a city</option> 
     </select> 
     <select name="b"> 
     <option></option> 
     <option value="bdistrict">b district</option> 
     </select> 
     <select name="c"> 
     <option></option> 
     <option value="cmarket">c market</option> 
     </select> 
     <button type="submit">submit</button> 
    </form> 
<?php 
$post_keys = [ 
    'a' => 'aaa', 
    'b' => 'bbb', 
    'c' => 'ccc' 
]; 

$value = '[no valid selection]'; 

foreach($post_keys as $key => $val) 
    if(isset($_POST[$key]) && '' !== $_POST[$key]) 
    $value = $val; 
    else 
    break; 
?> 
    <div>Form has been filled until: <?php echo $value;?>.</div> 
<? 
$combination = [ 
    '' => 'nothing', 
    'a' => 'only a', 
    'ab' => 'c missing', 
    'abc' => 'all', 
    'ac' => 'b missing', 
    'b' => 'only b', 
    'bc' => 'a missing', 
    'c' => 'only c' 
]; 

$key_seq = ''; 
foreach($post_keys as $key => $val) 
    if(isset($_POST[$key]) && '' !== $_POST[$key]) 
    $key_seq .= $key; 

$value = $combination[$key_seq]; 
?> 
    <div>Combination of a,b,c: <?php echo $value;?>.</div> 
    </body> 
</html> 
+0

Danke für die Antwort. Das ist ein schönes Denken .. – servetcolakoglu

Verwandte Themen