oneoption Posted August 31, 2012 Share Posted August 31, 2012 i've got the task to create a Guessing game, where you guess the number between 1-100. Also there has to be a number of maxium guesses..10. but just cant figure it out . It's in danish. so thats why the ? is everywhere:p <html> <head> <title>G?tteleg</title> </head> <body> <?php session_start(); $_SESSION['antal'] = 10 ?> <form action="guess2.php" method="POST"> G?t Nummer!: <input type="number" name="g?t"<br/> <input type="submit" name="send" value="G?t"> </form> </html> </body> <?php session_start(); $rand = rand(1, 100); $g?t = $_POST['g?t']; $send = $_POST['send']; echo $_SESSION['antal']; $_SESSION['antal']--; if ($g?t<1 || $g?t>100) { echo "Dit g?t skal v?re mellem 1 og 100"; } else if($g?t!=$send) { echo "Forkert... Det rigtige nummer var.." . $rand; } else { echo "Godt G?ttet"; } ?> Quote Link to comment Share on other sites More sharing options...
MMDE Posted August 31, 2012 Share Posted August 31, 2012 Why are there two different codes? Use one. You need to check if the $_SESSION['antal'] is set or not before assigning a new value to it. if(!isset($_SESSION['antal'])){ $_SESSION['antal'] = 10; } Only $_SESSION['antal']--; when they have guessed. You also need to make sure if they have guessed. You can't just go ahead and do this: $g?t = $_POST['g?t']; $send = $_POST['send']; Because $_POST['send'] might not be set, and then it will be looking for something that doesn't exist. It's just to use isset() again. Remember that only if they guess you can decrease the amount of guesses left, and you must make sure SESSION then too is already set. $g?t = $_POST['g?t']; Use numbers and English alphabetical characters only in variable names, and they must start with the latter. It also seems like you are setting a new random number each time. Isn't that confusing for the person guessing? There is nothing stopping a person from clearing the session and get 10 new attempts. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 31, 2012 Share Posted August 31, 2012 Did you see this thread? http://forums.phpfreaks.com/index.php?topic=349529 Quote Link to comment Share on other sites More sharing options...
oneoption Posted September 3, 2012 Author Share Posted September 3, 2012 It is working now. The last issue is now.. I need to echo something, if the abs of the guessed number is >50 . Here is some of the code if ($guess<1 || $guess>100) { echo ".."; } else { if($guesst!=$_SESSION['target']) { echo "wrong. the correct number was..." . $_SESSION['target']; } else if // This is where i am stuck, i come so far to know i have to use abs something. { echo "Way to High"; } Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 3, 2012 Share Posted September 3, 2012 Have you tried checking out the PHP manual? Quote Link to comment Share on other sites More sharing options...
Spring Posted September 3, 2012 Share Posted September 3, 2012 http://php.net/manual/en/control-structures.elseif.php Quote Link to comment 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.