11

I have started writing a PHP script for a game about creatures, there are 4 yes/no questions and what I am trying to do is write a function that will display 2 buttons that say yes and no and give then different names each time I run the function, for example yes1 and no1, then the next time the function is run the names of the buttons will be yes2 and no2.

I have attempted to do this already but it is not working correctly, below is the code I have done so far, any help would be much appreciated.

<?php
session_set_cookie_params(2592000);
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
 $questions = array('Does the creature live on land?', 'Does it have wings?', 'Does it live near water?', 'Can it fly?');
 function repeat()
 {
 $number = 0;
 $number++;
 echo "<form method ='post' action='Creatures.php'>
<input type='submit' name='yes'.$number value='Yes' />
<input type='submit' name='no'.$number value='No' />
</form>";

 }
//If form not submitted, display form.
if (!isset($_POST['start'])){
?>
<?php
} //If form is submitted, process input
else{ 
switch($_POST)
{
case yes1:
echo $questions[0];
repeat();
break;
case no1:
echo $questions[1];
repeat();
break;
case yes2:
echo $questions[2];
repeat();
break;
case no2:
$questions[3];
repeat();
}
}
?>
</body>
</html>
Harry12345
  • 1,144
  • 6
  • 20
  • 47
  • Each time you call `repeat()` you're setting `$number` to `0` and then incrementing it. What if you set `$number` after you set `$questions` but before `repeat()` is defined? – robert May 24 '12 at 19:11
  • I have just tried that but then for some reason $number is always set to 0 as I tried echoing it and no matter how many times I called the function $number remained 0 – Harry12345 May 24 '12 at 19:17

3 Answers3

12

To do this you need to maintain state between requests.

 function repeat() {
      $number = $_SESSION['number'];
      $number++;
      echo "<form method ='post' action='Creatures.php'>
     <input type='submit' name='answer' value='Yes' />
     <input type='submit' name='answer' value='No' />
     </form>";
     $_SESSION['number'] = $number;
 }

switch

   if($_POST['answer']=='Yes') {
      switch($_SESSION['number']) {
            case 1:
            break;
            case 2:
            break;
      }
   } else {
      switch($_SESSION['number']) {
            case 1:
            break;
            case 2:
            break;
      }
   }
ShaaD
  • 616
  • 3
  • 10
  • Thank you! The function is working like I wanted it to now, the switch statement is still not working but I might just try something instead of a switch now. – Harry12345 May 24 '12 at 20:10
  • This still doesn't work, I want the switch statement to do different things depending on what button is pressed (either yes or no), maybe there is some way to make the switch condition yes1 and make the first case so if its true (i.e. $_POST = 'yes1') and the second case if it's not true. Don't know if you understand?? If you do is this possible? – Harry12345 May 24 '12 at 20:46
7

I am not in agreement with the accepted answer: Here is an easier way to go about it without session:

function repeat() {
      static $number = 0;
      $number++;
      echo "<form method ='post' action='Creatures.php'>
     <input type='submit' name='answer' value='Yes' />
     <input type='submit' name='answer' value='No' />
     </form>";
 }
1

You have to store the 'number' in session or cookie as it will loose the value every time page submitted (reloaded).

Ruben Nagoga
  • 2,178
  • 1
  • 19
  • 30