2012-05-25 2 views
0

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> 
+2

나는 이전 질문의 답을 바탕으로 다음 질문에 대한 사슬을 만들어야한다고 생각한다. 먼저 이전의 답을 바탕으로 질문의 흐름을 보여주는 종이 위에 나무 구조를 그려야합니다. 일단 명확하면 구현 만 가능합니다. –

+0

그게 전부지만, 어떻게 해야할지 모르겠지만, 이미 종이에 나무 구조가 있고 각 질문을 어디로 가야하는지 그리고 끝 부분에 대답이 무엇인지 알지만 구현 방법을 모르겠다. 이 PHP에서 어떻게 질문의 사슬을 만들까요? – Harry12345

+0

샘플 트리 구조를 3 가지 질문에 맞출 수 있습니까? 다이어그램이 좋을 것입니다. 그것의 intereseting .... –

답변

0

은, 내가 제안 것이다 것은 다음 표

질문 표

QId,Question 

후 질문 옵션 표

Qid, AID, Answer Description 

QuestionsFlow 표

를 만드는 것입니다

특정 질문에 대한 질문에 답변을하면 질문 ID 및 답변 ID를 기반으로 다음 질문을 수정할 수 있습니다. 새로운 질문을 추가하거나 질문의 ​​흐름을 변경하는 것은 간단합니다. 예를 들어 다이어그램마다 QuesitonsFlow 테이블은 다음과 같습니다.

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

이렇게하면 테이블을 확장 할 수 있습니다.

+0

PHP에서 이전에 테이블을 사용하지 않았습니다. PHP로 테이블을 작성하는 방법은 무엇입니까? – Harry12345

+0

그럼 mysql 또는 sqlite 또는 기타와 같은 데이터베이스를 선택해야합니다. 배열을 사용하여 코드를 유지할 수도 있지만 코드 유지 관리의 관점에서 조금 어려울 것입니다. 또는 xml을 알고 있으면 해당 데이터도 xml에 저장할 수 있습니다. –

+0

글쎄, 아직 배우고있는 MySQL의 약간만 알고, 배열로 시도 할 것입니다. – Harry12345

관련 문제