Nelalen Posted September 28, 2014 Share Posted September 28, 2014 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: <?phpif (!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> Quote Link to comment https://forums.phpfreaks.com/topic/291336-novice-counting-script/ Share on other sites More sharing options...
Solution Strider64 Posted September 28, 2014 Solution Share Posted September 28, 2014 (edited) You were close. btw I don't know what you mean it won't open up in the browser for it should had even with an error(s). /* You had this */ $message = "Well done! It took you '$_POST["counter"]' tries!"; /* It should had been something like this */ $message = 'Well done! It took you ' . $_POST["counter"] . ' tries!'; Edited September 28, 2014 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/291336-novice-counting-script/#findComment-1492322 Share on other sites More sharing options...
Nelalen Posted September 28, 2014 Author Share Posted September 28, 2014 So this should be opening in browser and appearing correctly?? I suppose that's why the prof was confused as am I.. We've been having issues with the permissions on the server changing. I'll try taking another look at those and try a different browser to open.. Thank you! For anyone else please feel free to point out any other mistakes or syntax errors you find! Quote Link to comment https://forums.phpfreaks.com/topic/291336-novice-counting-script/#findComment-1492323 Share on other sites More sharing options...
ginerjm Posted September 28, 2014 Share Posted September 28, 2014 Add the lines from my signature to the beginning of your php code and see what errors you are getting. Quote Link to comment https://forums.phpfreaks.com/topic/291336-novice-counting-script/#findComment-1492324 Share on other sites More sharing options...
Nelalen Posted September 28, 2014 Author Share Posted September 28, 2014 I got it working!! Thanks so much!! I was worried since I had to submit it by tonight! I will definitely be back here for any more questions in the future!! Quote Link to comment https://forums.phpfreaks.com/topic/291336-novice-counting-script/#findComment-1492325 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.