2016-07-31 6 views
0

Ich habe eine if() Code wird so lang sein, und einige von ihnen teilen einige gemeinsame HTML-Code. Ich denke, der bessere Weg ist, dass jede HTML-Frage Basis auf der if-Bedingung aufrufen. Schau dir meinen Code an, z. wenn $fruit='apple', möchte ich die HTML-Code Frage 1,3,5 Echo. Wie sollte ich die globale Funktion für die Fragen festlegen?Wie sollte ich globale Funktion Basis auf dieser Situation PHP erstellen?

<?php 
    if($fruit=='apple'){ 
    /*call question 1,3,5*/ 
    }else if($fruit=='banana'){ 
    /*call question 1,2,4*/ 
    }/*.........a lot of else if.....*/ 

    /*question 
    1. <div>Is it good?</div> 
     .............some multiple choice here 
     <input type="text" value="submit"/> 
    2. <div>where is it from?</div> 
     .............some multiple choice here 
     <input type="text" value="submit"/> 
    3. <div>...........</div> 
     .............some multiple choice here 
     <input type="text" value="submit"/> 
    4. <div>..........</div> 
     .............some multiple choice here 
     <input type="text" value="submit"/> 
    */ 

?> 
+2

organisieren sie in ein Array '$ arr = [ 'Apfel' => [1,3,5], 'Banane' => [1, 2,4]] 'ecc – 0x13a

+1

Als eine Randnotiz bevorzugen einige Leute r [switch] (http://php.net/manual/en/control-structures.switch.php) wenn es viele '0sself's – FirstOne

+0

Schalter gibt, ist besser? schneller? – conan

Antwort

1

Sie könnten Ihren Code wie folgt schreiben:

$fruit = "apple"; 

if (in_array($fruit, ["apple", "banana"])) { 
    echo '<div>Is it good?</div> 
     .............some multiple choice here 
     <input type="text" value="submit"/>'; 
} 
if (in_array($fruit, ["banana", "pineapple"])) { 
    echo '<div>where is it from?</div> 
     .............some multiple choice here 
     <input type="text" value="submit"/>'; 
} 
// etc... 

Es gibt natürlich auch andere Möglichkeiten, es zu tun. Sie könnten eine Datenstruktur, die Sie alle Informationen, die Sie den HTML-Code benötigen, einschließlich bauen, und dann mit einer nur eine kleinen Schleife des ausgewählten HTML Ausgabe:

$fruit = "apple"; 

$questions = array(
    "1" => array(
     "question" => "Is it good?", 
     "answers" => ["Fantastic", "So-so", "Never again"] 
    ), 
    "2" => array(
     "question" => "Where is it from?", 
     "answers" => ["South America", "Africa", "Australia"] 
    ), 
    "3" => array(
     "question" => "Which color does it have", 
     "answers" => ["Yellow", "Red", "Orange"] 
    ), //...etc 
); 
$questionsForFruits = array(
    "apple" => [1, 3], 
    "banana" => [1, 2], 
    "pineapple" => [2] 
); 

$fruit = "apple"; 

foreach($questionsForFruits[$fruit] as $questionNo) { 
    $q = $questions[$questionNo]; 
    echo "<div>{$q['question']}</div> 
      <select>"; 
    foreach($q['answers'] as $index =>$answer) { 
     echo "<option value = '$index'>$answer</option>"; 
    } 
    echo "</select><br> 
      <input type='text' value='submit'/>"; 
} 
+0

Vielen Dank! – conan

1

Organisieren Sie die Optionen in ein Array

$questions = [ 
     'q1' => '<div>Is it good?</div> 
       .............some multiple choice here 
       <input type="text" value="submit"/>', 
     'q2' => '<div>where is it from?</div> 
       .............some multiple choice here 
       <input type="text" value="submit"/>', 
     ]; 

$fruit_questions = [ 
     'apple' => [1,3,5], 
     'banana' => [1,2,4], 
     'guava' => [17,21,4], 
     ]; 
Dann

, wenn Sie Fragen erhalten müssen, einfach tun:

$question_keys = $fruit_questions[$fruit]; 

$html_of_questions = ''; // This will hold the questions to echo 
foreach($question_keys as $question_key){ 
    $html_of_questions .= $questions['q'.$question_key] 
} 

echo $html_of_questions; 
+1

Vielen Dank! Ihr Code ist sehr einfach und gut. Sehr schlau! – conan

Verwandte Themen