PHP 脚本根据按下的按钮显示不同的数组


PHP script to show different array depending on button pressed

我正在编写一个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>

我建议不要使用数组,而是创建下表

问题表

QId,Question

奎西顿选项表

Qid, AID, Answer Description

问题流表

QId, AID, QID

回答特定问题时,您可以根据问题 ID 和答案 ID 回答下一个问题。添加新问题/更改问题流程也很简单。例如,根据您的图表,QuesitonsFlow表将是这样的

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

这样,您可以扩展表。