Hidden Fields

I am working on hidden fields and cannot figure this out. I’m doing the “I’m thinking of a number” in reverse so the program has to guess what number the user is thinking of in 7 guesses or less.

Guess a number in reverse

Pick a number between 1 and 100 and I will try a guess it!

[php]<?php

$tooHigh = isset($_POST[‘tooHigh’]) ? $_POST[‘tooHigh’] : 100;
$tooLow = isset($_POST[‘tooLow’]) ? $_POST[‘tooLow’] : 0;
$guess = isset($_POST[‘guess’]) ? $_POST[‘guess’] : 0;

if ($_POST['select']=="correct"){
 print "<br>I got it right!<br>"; }
 
 else if ($_POST['select']=="tooHigh"){ 
	$tooHigh = $guess - 1;
	$guess = round(tooLow + tooHigh)/2;
	}
	
	
	else if ($_POST['select']=="tooLow"){
		$tooLow = $guess + 1;
		$guess = round (tooLow + tooHigh)/2;
		}
		
		
$guess=rand($tooLow, $tooHigh);

print <<<HERE

Correct!

Too High

Too Low

My guess is $guess

HERE;
?>
[/php]

There cannot be any spaces before your closing Heredoc statement, line 38 of your post.

You are missing the form action and closing . Form is not valid.

Thanks for looking at my code.

I’m not seeing a space before the Heredoc statement and if you look above and below the PHP (blue area) you will see the form action and closing . The actual form is working fine, the problem is I’m just learning about hidden fields and Sessions and pretty much lost at this point.

The if statements are not working because it should figure out the number in 7 guesses or less. (I did the math and on paper it works) I’m suppose to use hidden fields or sessions to keep tally of the guesses so it can eventually calculate the correct guess.

Also, is that the correct syntax to round numbers in PHP?

Again thanks.

Also, my $guess=rand($tooLow, $tooHigh); isn’t working.

I overlooked the part that was not in the formatted code block. At least in the posted code, there is a space right before the H on line 38. It may not be there in the code you are using. I will look at it again.

This should get you on your way. If you are on windows, just keep hitting F5 to cycle through it. Otherwise just keep reloading the page. The guesses will never repeat the same number or ever go higher or lower then the last highest and lowest values.

[php]<?php
session_start();

$mynum = 30;
echo “My Number is $mynum
”;

$_SESSION[‘high’] = isset($_SESSION[‘high’]) ? $_SESSION[‘high’] : 100;
$_SESSION[‘low’] = isset($_SESSION[‘low’]) ? $_SESSION[‘low’] : 0;
$_SESSION[‘guess’] = isset($_SESSION[‘guess’]) ? $_SESSION[‘guess’] : 0;

echo “

”;
print_r($_SESSION);
echo “/
”;

$_SESSION[‘guess’] = rand($_SESSION[‘low’], $_SESSION[‘high’]);

echo "My Guess is Guess is {$_SESSION[‘guess’]}. ";

//Too Low
if ($_SESSION[‘guess’] < $mynum)
{
$_SESSION[‘low’] = $_SESSION[‘guess’] + 1;
$_SESSION[‘guess’] = rand($_SESSION[‘guess’], $_SESSION[‘high’]);
echo “I guessed to Low.”;
}

//Too High
elseif ($_SESSION[‘guess’] > $mynum)
{
$_SESSION[‘high’] = $_SESSION[‘guess’] - 1; //49
$_SESSION[‘guess’] = rand($_SESSION[‘low’], $_SESSION[‘high’]);
echo “I guessed to High.
”;
}
else
{
echo “I guessed right {$_SESSION[‘guess’]}”;
session_destroy();
}
?>[/php]

I have another question

But first thanks for showing me how to use the

 and getting me on the right track. I understand the sessions a lot better now.

Because the user is thinking of a number in his head and the computer is guessing I know I have to remove the following:

$mynum = 30;

print “My number is $myNum
”;

echo “

”;
print_r($_SESSION);
echo “
”;

My question is: After I remove the above how do I change from $mynum and direct it to the form’ high’ or ‘low’ in order to calculate the numbers?

For example you have:
//Too Low
if ($_SESSION[‘guess’] < $mynum)

Since the program doesn’t know the number (I know you inserted $mynum to troubleshoot) I tried to do this:
if ($_SESSION[‘guess’] == $_SESSION[‘low’])

Why wouldn’t this work?

Please help. :frowning:

Trying to learn this so I can do a bigger project.

Personally, this is how I’d do it. But I’m not sure why sessions would need to be involved unless you’re just trying to gain a grasp of sessions, which is fine, but there’s much better examples of using sessions than this.

[php]<?php
$tooLow = 0;
$tooHigh = 100;
//Check that the submit button was clicked
if (isset($_POST[‘submit’])) {
//Check “my_number” was submitted
if (isset($_POST[‘my_number’])) {$my_number = $_POST[‘my_number’];} else {$my_number = “”;}
//Find out if “my_number” is less than or equal to 0
if ($my_number <= $tooLow) { echo “The number you entered (”.$my_number.") is too low
“; }
//Find out if “my_number” is greater than or equal to 100
elseif ($my_number >= $tooHigh) { echo “The number you entered (”.$my_number.”) is too high
“; }
//If “my_number” isn’t too low or too high, echo it
else { echo “The number you entered (”.$my_number.”) is just right
“; }
$guess = rand($tooLow, $tooHigh);
echo “Random number: " . $guess.”
”;
if ($guess == $my_number) { echo “Wow, you’re a wizard! You guessed the right number!”; } else { echo “Sorry, you guessed the wrong number =(”; }
}
?>

[/php]

@awl19, you did it completly backwards from what the OP asked for.

I'm doing the "I'm thinking of a number"[b] in reverse[/b] so [b]the [u]program has to guess[/u] what number the user is thinking[/b]

You made it so the user has to guess the computers number. The only thing I left out was the part of 7 tries or less. I would love to see another example of doing what he wants without using sessions or a database (including flatfile). Surprise me.

Hmm, okay, I see. Well, I can think of two ways to do it besides sessions, one of them probably more reliable than all the other methods.

  1. JavaScript -
    Create a counter that increases by 1 on each click of submit. After x times, disable the button.
    (This would require that the data be loaded through AJAX because otherwise the page would reload on form submission and reset the count)

  2. Database (Best method) -
    Grab the users ip’s, store them in the db. Upon submitting the form, query the db to increase a “count” field by 1 till “x” times. After x times, clear the form from the page with an if statement and add another button which will send a query to reset the count to 0 again.

JS, PHP sessions, and cookies are all susceptible to user manipulation. The database method is, imo, the most secure and possibly the easiest.

I’m sure there’s a way to do it with something other than the above solutions, but I couldn’t find it. If I do, I’ll post it here.

Okay, I found a solution that will work simply.

[php]<?php
$tooLow = 0;
$tooHigh = 100;
//Check that the submit button was clicked
if (isset($_POST[‘submit’])) {
//Increase $count by increments of 1
$count = $_POST[‘num_count’] + 1;
echo $count . " attempt(s)
“;
//If $count is less than or equal to 6 then proceed with the script
if ($count <= 6) {
//Check “my_number” was submitted
if (isset($_POST[‘my_number’])) {$my_number = $_POST[‘my_number’];} else {$my_number = “”;}
//Find out if “my_number” is less than or equal to 0
if ($my_number <= $tooLow) { echo “The number you entered (”.$my_number.”) is too low
“; }
//Find out if “my_number” is greater than or equal to 100
elseif ($my_number >= $tooHigh) { echo “The number you entered (”.$my_number.”) is too high
“; }
//If “my_number” isn’t too low or too high, echo it
else { echo “The number you entered (”.$my_number.”) is just right
“; }
$guess = rand($tooLow, $tooHigh);
echo “Random number: " . $guess.”
”;
if ($guess == $my_number) { echo “Wow, you’re a wizard! You guessed the right number!”; } else { echo “Sorry, you guessed the wrong number =(”; }
//If $count is greater than or equal to 7 then bypass the script
} elseif ($count >= 7) {
echo “Sorry, you’ve reached your max attempts.”;
}
} else {
$count = 0;
}
?>

[/php]

Add a hidden field in the form for the count. If the form hasn’t been posted yet the value will be 0. If the form is posted, the count goes up in increments of 1: $count = $_POST[‘num_count’] + 1; unless of course the user reloads the page.

You have the # of attempts ok to a point, but it is still completely backwards. The script is supposed to guess the users number, not the user trying to guess the scripts number. That is why the user needs to select a too high or too low option or correct if the computer gets it right because the computer doesn’t know what the user is thinking.

Also, you keep changing the number to be guessed.

If I said I am thinking of a number and you get X guesses, I cant change the answer I am thinking of everytime you guess. The number I am thinking of (the computer) stays the same until you run out of guesses.

Also, the count keeps counting attempts. Party should be over at 7. Cmon, you can do it!

  • And your ideas for JS and a DB dont count. I didnt specify, but I meant how would you do it in PHP since we are on PHP Help, and I said no database.

I formatted your code better so it is easier to read. Also, there is no need to escape the variables. You have: echo "The number you entered (" . $my_number . ") is too low
";

This is ok:

echo "The number you entered b [/b]is too low
";

[php]<?php
$tooLow = 0;
$tooHigh = 100;
//Check that the submit button was clicked
if (isset($_POST[‘submit’]))
{
//Increase $count by increments of 1
$count = $_POST[‘num_count’] + 1;
echo $count . " attempt(s)
";
//If $count is less than or equal to 6 then proceed with the script
if ($count <= 6)
{
//Check “my_number” was submitted
if (isset($_POST[‘my_number’]))
{
$my_number = $_POST[‘my_number’];
}
else
{
$my_number = “”;
}
//Find out if “my_number” is less than or equal to 0
if ($my_number <= $tooLow)
{
echo “The number you entered (” . $my_number . ") is too low
";
}
//Find out if “my_number” is greater than or equal to 100
elseif ($my_number >= $tooHigh)
{
echo “The number you entered (” . $my_number . ") is too high
";
}
//If “my_number” isn’t too low or too high, echo it
else
{
echo “The number you entered (” . $my_number . ") is just right
";
}
$guess = rand($tooLow, $tooHigh);
echo "Random number: " . $guess . “
”;
if ($guess == $my_number)
{
echo “Wow, you’re a wizard! You guessed the right number!”;
}
else
{
echo “Sorry, you guessed the wrong number =(”;
}
//If $count is greater than or equal to 7 then bypass the script
}
elseif ($count >= 7)
{
echo “Sorry, you’ve reached your max attempts.”;
}
}
else
{
$count = 0;
}
?>

[/php]

This:
<input type=“text” name=“my_number” value=“Enter your number” />

Should be:

<input type=“text” name=“my_number” placeholder=“Enter your number” />

And if we are doing HTML5 we can get rid of the closing /

<input type=“text” name=“my_number” value=“Enter your number” />


You only have one field so this is duplicating the first isset post. The following should be the only check for isset post

if (isset($_POST[‘my_number’]))


Since $_POST[‘my_number’] is set on submit The following is extra un-needed code

      else
         {
         $my_number = "";
         }

What you should be doing is not checking for isset, but checking for not empty

if (!empty($_POST[‘my_number’])) {
//DO everything here
}

The program fails/not working if it cannot guess the users number in 7 guesses or less, not the user so this elseif would not be included in the program.

( elseif ($count >= 7)
{
echo “Sorry, you’ve reached your max attempts.”;
}

The program can do it in 7 guess or less by using this format, I’m just having trouble putting it too work using sessions:

if ($_POST[‘select’] == tooHigh) {
$tooHigh = $guess - 1;
$guess = round(tooLow + tooHigh)/2;
}

else if ($_POST[‘select’]==“tooLow”)
{
$tooLow = $guess + 1;
$guess = round (tooLow + tooHigh)/2;
}

Basically the guessed number is halfway between the high and low limits,

if guess is lower then the new guess is halfway between the last guess and the lowest limit

else if higher then the new guess is halfway between the higher limit and the last guess

otherwise must be the desired number.

Sponsor our Newsletter | Privacy Policy | Terms of Service