TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Creating a random number once and keeping it (http://www.talkphp.com/absolute-beginners/6128-creating-random-number-once-keeping.html)

bgs 02-14-2012 04:35 PM

Creating a random number once and keeping it
 
Hi,

I'm just beginning to learn PHP (day 2) and I can't really seem to figure this one out. My idea is a simple number guessing game that works something like this:

$num = rand(0,100);

(Accept $guess from user input)

if $num < $guess echo 'too low' etc.

The form references itself and sends the guess via POST. My problem is that the random number is getting set each time. I come from a forms/console development background and this is the first time I've delved into web programming, so there's some element here regarding the 'session' that isn't quite straight in my head.

Please feel free to enlighten me, poke fun at my code, etc.! Thanks for your help.

This is my code. It's almost entirely lifted from "Sams Teach Yourself PHP, MySQL and Apache" but I've modified the functionality to attempt to use a random number.

Code:

<?php
        $num = rand(0,100);
        if (!isset($_POST["guess"]))
        {
                $message = "What number am I thinking of?";
        }
        else if ($_POST["guess"] > $num)
        {
                $message = "Too high, guess again!";
        }
        else if ($_POST["guess"] < $num)
        {
                $message = "Too low, guess again!";
        }
        else
        {
                $message = "Correct!";
        }
?>

<html>
<head><title>Number guess</title></head>
<body>
<?php echo $message ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<input type="text" name="guess" />
<p><input type="submit" value="Submit"/></p>
</form>
<?php echo $num ?> <!-- just so I can see the output -->
</body>
</html>


bgs 02-14-2012 05:03 PM

Well, I've fixed it by using a hidden input and passing the value along via POST. Here's the working code:

Code:

<?php

        if (!isset($_POST["guess"]))
        {
                $message = "What number am I thinking of?";
                $num = rand(0,100);
        }
        else
        {
                $num = $_POST['num'];
                if ($_POST["guess"] > $num)
                {
                        $message = "Too high, guess again!";
                }
                else if ($_POST["guess"] < $num)
                {
                        $message = "Too low, guess again!";
                }
                else
                {
                        $message = "Correct!";
                }
}
?>

<html>
<head><title>Number guess</title></head>
<body>
<?php echo $message ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<input type="text" name="guess"/>
<input type="hidden" name="num" value="<?php echo $num ?>" />
<p><input type="submit" value="Submit"/></p>
</form>
<?php echo $num ?>
</body>
</html>


tony 02-15-2012 04:50 AM

The thing about web development is that it is stateless due to the nature of http requests. Meaning that you can't save state between requests (for example when you submit a form or click on a link). There is a few things you can do here to save the random number between requests, you can use a cookie to save it and then obtain it later, another would be using sessions (which sometimes are implemented using cookies). The cookies are saved on the client while the sessions are most of the time saved in client and sometimes in server. But on the server side you can also save it in a database or a local file.

Or you can also use the hidden input tag to keep it in the between requests.

The next is an example of how it would look using the hidden input. Just gotta make sure the

PHP Code:
<?php
$message = "What number am I thinking of?";
$num = $_POST['num'] ? $_POST['num'] : rand(0,100);
if (isset($_POST["submit"])) {
  $num = $_POST['num'];
  if ($_POST['guess'] > $num) {
    $message = "Too high, guess again!";
  } elseif ($_POST['guess'] < $num) {
    $message = "Too low, guess again!";
  } else {
    $message = "Correct!";
  }
}
?>

<html>
<head><title>Number guess</title></head>
<body>
<?php echo $message ?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST">
<input type="text" name="guess"/>
<input type="hidden" name="num" value="<?php echo $num ?>" />
<p><input type="submit" name="submit" value="Submit"/></p>
</form>
<?php echo isset($_POST['submit']) ? $num : ''; ?>
</body>
</html>

jojoone 04-01-2012 08:37 AM

Even though it's correct which you ought to educate oneself in affiliate advertising, the best sources Karen Millen Dresses are not programs. However, books are.Karen Millen Coats
Karen Millen One Shoulder Dress By basically studying up on affiliate advertising, you'll be able to get fantastic insights and preserve lots of funds. By simply looking on retail sites such as Amazon or EBay, it is possible to get hundred of titles on the subject.Karen
Millen Solid Color Dresses
You can find lots of step-by-step guides obtainable on the way to get your affiliate
advertising and marketing business going and the way to develop it up.
One more great useful resource for affiliate advertising may be the web. Merely by typing affiliate advertising and
marketing into any major online search engine, lots of info arrives up. You will find lots of free classes online that you
could take around the subject in the event you are established. Read by means of the information readily available for you for absolutely nothing.Karen Millen Multicolor Dresses Karen Millen Skirts Chances really are a course or a seminar will not educate you anymore than what you are able to learn on the web at no cost.


All times are GMT. The time now is 11:54 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0