Jump to content

Novice - Counting Script


Go to solution Solved by Strider64,

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/291336-novice-counting-script/
Share on other sites

  • Solution

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 by Strider64

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.