Jump to content


Photo

n00b question


  • Please log in to reply
3 replies to this topic

#1 FunkyELF

FunkyELF
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 October 2006 - 08:17 PM

Hey guys,

As an excersize, and to take a break from my book, I tried putting together the following page.  It has a hard coded secret number and you have to guess what it is.  It tells you whether you need to guess higher or lower.  It works okay.  I am wondering what a good way to have a counter would be.  To count the number of guesses.
Right now it counts the guesses but it does so using a visible field which can be edited.  Is there a way to pass information using post without it being visible like it is now?

Thanks,
~Eric

<?php
    $tries = 0;
    if(isset($_POST['okay'])){
        $tries = $_POST['tries'] + 1;
        $secret = 27;
        if($_POST['guess'] < $secret){
            echo "higher<br/>";
        } else if($_POST['guess'] > $secret){
            echo "lower<br/>";
        } else {
            echo "hooray ... got it in " . $tries . " tries<br/>";
        }
        
    }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="guess" value="<?php echo @$_POST['guess']; ?>">Guess<br/>
    <input type="text" name="tries" value="<?php echo $tries; ?>">Tries<br/>
    <input type="submit" name="okay" value="SUBMIT ! ! !"><br/>
</form>


#2 lead2gold

lead2gold
  • Members
  • PipPipPip
  • Advanced Member
  • 164 posts
  • LocationOttawa, On

Posted 13 October 2006 - 08:19 PM


<?php
    $tries = isset($_POST['tries'])?$_POST['tries']:0;
    if(isset($_POST['okay'])){
        $tries = $_POST['tries'] + 1;
        $secret = 27;
        if($_POST['guess'] < $secret){
            echo "higher<br/>";
        } else if($_POST['guess'] > $secret){
            echo "lower<br/>";
        } else {
            echo "hooray ... got it in " . $tries . " tries<br/>";
        }
        
    }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="guess" value="<?php echo @$_POST['guess']; ?>">Guess<br/>
    <input type="hidden" name="tries" value="<?php echo $tries; ?>">Tries<br/>
    <input type="submit" name="okay" value="SUBMIT ! ! !"><br/>
</form>


i modified your code to do what you ask... :) try it out

#3 FunkyELF

FunkyELF
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 13 October 2006 - 08:37 PM

i modified your code to do what you ask... :) try it out


Thanks, I figured it was probably something easy.  I was looking at html forms on w3schools but it didn't mention that where I was looking.

I just bought a book on PHP and MySQL and I'm trying to learn it but it looks like I also need to pick up a book on HTML, CSS, Java script, XML.
A book that would teach all of that would no doubt be bloated and not go into enough detail to be useful but it really seems like a pain to become an expert in all of those things to make a decent web page.

In C, you learn C and you can make a decent program.
In Java, you learn Java and you can make a decent program.
To make a decent web page the right way with PHP, you need to learn at the very least HTML and CSS and to make it functional you'll need to additionally learn MySQL and Java Script.  >:( >:( >:(

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 October 2006 - 09:01 PM

You can also use sessions for this.
<?php
    session_start();
    $tries = isset($_SESSION['tries'])?$_SESSION['tries']:0;
    $secret = isset($_SESSION['secret'])?$_SESSION['secret']:rand(1,100);
    $_SESSION['secret'] = $secret;
    if(isset($_POST['okay'])){
        $tries++;
        $_SESSION['tries'] = $tries;
        if($_POST['guess'] < $secret){
            echo "higher<br/>";
        } else if($_POST['guess'] > $secret){
            echo "lower<br/>";
        } else {
            echo "hooray ... got it in " . $tries . " tries<br/>";
            unset($_SESSION['tries']);
            unset($_SESSION['secret']);
        }

    }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Guess<input type="text" name="guess" value="<?php echo @$_POST['guess']; ?>"><br/>
    <input type="submit" name="okay" value="Try"><br/>
</form>

I also made a few other changes like making your secret number a random number and passing it via a session variable also.
Both the number of tries and the secret number are reset when the person guesses the current secret number.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users