PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
<?php
// gets the values from the user entered field
// the first time the page loads
// guess and $tryAgain is not set
if(isset($_POST['random'])){ $random = $_POST['random']; }
if(isset($_POST['guess'])){ $guess = $_POST['guess']; }
if(isset($_POST['tryAgain'])){ $tryAgain = $_POST['tryAgain']; }
if(isset($_POST['count'])){ $count = $_POST['count']; }
if(isset($_POST['random']))
// after postback get value for random from the post request
{ $random = $_POST['random'];}
else
// first time form loads, generate random number , if value has not been set set it
{$random = rand(1,5); }
if(isset($_POST['count']))
// after postback increment count by 1
{ $count = $_POST['count']+1; }
else
// if value has not been set set it
{$count = 0 ; }
if(isset($_POST['count']))
// after postback increment count by 1
{
if($guess==$random)
{ $count = 0;
$tryAgain =0 ; }
}
else
// if value has not been set set it
{$count = 0 ; }
if(isset($_POST['tryAgain']))
// if postrequest get value for random from the post request
// the previous value of count + 1
{ $tryAgain = $_POST['count'] + 1 ;}
else
// if value has not been set set it
{$tryAgain = $count; }
?>
<form method="post">
<input type="text" name="guess" value="">
<input type="text" name="random" value="<?php echo $random ?>">\
<input type="text" name="tryAgain" value="<?php echo $tryAgain ?>">
<input type="text" name="count" value="<?php echo $count ?>" >
<input type="submit">
<form method="post" >
<
<input type="submit">
<?php
if($guess == "")
{
print "Welcome!";
print "$random";
}
else if($guess == $random){
print "$guess is Correct!";
$random == $tryAgain;
// issue postback here reset $count and $tryAgain to 0
$tryAgain = 0;
$count = 0 ;
} else {
if($guess > $random){
print "Too High <br />";
} else if($guess < $random){
print "Too Low <br />";
}
print "Try Again!";
?>
// insert
<?php
$count = 0;
$count++;
print "<h3>$count</h3>";
// this is all part of the else block
}
?>
what im trying to do is that once the user guesses the random number correctly , ie $random = 2 , the user enters the correct random number 2 for $guess and clicks the submit button, $count and $tryAgain will be reset to zero.
Just move the HTML that generates the form to after all you logic. Then the form should be filled in with the correct values. It also makes a cleaner separation between the domain logic code and the presentation output generation code.
PS - Your formatting makes it very difficult to read your code, so I can really tell what it is doing. I would recommend generally following PHP formatting standards: http://www.php-fig.org/psr/psr-2/
<?php
// gets the values from the user entered field
// the first time the page loads
// guess and $tryAgain is not set
if(isset($_POST['random'])){ $random = $_POST['random']; }
if(isset($_POST['guess'])){ $guess = $_POST['guess']; }
if(isset($_POST['tryAgain'])){ $tryAgain = $_POST['tryAgain']; }
if(isset($_POST['count'])){ $count = $_POST['count']; }
if(isset($_POST['random']))
// after postback get value for random from the post request
{ $random = $_POST['random'];}
else
// first time form loads, generate random number , if value has not been set set it
{$random = rand(1,5); }
if(isset($_POST['count']))
// after postback increment count by 1
{ $count = $_POST['count']+1; }
else
// if value has not been set set it
{$count = 0 ; }
if(isset($_POST['count']))
// after postback increment count by 1
{
if($guess==$random)
{ $count = 0;
$tryAgain =0 ; }
}
else
// if value has not been set set it
{$count = 0 ; }
if(isset($_POST['tryAgain']))
// if postrequest get value for random from the post request
// the previous value of count + 1
{ $tryAgain = $_POST['count'] + 1 ;}
else
// if value has not been set set it
{$tryAgain = $count; }
if($guess == "")
{
print "Welcome!";
print "$random";
}
else if($guess == $random){
print "$guess is Correct!";
$random == $tryAgain;
// issue postback here reset $count and $tryAgain to 0
$tryAgain = 0;
$count = 0 ;
} else {
if($guess > $random){
print "Too High <br />";
} else if($guess < $random){
print "Too Low <br />";
}
print "Try Again!";
$count = 0;
$count++;
print "<h3>$count</h3>";
// this is all part of the else block
}
?>
<form method="post">
<input type="text" name="guess" value="">
<input type="text" name="random" value="<?php echo $random ?>">\
<input type="text" name="tryAgain" value="<?php echo $tryAgain ?>">
<input type="text" name="count" value="<?php echo $count ?>" >
<input type="submit">
</form>