benjahnee Posted March 4, 2013 Share Posted March 4, 2013 (edited) hi guys i have written the code of a simnple number guessing game which asks the user for a guess between 1 and 10 and then outputs whether or not the guess was correct and what the number was if it was correct. Would somebody please tell me how to adjust this code so that if the guess is wrong, the page will display " the number is lower" or "the number is higher" and prompt the user to guess again. here is what i have so far. thanks <?php $random = rand(1, 10); $guess = $_POST ['guess']; $submit = $_POST ['submit']; if (isset($submit)) { if ($guess < 1 || $guess > 10 ){ echo "Your guess must be a number from 1 to 10!"; }else { if ($guess!=$random){ echo "Incorrect guess, the answer is ".$random; } else { echo " That is correct!"; } } }else { header("location: index.php"); exit (); } ?> Edited March 12, 2013 by ignace Please, next time use code tags Quote Link to comment https://forums.phpfreaks.com/topic/275227-attempt-at-number-guessing-game-please-offer-revisions/ Share on other sites More sharing options...
yomanny Posted March 12, 2013 Share Posted March 12, 2013 <?php // Check if the form has been submitted if(isset($_POST['guess'])) { $guess = $_POST['guess']; $secret = $_POST['secret']; ?> <html> <head></head> <body> <?php if($guess == $secret) { // The user guessed right ?> <h1>Correct!</h1> <a href="">Play again</a> <?php } else { // The user did guess wrong, now let's see if it was lower or higher if($guess < $secret) { // The guess was too low ?> <h2>Wrong! The secret number is Higher, try again!</h2> <?php } else { // The guess was too high ?> <h2>Wrong! The secret number is Lower, try again!</h2> <?php } ?> <form action="" method="post"> <input type="hidden" name="secret" value="<?php echo $secret; ?>" /> Guess: <input name="guess" type="text" /> <input type="submit" value="Guess!" /> </form> </body> </html> <?php } } else { // Generate random number $secret = rand(1,10); ?> <html> <head></head> <body> <form action="" method="post"> <input type="hidden" name="secret" value="<?php echo $secret; ?>" /> Guess: <input name="guess" type="text" /> <input type="submit" value="Guess!" /> </form> </body> </html> <?php } - W Quote Link to comment https://forums.phpfreaks.com/topic/275227-attempt-at-number-guessing-game-please-offer-revisions/#findComment-1418169 Share on other sites More sharing options...
benjahnee Posted March 13, 2013 Author Share Posted March 13, 2013 thanks alot yomanny Quote Link to comment https://forums.phpfreaks.com/topic/275227-attempt-at-number-guessing-game-please-offer-revisions/#findComment-1418359 Share on other sites More sharing options...
Love2c0de Posted March 13, 2013 Share Posted March 13, 2013 (edited) Hi benjahnee, good evening to you. I thought I'd re-write your script because I think for this case you should process the PHP code first, then output what you need, instead of mixing processing and raw HTML together. <?php $secret = rand(1,10); // Check if the form has been submitted if(isset($_POST['guess'])) { $guess = intval($_POST['guess']); $secret = intval($_POST['secret']); $output = ""; if($guess == $secret) { $output .= "<h1>Correct!</h1>"; $output .= "<a href='#'>Play again</a>"; } elseif($guess < $secret) { $output .= "<h1>Your answer was too low. Please try again!"; } else { $output .= "<h2>Your answer was too high. Please try again!</h2>"; } } ?> <html> <head> <title>Page Name</title> </head> <body> <form action="" method="post"> <input type="hidden" name="secret" value="<?php echo $secret; ?>" /> Guess: <input name="guess" type="text" /> <input type="submit" value="Guess!" /> </form> <?php if(isset($output)) { print($output); } ?> </body> </html> It looks a lot cleaner. Hope this helps. Kind regards, L2c. Edited March 13, 2013 by Love2c0de Quote Link to comment https://forums.phpfreaks.com/topic/275227-attempt-at-number-guessing-game-please-offer-revisions/#findComment-1418468 Share on other sites More sharing options...
benjahnee Posted March 18, 2013 Author Share Posted March 18, 2013 Thanks alot love2code that is much better for me. Woud your be able to expand a little further for me though please. I would like it if when the guess was correct, the guess box is omitted and just the message and option to play again is displayed. i cant figure out how to do it. thanks Quote Link to comment https://forums.phpfreaks.com/topic/275227-attempt-at-number-guessing-game-please-offer-revisions/#findComment-1419278 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.