Page 1 of 1

trouble with another beginner example

Posted: Sun May 31, 2009 9:52 pm
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

Re: trouble with another beginner example

Posted: Sun May 31, 2009 10:00 pm
by mikemike
Is register_globals on? If not this won't work as you're not specifically setting $numDice anywhere, just use $_POST['numDice']

Re: trouble with another beginner example

Posted: Mon Jun 01, 2009 3:56 am
by montana111
yes register globals is on. ive tried it in firefox, explorer, and chrome and none of them show anything.

Re: trouble with another beginner example

Posted: Mon Jun 01, 2009 4:22 am
by onion2k
You have name="$numDice". That should be name="numDice".

Also, look up "for loops".

Re: trouble with another beginner example

Posted: Fri Jun 05, 2009 10:32 pm
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.