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
After you fix what DeveloperChris says, you still need to put guesscount into as session or form field, and increment on each attempt.
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.
the return statement is leaving the loop and the script
get rid of both return statements
DC
Further to your comments on the duplicate question at... Allowing 3 attempts at game - php
It would appear that this question is a 'homework' question as such and in fact with all questions no-one will give you the complete answer, nor in my opinion should they. We all expect that the person asking the question will take it upon themselves to investigate and understand the answers given.
Now in the case of your question, you appear to be missing a vital piece of information about how HTTP works (http is the protocol that drives all web pages and many other parts of the internet).
http is what's considered a stateless protocol, that is when you click on a link in a web page and go to another web page (or even the same web page), the new web page considers you a totally new visitor. It in effect has forgotten you.
Because this introduced issues for things like shopping carts (and PHP games) cookies were invented. this allowed the browser to carry around a small bit of information about you, in this way the web server or application remembered you. This has been extended into what respondents here are calling sessions.
A session is (usually) a cookie that stores an identifier. that identifier tells, in this case PHP, that you have been there before and where to find the information about you. PHP can load this information and make it available to you the programmer.
This happens EVERY TIME a page is loaded.
Now PHP does not know what to store in this 'session' it is up to you the programmer to decide what information needs to be stored. you need to tell PHP to save this information for the next time the page is loaded.
In your case its up to you to decide what needs to be remembered. Consider the reloading of the page to be a new 'iteration' of the loop. this should lead you to some obvious conclusions about what needs to be passed from one iteration to the next.
There you go. I haven't written the answer for you but hopefully have provided enough for you to pass your class in flying colours.
DC
if($guesscount < 3 && $guessedYear > $realyear){
echo "Wrong, year too high";
}
if($guesscount < 3 && $guessedYear > $realyear){
echo "Wrong, year too high";
}
duplicate code there. also i think you are looking for if{...}else if{..}else if{...}else{...}
and your question has nothing to do with javascript.
maybe core logic more like this?
$movie = isset($_POST['movie']) ? $_POST['movie'] : false;
$guessedYear = isset($_POST['year']) ? (int) $_POST['year'] : false;
if ($movie && $guessedYear) {
$realyear = $ages[$movie];
}
@$_SESSION[$movie]['guesscount']++;
if ($realyear && $_SESSION[$movie]['guesscount'] < 3) {
if ($guessedYear == $realyear) {
echo "Correct! " . "during year " . $realyear;
}
if ($guessedYear < $realyear) {
echo "Wrong, year too low";
}
if ($guessedYear > $realyear) {
echo "Wrong, year too high";
}
} elseif ($_SESSION[$movie]['guesscount'] >= 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']--;
}