4 개의 예/아니오 질문을하고 답변에 따라 최종적으로 생각하는 동물을 표시하는 PHP 스크립트를 작성 중이지만 아직 완료되지 않았습니다. 나는 처음 3 세트의 질문을했다. 그러나 6 번 또는 7 번 스위치/if 문을 쓰지 않고 마지막 질문 세트를 표시 한 다음 어떻게 대답 할 수 있는지 모르겠다. 할 수있는 더 좋은 방법이있다. 그것보다, 아마 어떤 종류의 루프를 사용하고 있을까요? 아래 코드는 제가 지금까지 작성한 코드입니다. 아직 끝나지 않았지만 http://s504518.brunelweb.net/Creatures.php에있는 퀴즈를 볼 수 있습니다. 어떤 도움을 주시면 감사하겠습니다. 대신 배열을 사용버튼을 눌렀을 때 다른 배열을 표시하는 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>
나는 이전 질문의 답을 바탕으로 다음 질문에 대한 사슬을 만들어야한다고 생각한다. 먼저 이전의 답을 바탕으로 질문의 흐름을 보여주는 종이 위에 나무 구조를 그려야합니다. 일단 명확하면 구현 만 가능합니다. –
그게 전부지만, 어떻게 해야할지 모르겠지만, 이미 종이에 나무 구조가 있고 각 질문을 어디로 가야하는지 그리고 끝 부분에 대답이 무엇인지 알지만 구현 방법을 모르겠다. 이 PHP에서 어떻게 질문의 사슬을 만들까요? – Harry12345
샘플 트리 구조를 3 가지 질문에 맞출 수 있습니까? 다이어그램이 좋을 것입니다. 그것의 intereseting .... –