Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

PHP Array or Function showing wrong questions

$
0
0

Hey Guys,

Please bear with me as I'm still rather new with PHP and have been chucked in the deep end a bit.

Anyways, Im working on this mini version of a 20 questions game, however there's a slight hiccup somewhere along the line and I can't seem to suss it out.

Say I say first question = yes it displays the 2nd question which is fine but then if I say the answer no to the second question is no it displays the 4th question but it should display question 5 which should be "Can you keep them as pets?"

Demo here - http://s341350.neongrit.net/session_test.php

Please be aware that it stores cookies!!
I've been staring at it for hours now and its probably something really silly that I've missed knowing my luck lol but if anyone can point me in the right direction I would really appreciate it.

Thank you. :)

<?php
session_set_cookie_params(2592000); //This sets the cookie for 30 days.
session_start(); //initiate the session
if(isset($_SESSION['track'])){
 	$_SESSION['track'] = $_SESSION['track'];
	}
 	//
 	else{
 	$_SESSION['track']=0;
	}
?>
<html>
<head>
<title>Advanced Web 2013 || Creature Guessing Game</title>
<style>
/* start button styles */
#start{
background-color:#9F3;
color:#fff;	
width:100px;
height:30px;
font-size:20px;
border-width:2px;
border-color:black;
border-radius:10px;
}

#start:hover{ /*style changes slightly upon mouse hover over buttons*/
background-color:#9F6;
border-width:1px;
border-radius:10px;
}

/* styles for other buttons */
.buttons{
background-color:#666633;
color:#fff;	
width:65px;
height:25px;
font-size:12px;
font-weight:bold;
border-width:2px;
border-color:#black;
border-radius: 10px;
}

.buttons:hover{
background-color:#999966;
border-width:1px;
border-radius:10px;
}

</style>
</head>
<body>
<div class="position">
<h1>Creature Guessing Game</h1>
<p>Hello<br> 
<br>
Can I guess what creature your thinking of? <br> 
Click the button below to play!</p>

<form method="post" action="session_test.php">
<input type="submit" name="start" value="Play!" id="start" />
</form>

<div>
 
<?php

//Get array for Question 1 
$first_question = "Does the creature live on land?"; 

//2-dimensional Array used here to ask certain questions based on answers from the user. In an array we have to point to arrays index. In this 2-dimensional array each question has two indexes (array number and question number) We access these later on.
 $question_array = array( 
    array( 'Does it have wings?', 'Is it a type of mammal?'), 
   	array('Can it fly?', 'Can you keep them as pets?', 'Can they be found in the Artic?', 'Do they have fins?'), 
	array('Are they nocturnal?','Do they live in Africa?','Are they reptillian?','Do they live in 			 	Australia?'), 
	array('Do they have Paws?','Can you swim with them?','Can they eat people?','Do they have tentacles?')
	);

//Set variables for the functions
 $yes; // holds creature types based on yes answers from user 
 $no; // holds creature types based on no answers from user
 $q_num = 0;; // holds the number of the question inside the array, default = 0
 
 //first function asks questions 2-7 // question number is stored in $q_num variable, used by the function later. form name value uses $q_num var to pinpoint which question to move on to based on users answer .. 
 function get_question($q_num) {
     echo "<form method ='GET' action='session_test.php'>
    <input type='submit' name='answer$q_num' value='Yes' class='buttons' />
    <input type='submit' name='answer$q_num' value='No' class='buttons' />
	<button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>";
	echo $q_num;
	//$q_num ++; // adds 1 to the $q_num var after each question 
}

  //Second function asks questions 8-15 used in the switch statement // the variables $yes and $no store the final answer based on the users yes/no selection 
 function get_final($yes,$no) {
      echo "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='$yes' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='$no' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
 }
 
 
//If start button has not been pressed, display nothing
if (isset($_POST['start'])){
 	
	if(isset($_SESSION['track'])) {
	echo $_SESSION["track"];
	echo $_SESSION["question"];
	echo $_SESSION["form"];
}

if($_SESSION['track']== 0) {
echo "<div class='questions'> $first_question </div>";
     echo "<div class='questions'><form method ='GET' action='session_test.php'>
     <input type='submit' name='yes' value='Yes' class='buttons' />
     <input type='submit' name='no' value='No' class='buttons' />
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
     </form></div>";
	 $_SESSION['track'] = 1;
	 $_SESSION['question'] = $first_question;
}
}

// If answer to question #1 is Yes ... Ask question #2
if ($_SESSION['track'] == 1 && isset($_GET['yes'])) 
{
echo "<div class='questions'>{$question_array[0][0]}</div>"; //question 2 is located in array 0 at index 0 
get_question(1);// see function set up above for explanation on using the different functions and what they do 
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?yes=Yes' readonly></input>";
	$_SESSION['track'] = 2;
	$_SESSION['question'] = $question_array[0][0];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <input type='submit' name='answer1' value='Yes' class='buttons' />
     <input type='submit' name='answer1' value='No' class='buttons' />
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
     </form>";
}

// If answer to question #1 is No ... Ask question #3
if ($_SESSION['track'] == 1 && isset($_GET['no']))
{
echo "<div class='questions'>{$question_array[0][1]}</div>"; // question 3 is located in array 0 at index 1
get_question(2);
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?no=No' readonly></input>";
	$_SESSION['track'] = 3;
	$_SESSION['question'] = $question_array[0][1];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
    <input type='submit' name='answer2' value='Yes' class='buttons' />
    <input type='submit' name='answer2' value='No' class='buttons' />
	<button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>";
}

// If answer to question #2 is Yes ... Ask question #4
switch($_SESSION['track'] == 2 && isset($_GET['answer1']))
{
case 'Yes': echo "<div class='questions'>{$question_array[1][0]}</div>"; // question 4 is located in array 1 at index 0
get_question(3);
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer1=Yes' readonly></input>";
	$_SESSION['track'] = 4;
	$_SESSION['question'] = $question_array[1][0];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
    <input type='submit' name='answer3' value='Yes' class='buttons' />
    <input type='submit' name='answer3' value='No' class='buttons' />
	<button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>";
break;
// If answer to question #2 is No ... Ask question #5
case 'No': echo "<div class='questions'>{$question_array[1][1]}</div>"; // question 5 is located in array 1 at index 1
get_question(4);
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer1=No' readonly></input>";
	$_SESSION['track'] = 5;
	$_SESSION['question'] = $question_array[1][1];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
    <input type='submit' name='answer4' value='Yes' class='buttons' />
    <input type='submit' name='answer4' value='No' class='buttons' />
	<button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>";
}

// If answer to question #3 is Yes .. Ask question #6
switch($_SESSION['track'] == 3 && isset($_GET['answer2']))
{
case 'Yes': echo "<div class='questions'>{$question_array[1][2]}<div>"; // question 6 is located in array 1 at position 2
get_question(5);
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer2=Yes' readonly></input>";
	$_SESSION['track'] = 6;
	$_SESSION['question'] = $question_array[1][2];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
    <input type='submit' name='answer5' value='Yes' class='buttons' />
    <input type='submit' name='answer5' value='No' class='buttons' />
	<button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>";
break;
// If answer to #3 is no ... Ask question #7
case 'No': echo "<div class='questions'>{$question_array[1][3]}</div>"; // question 7 is located in array 1 at position 3
get_question(6);
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer2=No' readonly></input>";
	$_SESSION['track'] = 7;
	$_SESSION['question'] = $question_array[1][3];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
    <input type='submit' name='answer6' value='Yes' class='buttons' />
    <input type='submit' name='answer6' value='No' class='buttons' />
	<button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>";
}

//**and so on and so forth...**
// If answer to question #4 is Yes ... Ask question #8
switch($_SESSION['track'] == 4 && isset($_GET['answer3']))
{
case 'Yes': echo "<div class='questions'>{$question_array[2][0]}</div>";
get_final('Owl','Pidgeon'); // see function set up above for explanation on using the different functions and what they do
// if the answer to question 8 is Yes its a Eagle, if No then is a Parrot... These answers are atored in the $yes and $no vars inside the  get_final() function. 
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer3=Yes' readonly></input>";
	$_SESSION['track'] = 8;
	$_SESSION['question'] = $question_array[2][0];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Owl' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='Pidgeon' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
	
break;
//Otherwise ask question #9
case 'No': echo "<div class='questions'>{$question_array[2][1]}</div>";
get_final('Ostrich','Penguin'); 
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer3=No' readonly></input>";
	$_SESSION['track'] = 9;
	$_SESSION['question'] = $question_array[2][1];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Ostrich' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='Penguin' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
}

// If answer to question #5 is Yes ... Ask question #10
switch($_SESSION['track'] == 5 && isset($_GET['answer4']))
{
case 'Yes': echo "<div class='questions'>{$question_array[2][2]}</div>";
get_final('Snake','Dog'); 
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer4=Yes' readonly></input>";
	$_SESSION['track'] = 10;
	$_SESSION['question'] = $question_array[2][2];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Snake' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='Dog' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
break;
//Otherwise ask question #11
case 'No': echo "<div class='questions'>{$question_array[2][3]}<div>";
get_final('Kangaroo','Lion'); // see above for expanation on the get_final() funstion 
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer4=No' readonly></input>";
	$_SESSION['track'] == 11;
	$_SESSION['question'] = $question_array[2][3];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Kangaroo' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='Lion' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
}

// If answer to question #6 is Yes .. Ask question #12
switch($_SESSION['track'] == 6 && isset($_GET['answer5']))
{
case 'Yes': echo "<div class='questions'>{$question_array[3][0]}</div>";
get_final('Polar Bear','Humpback Whale');
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer5=Yes' readonly></input>";
	$_SESSION['track'] = 12;
	$_SESSION['question'] = $question_array[3][0];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Polar Bear' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='Humpback Whale' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
break;
//Otherwise ask question #13
case 'No': echo "<div class='questions'>{$question_array[3][1]}</div>";
get_final('Dolphin','Sea Otter');
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer5=No' readonly></input>";
	$_SESSION['track'] == 13;
	$_SESSION['question'] = $question_array[3][1];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Dolphin' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='Sea Otter' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
}

// If answer to question #7 is Yes ... Ask question #14 
switch($_SESSION['track'] == 7 && isset($_GET['answer6']))
{ 
case 'Yes': echo "<div class='questions'>{$question_array[3][2]}</div>";

get_final('Shark','type of harmless fish maybe?');
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer6=Yes' readonly></input>";
	$_SESSION['track'] = 14;
	$_SESSION['question'] = $question_array[3][2];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Shark' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='type of harmless fish maybe?' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
break;
//Otherwise ask question #15
case 'No': echo "<div class='questions'>{$question_array[3][3]}</div>";
get_final('Jellyfish','Crocodile');
	echo "Send to a friend: <input type='text' value='http://s341350.neongrit.net/web.php?answer6=no' readonly></input>";
	$_SESSION['track'] = 15;
	$_SESSION['question'] = $question_array[3][3];
	$_SESSION['form'] = "<form method ='GET' action='session_test.php'>
     <button type='submit' name='answer' value='Jellyfish' class='buttons'> Yes </button>
     <button type='submit' name='answer' value='Crocodile' class='buttons'> No </button>
	 <button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
    </form>"; 
}

//****//
if (isset($_GET['answer'])) //If the final answer has been reached ...
{
echo "Its a ";  print_r($_GET['answer']); // display final answer // *** error here echo alone doesnt seem to work so have used print_r function to show everything inside the array *** // 
echo "<br>";
echo "<form method ='GET' action='session_test.php'> 
<button type='submit' name='restart' value='restart' class='buttons'> Restart </button>
</form>";
echo "<br><br>"; //echo a line break for spacing after answer **makes it look nicer** 
}

// unset session if restart has been pressed 
if (isset($_GET['restart'])) {
	session_destroy(); // destroy session
}


?>
</body>
</html>

Viewing all articles
Browse latest Browse all 13200

Trending Articles