0

Hi I am clearing my basics of php and i am kind of stuck in implementing this guess game. My problem is that I want $num_tries to work as a counter and it increases everytime by 1 whenever the user answers incorrectly.

<?php
$target=58;
$message="";
$num_tries=0;
if(isset($_POST["num_tries"])) {
    print "set";
    ++$num_tries;
} else {
    print "not set";
    $num_tries=0;
}

if (!isset($_POST["guess"])) {
    $message="Welcome to the game of Guess!!!!!!";
} elseif($_POST["guess"]> $target) {
    $message= "Try a smaller Number";
} elseif($_POST["guess"] < $target) {
    $message= "Try a larger Number";
} else {
    $message= "congrats....You got it.";
}
?>
<HTML>
<HEAD><TITLE>Guess Game</TITLE>
</HEAD>
<BODY>
<H1><?php print $message ?></H1>
Guess Number:<?php print $num_tries ?>

<FORM action="<?php print $_SERVER['PHP_SELF'];?>" method="POST">
    <INPUT type="text" name="guess">
    <INPUT type="hidden" name="num_tries" value="<?php $num_tries?>">
</FORM>
</BODY>
</HTML>
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Sudhanshu
  • 120
  • 11
  • 1
    Well, the very first thing you do is `$num_tries=0`... – Álvaro González Mar 14 '16 at 17:09
  • 1
    *"I want $num_tries to work as a counter and it increases everytime by 1"* -- on every script invocation you initialize it with `0` then `++$num_tries` increases it if needed. It works perfectly. – axiac Mar 14 '16 at 17:11
  • 1
    What you probably want is to pass its value between executions (that's why you added it in a hidden `input`). But on the next execution you ignore the value completely. You should do `if(isset($_POST["num_tries"])) { print "set"; $num_tries = (int)$_POST['num_tries']; ++$num_tries; } else { ... }` – axiac Mar 14 '16 at 17:12
  • Thanks Axiac ..... I tried all the mentioned solutions but only yours worked. Thanks a lot. – Sudhanshu Mar 16 '16 at 05:19

3 Answers3

2

You can solve it in your first lines:

$target=58;
$message="";
$num_tries = (!empty($_POST['num_tries']) ? $_POST['num_tries'] : 0);

That means if the form is not submitted before, $num_tries will be zero, otherwise, the value will be the last submitted value.

And then you need to print in the hidden element

<INPUT type="hidden" name="num_tries" value="<?php echo $num_tries?>">
---------------------------------------------------^^^^
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
1
$message="";
$num_tries=0;

change the above declaration to $num_tries=isset($_POST["num_tries"]) ? $_POST["num_tries"] : 0; which means if the post var value is set, then assign post var value, else assign 0.

and next change what you need to make is printing the value under hidden element. you can use print or print_r or echo.

<INPUT type="hidden" name="num_tries" value="<?php echo $num_tries; ?>">
ameenulla0007
  • 2,663
  • 1
  • 12
  • 15
0

You are using <?php $num_tries ?> which literally does nothing.

You need to use <?php echo $num_tries ?> (print also works, but for your use case, echo is more efficient) in order to actually place the value into the form.

Additionally, $num_tries = 0; will not bother reading the existing value. You can use $num_tries = $_POST['num_tries'] | 0; although it will generate a PHP notice when the field isn't set, if you care about this, use isset with a ternary operator or if/else.

Olipro
  • 3,489
  • 19
  • 25