Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 01-26-2006, 02:23 AM   #1 (permalink)
 
Newb Techie

Join Date: Jan 2006

Posts: 35

Arbitus

Send a message via AIM to Arbitus
Default PHP Problem

Well I decided to start again on PHP, I got destracted from the book to make a game I thought I could. I've got a lot done but I'm sort of stuck on something. It's a guessing game, where the user guess's a random number using rand(1,100). But when I decided to show the answer to test it, I found out that my random number, which I put in a variable, continues to change. Also, I had it working before, but it seems to not be working now, but when the number is guessed correctly, the congrats text in $win is not printed out. Here is the whole script. It's sloppy I know


Quote:
<html>
<head>
<title>
Guessing Game
</title>
<?php

// Define the variables
$random = rand(1,100);
$user = $_POST['yourguess'];
$highlow = " ";
$win = " ";

if ($user > $random)
{
$highlow = "Lower";
}
else if ($user < $random)
{
$highlow = "Higher";
}
else if ($user == $random)
{
$highlow = "Winner!";
$win = "Congradulations, you won!";
$random = rand(1,100);
}
else
{
$highlow = "s ";
}

?>
</head>
<Body bgcolor="#558BB7" text="#000000">





<center>
<h1>
<?php
echo "$win";
?>
<form method="POST" action="random.php">
<table border="1" width="56%">
<tr>
<td width="47%">
<p align="center">Your Guess:</td>
<td width="53%"><input type="text" name="yourguess" size="20"></td>
</tr>
<tr>
<td width="47%">
<p align="center">Higher or Lower:</td>
<td width="53%">
<?php
echo $highlow;
?>

</td>
</tr>
<tr>
<td width="53%">
<p align="center">Answer:</td>
<td width="53%">
<?php
echo $random;
?>

</td>
</tr>
<tr>
<td width="47%">
<p align="center">Give Up?:</td>
<td width="53%">
<p align="center">
Yes: <INPUT type="checkbox" name="giveup" value="OFF"
</td>
</tr>
<tr>
<td colspan="2">
<p align="center"><input type="submit" value="Guess!"></td>
</tr>
</table>

</form>

</center>

</body>
</html>
Also, how can I make it so the textbox doesn't show old answers. I don't like how it auto-completes like that.

If you don't understand what I'm talking about for either of my questions. You can see that script here: www.arbitus.jaysworldnetworks.com/PHP/random.php
Arbitus is offline  
Old 01-26-2006, 05:49 AM   #2 (permalink)
 
Ultra Techie

Join Date: Jul 2005

Posts: 530

TheHeadFL

Send a message via AIM to TheHeadFL
Default

Simple fix on your main problem:

You need to add an input type "hidden" to your script, and the value of this would be the random number you calculated.

Therefore, at the beginning of the script, if the hidden input is not null, you generate a new random. Otherwise, treat that as your previously decided random value.
__________________
Desktop machine: 2 x Opteron 246, Asus K8N-DL, 2GB PC3200 ECC Reg., XFX GeForce 6600GT, 74gb WD Raptor, 2 x 19\" LCDs, Windows XP x64
Server machine: Intel P4 3.0GHz 2MB EM64T, ECS i865pe, 1GB PC3200, 36gb WD Raptor, Windows Server 2003
Laptop: Dell Inspiron 9100 (Intel P4 3.2GHz 1MB Prescott, i865pe, 512MB PC3200, Mobility Radeon 9700, DVD+R/DL Burner), Windows XP
Linux: P3 450Mhz, 386MB ram, Slackware 10.1 (Running mySQL/Apache)
TheHeadFL is offline  
 
Closed Thread

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On