trouble with another beginner example

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
montana111
Forum Newbie
Posts: 6
Joined: Thu May 28, 2009 7:51 pm

trouble with another beginner example

Post by montana111 »

im trying to make a program that prints out various amouns of random dice like so:

Code: Select all

 
<html>
<head>
<title>Create 4 10 or 20 random dice</title>
</head>
 
<body bgcolor="#ffcc99">
<h3>Creating 4 random dice</h3>
 
<?php
 
global $numDice;
if(empty($numDice)){
    print howManyDice();
} else {
    switch($numDice){
                case 4:
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        break;
              case 10:
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();  
                        createRandDice();
                        createRandDice();
                        break;
                case 20:
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();  
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();
                        createRandDice();  
                        createRandDice();
                        createRandDice();
                        break;
                default:
                        createRandDice();
                        break; 
            }
}
 
function createRandDice(){
                 $rand = rand(1,6);
                 print <<<HERE 
                 <img src="$randDie.jpg"
                                                        alt="$rand"
                                                        height ="100"
                                                        width ="100">
HERE;
}
 
function howManyDice(){
                 $output = <<<HERE
                    Enter 4, 10, or 20:
                    <form method ="post">
                                <input type ="text"
                                             name ="$numDice"
                                             value ="">
                                             <input type ="submit"
                                            
 value ="Make dice">
                    </form>
HERE;
return $output;
}
?>
 
</body>
</html>
 
 
when i run it it doesnt do anything. i just get a white page. my editor shows some output but it is still incorrect. it shows the background, some text from my code and then a text box and more text from my code. i.e. it kind of prints the form from howMany().

Can someone help me? thanks
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: trouble with another beginner example

Post by mikemike »

Is register_globals on? If not this won't work as you're not specifically setting $numDice anywhere, just use $_POST['numDice']
montana111
Forum Newbie
Posts: 6
Joined: Thu May 28, 2009 7:51 pm

Re: trouble with another beginner example

Post by montana111 »

yes register globals is on. ive tried it in firefox, explorer, and chrome and none of them show anything.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: trouble with another beginner example

Post by onion2k »

You have name="$numDice". That should be name="numDice".

Also, look up "for loops".
montana111
Forum Newbie
Posts: 6
Joined: Thu May 28, 2009 7:51 pm

Re: trouble with another beginner example

Post by montana111 »

im just getting back to this again. still nothing works. i tried messing around with the array stuff but that doesnt seem to do the trick (im probably doing it wrong). all i did was replace the part where i declare " global $numDice " with $_POST['numDice'] but again nothing. i replaced value="$numDice" with value="numDice" also. still doesnt work. any more suggestions? thanks guys.
Post Reply