Can someone help, my game wont sequence properly, i have echo line x everywhere so i can watch the sequence. at this time, it jumps from the guess form(2nd sequence) to the firstform(where you set the limit of the game).
<?
//$stage = 1;
//$_SESSION['stage'] = 1;
//if(!$_SESSION['stage']){$_SESSION['stage'] = 1;}
if(!$_SESSION['stage']){$_SESSION['stage'] = $stage;}
/* sequence control
$_SESSION['stage']++;
$_SESSION['stage'] = 1;
echo "stage at" . $_SESSION['stage'];
*/
if($_SESSION['stage'] == 1){
if(!$_SESSION['r']){
$_SESSION['stage'] = 1;
firstForm();
echo "<p>31</p>";
//exit();
}else{
if($_POST['submit']){
echo "<p>line 35</p>";
if($_SESSION['stage'] == 1){
echo "<p>line 37</p>";
checkRange();
}else{echo "<p>line 39</p>";$_SESSION['stage']++;}
}
if(!$_POST['submit']){
echo "<p>42</p>";
firstForm();
}
}
}
if($_SESSION['stage'] >= 2){
echo "48";
if(!$_SESSION['r']){
echo "49 stage at" . $_SESSION['stage'];
//guessForm();
}
if(!$_POST['guess'] && $_POST['submit'] != "guess"){
echo "<p>line 54</p>";
if($_SESSION['stage'] == 2){
echo "<p>line 56</p>";
guessForm();
}else{
echo "<p>line 59</p>";
checkGuess($_POST['guess']);
}
}else{
echo "<p>line 63</p>";
checkGuess($_POST['guess']);
if($_POST['guess']<$_SESSION['r']){
echo "Guess was too low, try again.";
guessForm();
}elseif($_POST['guess']>$_SESSION['r']){
echo "Guess was too high, try again.";
guessForm();
}else{
echo "<h1>Correct Guess! Well done.</h1>";
echo "<h2>It took you " . $_SESSION['counter'] . " guesses.";
restartForm();
$_SESSION['r'] = null;
$_SESSION['counter'] = null;
$_SESSION['stage'] = 1;
}
echo "<p>line 77</p>";
$_SESSION['stage']=2;
echo "<p>current Step: (".$_SESSION['stage'].")</p>";
}
echo "<p>line 83</p>";
$_SESSION['stage']=2;
echo "<p>current Step: (".$_SESSION['stage'].")</p>";
}
echo "<p>current Step: (".$_SESSION['stage'].")</p>";
function checkRange(){
if(!$_POST['jellyBeans']){
echo "<h2>You did not enter a capacity.</h2>";
firstForm();
exit();
}elseif(!is_numeric($_POST['jellyBeans'])){
echo "<h2>Numbers only.</h2>";
firstForm();
exit();
}elseif($_POST['jellyBeans'] <= 0){
echo "<h2>Try a bigger number.</h2>";
firstForm();
exit();
}elseif($_POST['jellyBeans'] > 10000){
echo "<h2>Slow down partner, choose somthing smaller</h2>";
firstForm();
exit();
}else{
echo "<p>107</p>";
echo "<p>random number is: " . $_SESSION['r'] = rand(0,$_POST['jellyBeans']) . "</P>";
$_SESSION['stage']++;
echo "<p>current Step: (".$_SESSION['stage'].")</p>";
}
}
function firstForm(){
?>
<form acton="<?=$_SERVER['php_self']?>" method="post">
<p><label for="jellyBeans">How many jelly beans do you want to be in the game</label><input name="jellyBeans"value="<?=$_POST['jellyBeans']?>" ></p>
<p>
<button value="submit" name="submit" type="submit">Choose</button>
</p>
<p>
<button value="<? $_SESSION['stage'] = 1 ?>" name="submit" type="submit">Restart?</button>
</p>
</form>
<? }
function guessForm(){
?>
<form acton="<?=$_SERVER['php_self']?>" method="post">
<p><label for="guess">Guess what random number was selected</label><input name="guess" type="text" ></p>
<p>
<button value="submit" name="submit" type="submit">Guess</button>
</p>
<p>
<button value="<? $_SESSION['stage'] = 1 ?>" name="submit" type="submit">Restart?</button>
</p>
</form>
<? }
function checkGuess($val){
echo var_dump($val);
if($val == ""){
$error[] = "You did not enter a value.";
}else{
if(!is_numeric($val)){
$error[] = "The value you entered was not a number.";
}
}
if($error){
foreach($error as $v){
echo "<h2>".$v."</h2>";
}
echo "<h3>Try again.</h3>";
guessForm();
exit();
}
}
?>
Just a quick glance..
You'll need to add a session_start();
so your $_SESSION is remembered between page loads.
As a side note: please use full php tags <?php
on the first line instead of <?
as it's not always enabled on all servers.
<?php
session_start(); // ** Added
// The Rest of your code //