ivanmcruz32 Posted November 3, 2009 Share Posted November 3, 2009 hi everyone, i'm new here. I'm taking a PHP class and it is kicking my butt. So, maybe a little help, since I can't get any from my teacher. :'( I have to use loops to create a game called "I'm thinking of a number". The program is supposed to take a number from a user as input and a hidden value. It then tells whether the user is too high, low or correct and tell how many chances it took. So, how can I fix my code? <html> <head> <title> Number Guessing v1.1.1.90 </title> <style type = "text/css"> body { background: green; color: white; } </style> </head> <body> <center> <h1>Number Guessing</h1><br><br> <h2>Please guess a number between 1 and 10</h2><br><br> <?php if (empty ($guessNum)){ $guessNum = 1; } run(); for($guessNum = 1;$guess != $hiddenGuess; $guessNum++){ if ($guess < $hiddenGuess){ echo "You guessed too low"; }else if ($guess > $hiddenGuess){ echo "You guessed too high"; } } if ($guess == $guessNum){ echo "You are correct!<br>You get a cookie!<br>"; echo "It took you $guessNum tries."; }else{ echo "Umm, Houston, there is a problem<br>"; } function run(){ global $guess, $hiddenGuess; print <<<HERE <input type = "text" name = "guess" value = $guess> <input type = "hidden" name = "hiddenGuess" value = $hiddenGuess> <input type = "submit" value = Enter> HERE; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/180150-solved-im-thinkin-of-a-number-looping/ Share on other sites More sharing options...
Adam Posted November 3, 2009 Share Posted November 3, 2009 What's your problem first? Quote Link to comment https://forums.phpfreaks.com/topic/180150-solved-im-thinkin-of-a-number-looping/#findComment-950361 Share on other sites More sharing options...
JonnoTheDev Posted November 3, 2009 Share Posted November 3, 2009 The code is terrible. Global variables are bad. Your HTML markup is invalid. You dont have a form. You do not even need a loop for this task nor do you require functions. Here is you script simplified: <html> <head> <title>Number Guessing v1.1.1.90</title> <style type = "text/css"> body { background: green; color: white; } </style> </head> <body> <h1>Number Guessing</h1><br><br> <h2>Please guess a number between 1 and 10</h2><br><br> <?php if($_POST['submit'] == 'Enter') { $_POST['attempts']++; if(is_numeric($_POST['guess'])) { if($_POST['guess'] == $_POST['hiddenGuess']) { print "<p>You guessed correctly</p>"; } if($_POST['guess'] > $_POST['hiddenGuess']) { print "<p>You guessed too high</p>"; } if($_POST['guess'] < $_POST['hiddenGuess']) { print "<p>You guessed too low</p>"; } } else { print "<p>Please enter a number</p>"; } } ?> <p>Attempts: <?php print isset($_POST['attempts']) ? $_POST['attempts'] : 0; ?></p> <form method="post" action=""> <input type="text" name="guess" /> <input type="hidden" name="attempts" value="<?php print $_POST['attempts']; ?>" /> <input type="hidden" name="hiddenGuess" value="<?php print is_numeric($_POST['hiddenGuess']) ? $_POST['hiddenGuess'] : rand(1,10); ?>" /> <input type="submit" name="submit" value="Enter" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/180150-solved-im-thinkin-of-a-number-looping/#findComment-950368 Share on other sites More sharing options...
ivanmcruz32 Posted November 3, 2009 Author Share Posted November 3, 2009 thank you! that worked perfectly! i've been working on this thing for weeks and couldn't figure out how to make it work correctly. Quote Link to comment https://forums.phpfreaks.com/topic/180150-solved-im-thinkin-of-a-number-looping/#findComment-950377 Share on other sites More sharing options...
JonnoTheDev Posted November 3, 2009 Share Posted November 3, 2009 thank you! that worked perfectly! i've been working on this thing for weeks and couldn't figure out how to make it work correctly. You should certainly learn how the code works to progress. Quote Link to comment https://forums.phpfreaks.com/topic/180150-solved-im-thinkin-of-a-number-looping/#findComment-950484 Share on other sites More sharing options...
ivanmcruz32 Posted November 3, 2009 Author Share Posted November 3, 2009 i'm trying to get this information. I think the book I have PHP5 mysql is NOT WORKING OUT!! this is what my teacher gave us to work with. i'm lost, i'm confused and still reading Quote Link to comment https://forums.phpfreaks.com/topic/180150-solved-im-thinkin-of-a-number-looping/#findComment-950497 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.