Search the Community
Showing results for tags 'counting'.
-
My prof hasn't gotten back to me and he doesn't seem like even he knows how to do it so I'm posting here for some help. We've barely done any script writing yet and I can't seem to figure out how to go about doing it. He wants us to modify the existing script so that it generates a random number from 1 - 1000 and then counts the number of guesses and outputs how many guesses at the end. It won't open in the browser so I'm not sure what's wrong. Any help would be much appreciated! This is what I have so far: <?php if (!isset($_POST["guess"])) { $message = "Welcome to the guessing machine!"; $_POST["numtobeguessed"] = rand(1,1000); $_POST["counter"] = 0; } else if ($_POST["guess"] > $_POST["numtobeguessed"]) { $message = $_POST["guess"]." is too big! Try a smaller number."; $_POST["counter"]++; } else if ($_POST["guess"] < $_POST["numtobeguessed"]) { $message = $_POST["guess"]." is too small! Try a larger number."; $_POST["counter"]++; } else { // must be equivalent $message = "Well done! It took you '$_POST["counter"]' tries!"; } ?> <html> <head> <title>A PHP number guessing script</title> </head> <body> <h1><?php echo $message; ?></h1> <form action="" method="POST"> <p><strong>Type your guess here:</strong> <input type="text" name="guess"></p> <input type="hidden" name="numtobeguessed" value="<?php echo $_POST["numtobeguessed"]; ?>" ></p> <input type="hidden" name="counter" value="<?php echo $_POST["counter"]; ?>" <p><input type="submit" value="submit your guess"/></p> </form> </body> </html>