View Single Post
  #5 (permalink)  
Old 02-26-10, 06:50 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Fully documented for you.

PHP Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
My attempt at the Guessing game problem-Crystal Benes
</title>
</head>
<body>
<?php
    
//Get our form results
    
$guess  $_POST['guess'];
    
$number intval($_POST['number']);
    
$num_guesses intval($_POST['num_guess']);
    
//increment the num_guesses that we pass in the hidden form field;
    
$num_guesses++;
    if (
$number 0) { //IF number is more than 0;
        
if ($guess $number) {
            
//IF the Guess minus the Number = less than or equal to 10;
            
if(($guess $number) <= 10) {
                echo 
"You're HOT, HOT, HOT!!!";
            }
            else { 
//else $guess is just more than the number.
                
echo "Sorry, your guess is too high, try again";
            }
        }
        elseif(
$guess $number) {
            
//IF the Number minus Guess = less than or equal to 10;
            
if(($number $guess) <= 10) {
                echo 
"You're HOT!";
            }
            else { 
//else number is more than the guess
                
echo "Sorry, your guess is too low, try again";    
            }
        }
        else { 
//If number is neither higher or lower than guess, then it must be the number.
                //print out the secret number.
            
echo "You guessed the secret number ($number) <br />";
                
//print out how many times it took to get it.
            
echo "It only took $num_guesses guesses for you to get it. <br/>";
                
//tell them to try again.
            
echo "See if you can guess it again <br />";
                
//empty the variable of the old number
            
unset ($number);
                
//pick a new number
            
$numberrand(1,15);
                
//reset the number of guesses.
            
$num_guesses 0;
        }
    }
    else {  
//IF number is == 0, lets pick a number, and set the num_guesses to 0;
        
$number rand(1,15);
        
$num_guesses 0;
       }

    
//NOW we print out the form.
?>

<form method="post" name="guess">
    <input type="hidden" name="num_guess" value="<?php echo $num_guesses//hidden field that holds our number of guesses?>">
    <input type="hidden" name="number" value="<?php echo $number//hidden field that holds our number?>">
    Pick a number 1 through 1000: <input name="guess" type="text">
    <input name="submit" type="submit" value="Submit Guess ( <?php echo $num_guesses//print out the number of guesses in the submit button?> )">
    </form>    
    </body>
    </html>
Reply With Quote