Trying to get it to loop through 3 times and after the 3rd time (if not guessed right) show the right answer.
Currently - its going through the guesses, but isnt showing
I think you're problem is this line $rand_keys = array_rand($ages, 1);
. Each time the user submits their answer a new $rand_keys
is selected and fed into the dropdown regardless of what the submitted answer was.
So you'll want to check if there exists an answer (otherwise it's the first time the page is loaded). If there is an answer and it was correct then show a congrats message and generate a new movie id.
if($_POST['submit']) {
$movie = $_POST['movie'];
$guessedYear = $_POST['year'];
if ($guessedYear == $ages[$movie]) {
// well done you got it right, next movie
$rand_keys = array_rand($ages, 1);
}
else if ($guessedYear == $ages[$movie] && $_POST['tries'] >= 3) {
// took over 3 tries and didn't get it right, next movie
$rand_keys = array_rand($ages, 1);
}
else {
// find $movie index from $ages and use that
}
// you have one less try
$tries = $_POST['tries'] - 1;
}
else {
$rand_keys = array_rand($ages, 1);
$tries = 3;
}
Then in the form send the $tries
variable along with the other ones, or as the other people here have said put it in the session variables. With that I think you should be able to remove the while loop completely.