0

私は 4 つのはい/いいえの質問をし、答えに応じて最後にどの動物を考えているかを表示する PHP スクリプトを作成していますが、まだ完成していません。4 つのうち最初の 3 つの質問セットを実行しましたが、最後の質問セットを表示してから、約 6 または 7 つの switch/if ステートメントを実行せずに回答を表示する方法がわかりません。より良い方法はありますか?これよりも、おそらくある種のループを使用していますか?以下は私がこれまでに書いたコードです。クイズはhttp://s504518.brunelweb.net/Creatures.phpで見ることができますが、まだ完成していません。どんな助けでも大歓迎です。

<?php
session_set_cookie_params(2592000); //Sets cookie to last for 30 days
session_start();
?>

<html>
<head>
<title>Creature Guessing Game</title>
</head>
<body>
<h1>Creature Guessing Game</h1>
<p> Welcome to the creature guessing game! </p>
<p>Click the button below to start or restart the game </p>

<form method="post" action="Creatures.php">
<input type="submit" name="start" value="Start Game" />
</form>
 <?php
 $firstquestion = "Does the creature live on the land?"; //Question 1

 $questions = array( //Question 2   //Question 3
    array('Does it have wings?', 'Is it amphibious?'), //First answer is for yes, second for no

        //Question 4    //Question 5     //Question 6       //Question 7
    array('Can it fly?', 'Is it an insect?', 'Is it white?', 'Does it have tentacles?'), //First 2 are yes/no for Q2 and second 2 are yes/no for Q3

            //Question 8    //Question 9       //Question 10    //Question 11
    array('Is it brown?','Does it live in Australia?','Is it green?','Does it have 2 arms?'), //First 2 are yes/no for Q4, second 2 are yes/no for Q5

        //Question 12           //Question 13           //Question 14           //Question 15
    array('Does it live in the Artic?','Do they eat their legs in France?','Has is got eight of them?','Can you keep them as pets?')
    //First 2 are yes/no for Q6 and second 2 are yes/no for Q7
            );

 $answers1 = array('Eagle','Parrot','Ostrich','Turkey','Grasshopper','Ant','Gorilla','Tiger'); //Answers is Q1 is yes

 $answer2 = array('Penguin','Goose','Frog','Salamander','Octopus','Jellyfish','Goldfish','Eel'); //Answers is Q1 is no

 $number;
  function showquestion($number) {
      echo "<form method ='post' action='Creatures.php'>
     <input type='submit' name='answer$number' value='Yes' />
     <input type='submit' name='answer$number' value='No' />
     </form>";
 }
//If form not submitted, display form.
if (!isset($_POST['start'])){
?>
<?php
} //If form is submitted, process input
else{ 
echo $firstquestion;
     echo "<form method ='post' action='Creatures.php'>
     <input type='submit' name='yes1' value='Yes' />
     <input type='submit' name='no1' value='No' />
     </form>";
}
if ($_POST['yes1']) //If answer to Q1 is yes then display this
{
echo $questions[0][0];
showquestion(1);
}

if ($_POST['no1']) 
{
echo $questions[0][1]; //If answer to Q1 is no then display this
showquestion(2);
}

switch($_POST['answer1']) //Q2 - Show the next question depending on the button pressed
{
case 'Yes': echo $questions[1][0]; showquestion(3);
break;
case 'No': echo $questions[1][1]; showquestion(4);
}

switch($_POST['answer2']) //Q3 - Show the next question depending on the button pressed
{
case 'Yes': echo $questions[1][2]; showquestion(5);
break;
case 'No': echo $questions[1][3]; showquestion(6);
}
?>
</body>
</html>
4

1 に答える 1

0

配列を使用する代わりに、次のテーブルを作成することをお勧めします

質問表

QId,Question

質問オプション表

Qid, AID, Answer Description

質問フロー表

QId, AID, QID

特定の質問に対して質問が回答されると、質問 ID と回答 ID に基づいて次の質問に移ることができます。新しい質問を追加したり、質問の流れを変更したりするのも簡単です。たとえば、図のように、QuestionsFlow テーブルは次のようになります

QID | AnsID | NextQID
1   | 1     |  2
1   | 2     | 3

このように、テーブルを拡張できます。

于 2012-05-25T13:24:28.240 に答える