根据按下的按钮显示不同数组的PHP脚本
我正在编写一个PHP脚本,询问4个是/否问题,并根据答案显示您在最后想什么动物,但它还没有完成。我已经完成了前3组问题,共4组,但我不知道如何在不做6或7个switch/if语句的情况下显示最后一组问题和答案,有没有比这更好的方法,也许使用某种循环?下面是到目前为止我编写的代码,您可以在查看测验,尽管它尚未完成。任何帮助都将不胜感激根据按下的按钮显示不同数组的PHP脚本,php,arrays,loops,switch-statement,decision-tree,Php,Arrays,Loops,Switch Statement,Decision Tree,我正在编写一个PHP脚本,询问4个是/否问题,并根据答案显示您在最后想什么动物,但它还没有完成。我已经完成了前3组问题,共4组,但我不知道如何在不做6或7个switch/if语句的情况下显示最后一组问题和答案,有没有比这更好的方法,也许使用某种循环?下面是到目前为止我编写的代码,您可以在查看测验,尽管它尚未完成。任何帮助都将不胜感激 <?php session_set_cookie_params(2592000); //Sets cookie to last for 30 days ses
<?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,Question
Qid, AID, Answer Description
问题流量表
QId, AID, QID
当为特定问题回答了一个问题时,您可以根据问题ID和答案ID生成下一个问题。添加新问题/更改问题流也很简单。例如,根据您的图表,QuesitonFlow表如下
QID | AnsID | NextQID
1 | 1 | 2
1 | 2 | 3
通过这种方式,您可以扩展表格。我认为您应该根据上一个问题的答案创建到下一个问题的链接。首先,你应该在一张纸上画一些树形结构,显示基于前面答案的问题流程。这就是我正在尝试做的,但不知道怎么做,我已经在纸上有了一个树形结构,我知道每个问题应该去哪里,最后的答案是什么,但不知道如何在PHP中实现,我该如何创建问题链?你能把你的示例树结构放在3个问题上吗?一张图表就好了。有趣的是,它至少需要4个问题,因为至少需要16个可能的答案。我的图表看起来与此类似,但每个块的末尾都有另外两个分支。根据该图表,如果Q1为是,Q2为否,Q3是否会出现?我以前在PHP中没有使用过表,如何在PHP中创建表?您需要选择mysql或sqlite之类的数据库。使用数组也是可以做到的,但是从代码可维护性的角度来看,这有点困难,或者如果你知道xml,你也可以用xml存储数据。我只知道一点mysql,因为我还在学习,我会尝试使用数组。