| | |
Need help with Random Divs code
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
this code will not load on my localhost, but gives no errors. Will you load this and see if it will work:
PHP Syntax (Toggle Plain Text)
<style type="text/css"> <?php $count = 1; function divs() { $int = rand(6, 30); while ($int <= 31) { $t = rand(1, 99); $l = rand(1, 99); $w = rand(2, 7); $h = rand(0.25, 1.25); if( rand(0, 1) == 0) $col = "000049"; else $col = "99ccff"; echo ("div#d".$count." {position:absolute; top:".$t."%; left:".$l."%; width:".$w."in; height:".$h."in;"); echo ("background-color:#".$col.";}"); $count++; } } function implement() { while ($count != 0) { echo ("<div id=\"d".$count."\"></div>"); $count--; } } ?> <?php divs(); ?> a:link {color: #fff; text-decoration: none; outline: none;} a:visited {color: #fff; text-decoration: none; outline: none;} a:active {color: #fff; text-decoration: none; outline: none;} a:hover {color: #fff; text-decoration: none; outline: none;} </style> </head> <body> <?php implement(); ?> </body> </html>
•
•
Join Date: Jan 2006
Posts: 53
Reputation:
Solved Threads: 0
•
•
•
•
this code will not load on my localhost, but gives no errors. Will you load this and see if it will work:
PHP Syntax (Toggle Plain Text)
<style type="text/css"> <?php $count = 1; function divs() { $int = rand(6, 30); while ($int <= 31) { $t = rand(1, 99); $l = rand(1, 99); $w = rand(2, 7); $h = rand(0.25, 1.25); if( rand(0, 1) == 0) $col = "000049"; else $col = "99ccff"; echo ("div#d".$count." {position:absolute; top:".$t."%; left:".$l."%; width:".$w."in; height:".$h."in;"); echo ("background-color:#".$col.";}"); $count++; } } function implement() { while ($count != 0) { echo ("<div id=\"d".$count."\"></div>"); $count--; } } ?> <?php divs(); ?> a:link {color: #fff; text-decoration: none; outline: none;} a:visited {color: #fff; text-decoration: none; outline: none;} a:active {color: #fff; text-decoration: none; outline: none;} a:hover {color: #fff; text-decoration: none; outline: none;} </style> </head> <body> <?php implement(); ?> </body> </html>
Both the $count in implement() and in divs() is not the same as the global $count variable declared above it.
•
•
Join Date: Jan 2006
Posts: 53
Reputation:
Solved Threads: 0
•
•
•
•
thanks so much for helping. i've been working on this hours, and earlier i had gotten it right, somehow..
You have two infinite loops, firstly the $int will always be less than 31 because you are generating a random number between 6 and 31.
And the second WHILE loop, $count will never not equal 1. Because $count is equal to nothing inside the function, look at this for example: -
PHP Syntax (Toggle Plain Text)
<?php $count = 1; function test() { echo($count); } test(); ?>
It outputs nothing.......
But this outputs 1: -
PHP Syntax (Toggle Plain Text)
<?php $count = 1; function test() { global $count; echo($count); } test(); ?>
Using "global" for you should sort out the problem as demostrated by this code that outputs 2: -
PHP Syntax (Toggle Plain Text)
<?php $count = 1; function test() { global $count; $count++; } function test2() { global $count; echo($count); } test(); test2(); ?>
However you need to sort out the first $int WHILE loop problem first.
There are a number of ways you can achieve the same thing, by either passing $count in the functions parameters, returning the new $count value from the functions, or by using the "global" syntax like as shown in the above examples.
Last edited by Barnz; Nov 9th, 2006 at 8:10 pm.
![]() |
Similar Threads
- C++ Random Numbers (C++)
- C-Strings & Random Integers (C++)
- How to put the random integer between 1-100 into the 2 dimensions array (5x5 metrix) (C++)
- C++ Random Numbers (C++)
- random (Java)
Other Threads in the PHP Forum
- Previous Thread: PHP RPM Question
- Next Thread: Automatic email
| Thread Tools | Search this Thread |
apache api array beginner binary body broken buttons cakephp checkbox class cms code cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions global google href htaccess html image include insert ip javascript joomla limit link list login mail mediawiki menu mlm msqli_multi_query multiple mycodeisbad mysql number oop parameter paypal pdf php phpincludeissue problem query radio random recourse recursion regex remote script search seo server sessions sms source sp space speed sql static subdomain syntax system table tag tutorial update upload url validator variable vbulletin video web webdesign white wordpress xml youtube





