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

时间:2012-05-25 12:50:46

标签: php arrays loops switch-statement decision-tree

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

1 个答案:

答案 0 :(得分:0)

我将建议创建以下表格

,而不是使用数组

问题表

QId,Question

问题选项表

Qid, AID, Answer Description

QuestionsFlow Table

QId, AID, QID

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

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

这样你可以扩展你的表格。