Current location: Hot Scripts Forums » Programming Languages » PHP » Help, I've been trying for 2 days


Help, I've been trying for 2 days

Reply
  #1 (permalink)  
Old 02-24-10, 05:46 PM
crisblue13 crisblue13 is offline
New Member
 
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
X_X Help, I've been trying for 2 days

Hello all,
im very very very new to the PHP language. I dont get it !
I am in a class called Relational Database Coding
The book we're using is PHP 6 and MYSQL 5 by Larry Ullman,
I dont feel like I have gotten very much from the first 3 chapters before we were given an assignment..
I am to build a guessing game.. which I've got the code to, but I need to add to it if the user is 10 away from the secret number tell the user that they are "Hot" etc.
I also have to have a running thingy .. like at the end when they guess the number tell how many times it took them to guess.. I have tried so many things and all I did was mess up my code more and more.. so i took it all out.. -- i currently only have it set to rand 1,5 so i dont have to guess too long to figure out if it is working... UGH. .Im so confused
Here is the code I have now.
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
    $guess  
$_POST['guess'];
    
$number $_POST['number'];
    
$num_guesses $_POST['num_guesses'];
    

        
        
    if (
$number 0) {
        if (
$guess $number) {
            echo 
"Sorry, your guess is too high, try again";
            echo 
"<form method=\"post\" name=\"guess\">
                    <input type=\"hidden\" name=\"number\"value= \"
$number\">
                    Pick a number 1 through 1000: <input name=\"guess\" type=\"text\">
                    <input name=\"submit\" type=\"submit\" value=\"Submit Guess\">
                </form>"
;
            
        }
    
        
        elseif(
$guess $number) {
        
            echo 
"Sorry, your guess is too low, try again";
            echo 
"<form method=\"post\" name=\"guess\">
                    <input type=\"hidden\" name=\"number\"value= \"
$number\">
                    Pick a number 1 through 1000: <input name=\"guess\" type=\"text\">
                    <input name=\"submit\" type=\"submit\" value=\"Submit Guess\">
                </form>"
;
                    
        }
        
        else {
            echo 
"You guessed the secret number ($number) <br />";
            echo 
"See if you can guess it again <br />";
            unset (
$number);
            
$numberrand(1,5);
            
            echo 
"<form method=\"post\" name=\"guess\">
                    <input type=\"hidden\" name=\"number\"value= \"
$number\">
                    Pick a number 1 through 1000: <input name=\"guess\" type=\"text\">
                    <input name=\"submit\" type=\"submit\" value=\"Submit Guess\">
                </form>"
;
                
        }
    }
    
    else {
        
        
$number rand(1,5);
        
?>

<form method="post" name="guess">
    <input type="hidden" name="number" value="<?php echo $number?>">
    Pick a number 1 through 1000: <input name="guess" type="text">
    <input name="submit" type="submit" value="Submit Guess">
    </form>
    <?php
    
}
    
?>
    </body>
    </html>

Last edited by job0107; 02-24-10 at 11:09 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-24-10, 11:17 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Try it like this:
This one uses a fixed number as the answer. See $answer
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>
<script type="text/javascript">
function highlightInput()
{
 document.getElementById("guess").focus();
 document.getElementById("guess").select();
 }
</script>
</head>
<body onload="highlightInput();">
<?php
$answer 
200// Number to guess. //
$msg "";
$finish 0;
$numGuesses = empty($_POST["numGuesses"]) ? $_POST["numGuesses"];
$number = empty($_POST["guess"]) ? "" $_POST["guess"];
if(!empty(
$_POST["submit"]))
{
 if(!empty(
$number)){$numGuesses++;}
 if((
$number $answer 11 && $number $answer) || ($number $answer 11 && $number $answer))
 {
  
$msg "Your hot!";
  }
 else if(
$number == $answer){$msg "You win!<br />The answer = ".$number;$number "";$finish 1;}
 else
 {
  if(!empty(
$number))
  {
   if(
$number $answer){$msg "Sorry, your guess is too low, try again";}
   if(
$number $answer){$msg "Sorry, your guess is too high, try again";}
   }
  }
 }
echo 
"<h1><b>".$msg."</b></h1><br />Number of guesses = ".$numGuesses;
if(
$finish == 1){$numGuesses 0;}
?>
<form action="#" method="post">
<input type="hidden" name="numGuesses" value="<?php echo $numGuesses?>">
Pick a number 1 through 1000: <input id="guess" name="guess" type="text" value="<?php echo $number?>">
<input name="submit" type="submit" value="Submit Guess">
</form>
</body>
</html>
Or this way using a random generated number from 1 to 1000.
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>
<script type="text/javascript">
function highlightInput()
{
 document.getElementById("guess").focus();
 document.getElementById("guess").select();
 }
</script>
</head>
<body onload="highlightInput();">
<?php
$msg 
"";
$finish 0;
$numGuesses = empty($_POST["numGuesses"]) ? $_POST["numGuesses"];
$number = empty($_POST["guess"]) ? "" $_POST["guess"];
$answer = empty($_POST["answer"]) ? rand(1,1000) : $_POST["answer"];
if(!empty(
$_POST["submit"]))
{
 if(!empty(
$number)){$numGuesses++;}
 if((
$number $answer 11 && $number $answer) || ($number $answer 11 && $number $answer))
 {
  
$msg "Your hot!";
  }
 else if(
$number == $answer){$msg "You win!<br />The answer = ".$number;$number "";$finish 1;}
 else
 {
  if(!empty(
$number))
  {
   if(
$number $answer){$msg "Sorry, your guess is too low, try again";}
   if(
$number $answer){$msg "Sorry, your guess is too high, try again";}
   }
  }
 }
echo 
"<h1><b>".$msg."</b></h1><br />Number of guesses = ".$numGuesses;
if(
$finish == 1){$numGuesses 0;$answer "";}
?>
<form action="#" method="post">
<input type="hidden" name="numGuesses" value="<?php echo $numGuesses?>">
<input type="hidden" name="answer" value="<?php echo $answer?>">
Pick a number 1 through 1000: <input id="guess" name="guess" type="text" value="<?php echo $number?>">
<input name="submit" type="submit" value="Submit Guess">
</form>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 02-24-10 at 11:49 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-24-10, 11:58 PM
crisblue13 crisblue13 is offline
New Member
 
Join Date: Feb 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you .. I will try that..
I was wondering tho, is there any way to do it with the starting code?
The code that ws given was a starter of a working program given by the teacher.
We were just asked to add to it. Im literally a major beginner at this.. I dont understand it at all,
when I ask the teacher for help he uses these "big" words like string and array and other words that .. yes
I've read about them but I dont remember what they are.. looking at actual code, i have no clue whats what.
I have a little bit of experience with VB.NET but PHP is for some reason just too difficult for me
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-25-10, 12:56 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Well Visual Basic and PHP use the same logic structure.
Only the syntax is different.

Let's take a look at this code.
I will try to explain it line by line and then you can try to adapt it to your original code.

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>

<!-- This script is simply putting the focus on the input element and selecting any text that may be in it. -->
<script type="text/javascript">
function highlightInput()
{
 document.getElementById("guess").focus();
 document.getElementById("guess").select();
 }
</script>

</head>

<!-- In the body element I am using the onload event listener to run the Javascript function highlightInput(). -->
<body onload="highlightInput();">

<?php
// I am using $msg to store the messages that will be output in the HTML code. //
$msg "";

// I am using $finish in the code to indicate that I have finished the quiz. It is initally set to 0 and I will set it to 1 when I enter the correct answer. //
$finish 0;

// The value for $numGuesses is loaded using boolean logic. It is a simplified form of an if/else statement. If $_POST["numGuesses"] has no value then $numGuesses is set to 0 else it is set to the value in $_POST["numGuesses"]. //
$numGuesses = empty($_POST["numGuesses"]) ? $_POST["numGuesses"];

// $number and $answer are also initalized using boolean logic. //
$number = empty($_POST["guess"]) ? "" $_POST["guess"];
$answer = empty($_POST["answer"]) ? rand(1,1000) : $_POST["answer"];

// Here I am checking to see if $_POST["submit"] has a value. If the form has been submitted it will and then the rest of the PHP code will be evaluated. If not, it will skip over the rest of the PHP code and display the form again. //
if(!empty($_POST["submit"]))
{
 
// In this if statement I am checking to see if a number was entered into the input element in the form. If a value was entered then $numGuesses is incremented by one. //
 
if(!empty($number)){$numGuesses++;}
 
 
// In this if statement I am checking to see if the number entered is within + or - 10 of the answer. If it is then it loads the string "Your hot!" into the $msg variable. //
 
if(($number $answer 11 && $number $answer) || ($number $answer 11 && $number $answer))
 {
  
$msg "Your hot!";
  }
  
 
// If the entered number is the answer then this else if statement loads the message "You win!" and the answer into the $msg variable. Then $number is set to a null string and $finish is set to 1 indicating that the quiz is over. And $finifh will be used later in the code. //
 
else if($number == $answer)
 {
  
$msg "You win!<br />The answer = ".$number;
  
$number "";
  
$finish 1;
  }

 
// In this last else statement I am checking to see if there was a value submitted that was loaded into $number previously. If there was, then I am checking to see if it is greater or smaller that the answer and then loading the appropriate message into $msg. //
 
else
 {
  if(!empty(
$number))
  {
   if(
$number $answer){$msg "Sorry, your guess is too low, try again";}
   if(
$number $answer){$msg "Sorry, your guess is too high, try again";}
   }
  }
 }
 
// And finally I echo $msg and the number of guesses. //
echo "<h1><b>".$msg."</b></h1><br />Number of guesses = ".$numGuesses;

// In this if statement I am checking to see if $finish was set to 1 when the correct answer was entered in the form. Then I reset $numGuesses back to 0 and $answer back to a null string so the quiz will start over again with another random number. //
if($finish == 1)
{
 
$numGuesses 0;
 
$answer "";
 }
?>

<!-- The form is always displayed again after it has been submitted or the page is refreshed or initalized for the first time.
     And I am loading the values that were submitted, back into the forms input elements. -->
<form action="#" method="post">
<input type="hidden" name="numGuesses" value="<?php echo $numGuesses?>">
<input type="hidden" name="answer" value="<?php echo $answer?>">
Pick a number 1 through 1000: <input id="guess" name="guess" type="text" value="<?php echo $number?>">
<input name="submit" type="submit" value="Submit Guess">
</form>
</body>
</html>
The only thing I didn't do in this code is to make sure $number is an integer and not an alpha character.
So it will not give the correct response if you enter an alpha character.
__________________
Jerry Broughton

Last edited by job0107; 02-25-10 at 01:40 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-26-10, 07: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>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Todays time and date + and - five days with PHP Oskare100 PHP 4 01-22-07 06:46 PM


All times are GMT -5. The time now is 12:32 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.