Need help with Random Divs code

Reply

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Need help with Random Divs code

 
0
  #1
Nov 9th, 2006
this code will not load on my localhost, but gives no errors. Will you load this and see if it will work:

  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>
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 53
Reputation: Barnz is an unknown quantity at this point 
Solved Threads: 0
Barnz Barnz is offline Offline
Junior Poster in Training

Re: Need help with Random Divs code

 
0
  #2
Nov 9th, 2006
Originally Posted by tefflox View Post
this code will not load on my localhost, but gives no errors. Will you load this and see if it will work:

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: Need help with Random Divs code

 
0
  #3
Nov 9th, 2006
thanks so much for helping. i've been working on this hours, and earlier i had gotten it right, somehow..
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 53
Reputation: Barnz is an unknown quantity at this point 
Solved Threads: 0
Barnz Barnz is offline Offline
Junior Poster in Training

Re: Need help with Random Divs code

 
0
  #4
Nov 9th, 2006
Originally Posted by tefflox View Post
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: -

  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: -

  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: -

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC