General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 30th, 2003, 07:55 AM
aqua aqua is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 aqua User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Undefined index and variable

Hi all,

When I first load the file, an error msg of undefined index: guess pop up. After the first guess, the error msg is gone.

Another problem, the num_tries won't increment.

Can anybody help me to solve these problem?

<?php
$num_to_guess = 42;
Static $num_tries = 0;
$num_tries = (isset($_POST["num_tries"])) ? $num_tries+=1 : 0;
$message = "";
if (!isset($_POST["guess"])) {
$message = "Welcome to the guessing machine!";
} elseif ($_POST["guess"] > $num_to_guess ) {
$message = "$_POST[guess] is too big! Try a smaller number";
} elseif ($_POST["guess"] < $num_to_guess ) {
$message = "$_POST[guess] is too small! Try a larger number";
} else { // must be equivalent
$message = "Well done!";
}
$guess = (int) $_POST["guess"];
?>
<html>
<body>
<h1>
<?php print $message ?>
</h1>
Guess number: <?php print $num_tries?>
<form action="<?php print $_SERVER["PHP_SELF"] ?>" method="POST">
Type your guess here:
<input type="text" name="guess" value="<?php print $guess?>">
<input type="hidden" name="num_tries" value="<?php print $num_tries?>">
</form>
</body>

Thx a lot!
aqua

Reply With Quote
  #2  
Old November 30th, 2003, 11:16 AM
Bruski Bruski is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Toronto, Canada
Posts: 192 Bruski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
i would do it in a loop much more efficent

Reply With Quote
  #3  
Old November 30th, 2003, 11:03 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
The message you're getting is only a warning. Most production servers have these types of warnings turned off...

However, an easy way to fix this would be to put the entire bulk of the program into an if statement...

I would re-write your code like this:

PHP Code:
<?php 
Static $num_tries 0
$num_to_guess 42
$guess = (int) $_POST["guess"]; 
$message ""

if (isset(
$_POST['guess'])) { 
    
$num_tries = (isset($_POST["num_tries"])) ? $num_tries+=0
    if (!isset(
$_POST["guess"])) { 
    
$message "Welcome to the guessing machine!"
    } elseif (
$_POST["guess"] > $num_to_guess ) { 
    
$message "$_POST[guess] is too big! Try a smaller number"
    } elseif (
$_POST["guess"] < $num_to_guess ) { 
    
$message "$_POST[guess] is too small! Try a larger number"
    } else { 
// must be equivalent 
    
$message "Well done!"
    } 


?> 
<html> 
<body> 
<h1> 
<?php print $message ?> 
</h1> 
Guess number: <?php print $num_tries?> 
<form action="<?php print $_SERVER["PHP_SELF"?>" method="POST"> 
Type your guess here: 
<input type="text" name="guess" value="<?php print $guess?>"> 
<input type="hidden" name="num_tries" value="<?php print $num_tries?>"> 
</form> 
</body> 
</html>

Reply With Quote
  #4  
Old December 5th, 2003, 07:59 AM
aqua aqua is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 2 aqua User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Increment problem!

The problem on incrementing num_tries. Anybody knows how to solve it?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Undefined index and variable


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT