Jump to content

n00b question


FunkyELF

Recommended Posts

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

[code]<?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>[/code]
Link to comment
Share on other sites

[quote author=FunkyELF link=topic=111438.msg451648#msg451648 date=1160770643]

[code]<?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>[/code]
[/quote]

i modified your code to do what you ask... :) try it out
Link to comment
Share on other sites

[quote author=lead2gold link=topic=111438.msg451650#msg451650 date=1160770790]
i modified your code to do what you ask... :) try it out
[/quote]

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.  >:( >:( >:(
Link to comment
Share on other sites

You can also use sessions for this.
[code]<?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>[/code]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.