0

我正在编写一个 PHP 脚本,它会询问 4 个是/否问题,并根据答案显示你最后想到的动物,但它还没有完成。我已经完成了前 3 组问题,共 4 组,但我不知道如何显示最后一组问题,然后在不做大约 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 推断下一个问题。添加新问题/更改问题流程也很简单。例如,根据您的图表,QuesitonsFlow 表将是这样的

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

这样你就可以扩展你的桌子。

于 2012-05-25T13:24:28.240 回答