Allowing 3 attempts at game - php
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/4892506/loop-iteration-in-php-game">Loop Iteration in Php Game</a>  </p>
</blockquote>



<p>Trying to allow the user to guess at "1" movie, three times, if they dont get it, it tells them the right answer (which is variable: $rand_keys). It also deducts guesses and displays the amount of guesses left (possible error with $guesses variable?).</p>

<p>Whats going wrong here? Please show me a possible solution.</p>

<pre><code>&lt;style type="text/css"&gt;
input {border:1px solid #ADD8E6; font-size:1.2em;}
input.spec {background-color:#ddd;}
&lt;/style&gt;
&lt;?php
    echo "&lt;fieldset&gt;&lt;h1&gt;&lt;legend&gt;Testing your Academy Award Trivia&lt;/h1&gt;";
    $ages['Casablanca'] = "1943";
    $ages['Around The World in 80 Days'] = "1956";
    $ages['Patton'] = "1970";
    $ages['Annie Hall'] = "1977";
    $ages['Chariots of Fire'] = "1981";
    $ages['Dances With Wolves'] = "1990";
    $ages['Crash'] = "2005";
    $ages['The Departed'] = "2006";

    $rand_keys = array_rand($ages, 1);

    $guesses = 3;

?&gt;
&lt;form method='post' name="inputyear" onsubmit="return validate(this);"&gt;
Give the year below won academy award&lt;br&gt;&lt;br&gt;
&lt;Strong&gt;Movie:&lt;/strong&gt; &lt;input type='text' name='movie' class="spec" value='&lt;?= $rand_keys ?&gt;'     readonly='readonly' /&gt;&lt;br&gt;&lt;br&gt;
&lt;Strong&gt;Year it Won the Oscar:&lt;/Strong&gt; &lt;input type='text' name='year' size="30" /&gt;&lt;br/&gt;&lt;br&gt;
&lt;strong&gt;You have: &lt;/strong&gt; &lt;?php $guesses; ?&gt; guesses left&lt;br&gt;&lt;br&gt;
&lt;input type='submit' name='submit' value="Get Result" onClick="makeGuess()" /&gt;
&lt;/form&gt;

&lt;?php
$movie = isset($_POST['movie']) ? $_POST['movie'] : false;
$guessedYear = isset($_POST['year']) ? (int) $_POST['year'] : false;
if ($movie &amp;&amp; $guessedYear) {
    $realyear = $ages[$movie];
}
@$_SESSION[$movie]['$guesses']++;
if ($realyear &amp;&amp; $_SESSION[$movie]['$guesses'] &lt; 3) {

    if ($guessedYear == $realyear) {
        echo "Correct! " . "during year " . $realyear;
    }
    if ($guessedYear &lt; $realyear) {
        echo "Wrong, year too low";
        $guesses--;
    }
    if ($guessedYear &gt; $realyear) {
        echo "Wrong, year too high";
        $guesses--;
    }

} elseif ($_SESSION[$movie]['$guesses'] &gt;= 3) {
    echo "Sorry, too many tries. the answer was " . $realyear;
} else {
    echo "Sorry, You managed not to pick a year. Please try again";
    $_SESSION[$movie]['guesscount']--;
}
?&gt;
</code></pre>
