944,147 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 3225
  • PHP RSS
Nov 9th, 2006
0

Need help with Random Divs code

Expand Post »
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)
  1. <style type="text/css">
  2. <?php
  3.  
  4. $count = 1;
  5.  
  6. function divs() {
  7.  
  8. $int = rand(6, 30);
  9.  
  10. while ($int <= 31) {
  11.  
  12. $t = rand(1, 99);
  13. $l = rand(1, 99);
  14. $w = rand(2, 7);
  15. $h = rand(0.25, 1.25);
  16.  
  17. if( rand(0, 1) == 0)
  18. $col = "000049";
  19. else
  20. $col = "99ccff";
  21.  
  22. echo ("div#d".$count." {position:absolute; top:".$t."%; left:".$l."%; width:".$w."in; height:".$h."in;");
  23. echo ("background-color:#".$col.";}");
  24.  
  25. $count++;
  26. }
  27.  
  28. }
  29.  
  30. function implement() {
  31.  
  32. while ($count != 0) {
  33.  
  34. echo ("<div id=\"d".$count."\"></div>");
  35. $count--;
  36.  
  37. }
  38. }
  39.  
  40. ?>
  41.  
  42. <?php
  43. divs();
  44. ?>
  45.  
  46.  
  47. a:link {color: #fff; text-decoration: none; outline: none;}
  48. a:visited {color: #fff; text-decoration: none; outline: none;}
  49. a:active {color: #fff; text-decoration: none; outline: none;}
  50. a:hover {color: #fff; text-decoration: none; outline: none;}
  51.  
  52.  
  53. </style>
  54. </head>
  55. <body>
  56.  
  57. <?php
  58. implement();
  59. ?>
  60.  
  61. </body>
  62. </html>
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Nov 9th, 2006
0

Re: Need help with Random Divs code

Click to Expand / Collapse  Quote originally posted by tefflox ...
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)
  1. <style type="text/css">
  2. <?php
  3.  
  4. $count = 1;
  5.  
  6. function divs() {
  7.  
  8. $int = rand(6, 30);
  9.  
  10. while ($int <= 31) {
  11.  
  12. $t = rand(1, 99);
  13. $l = rand(1, 99);
  14. $w = rand(2, 7);
  15. $h = rand(0.25, 1.25);
  16.  
  17. if( rand(0, 1) == 0)
  18. $col = "000049";
  19. else
  20. $col = "99ccff";
  21.  
  22. echo ("div#d".$count." {position:absolute; top:".$t."%; left:".$l."%; width:".$w."in; height:".$h."in;");
  23. echo ("background-color:#".$col.";}");
  24.  
  25. $count++;
  26. }
  27.  
  28. }
  29.  
  30. function implement() {
  31.  
  32. while ($count != 0) {
  33.  
  34. echo ("<div id=\"d".$count."\"></div>");
  35. $count--;
  36.  
  37. }
  38. }
  39.  
  40. ?>
  41.  
  42. <?php
  43. divs();
  44. ?>
  45.  
  46.  
  47. a:link {color: #fff; text-decoration: none; outline: none;}
  48. a:visited {color: #fff; text-decoration: none; outline: none;}
  49. a:active {color: #fff; text-decoration: none; outline: none;}
  50. a:hover {color: #fff; text-decoration: none; outline: none;}
  51.  
  52.  
  53. </style>
  54. </head>
  55. <body>
  56.  
  57. <?php
  58. implement();
  59. ?>
  60.  
  61. </body>
  62. </html>
Not tried the code, but it may be the scope of your $count variable thats the problem.

Both the $count in implement() and in divs() is not the same as the global $count variable declared above it.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Barnz is offline Offline
53 posts
since Jan 2006
Nov 9th, 2006
0

Re: Need help with Random Divs code

thanks so much for helping. i've been working on this hours, and earlier i had gotten it right, somehow..
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Nov 9th, 2006
0

Re: Need help with Random Divs code

Click to Expand / Collapse  Quote originally posted by tefflox ...
thanks so much for helping. i've been working on this hours, and earlier i had gotten it right, somehow..
Is it working now?

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)
  1. <?php
  2.  
  3. $count = 1;
  4.  
  5. function test()
  6. {
  7. echo($count);
  8. }
  9.  
  10. test();
  11.  
  12. ?>

It outputs nothing.......

But this outputs 1: -

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $count = 1;
  4.  
  5. function test()
  6. {
  7. global $count;
  8.  
  9. echo($count);
  10. }
  11.  
  12. test();
  13.  
  14. ?>

Using "global" for you should sort out the problem as demostrated by this code that outputs 2: -

PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $count = 1;
  4.  
  5. function test()
  6. {
  7. global $count;
  8.  
  9. $count++;
  10. }
  11.  
  12. function test2()
  13. {
  14. global $count;
  15.  
  16. echo($count);
  17. }
  18.  
  19. test();
  20. test2();
  21.  
  22. ?>

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Barnz is offline Offline
53 posts
since Jan 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP RPM Question
Next Thread in PHP Forum Timeline: Diff. between PHP 4 and PHP 5 ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC