954,190 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with Random Divs code

this code will not load on my localhost, but gives no errors. Will you load this and see if it will work:

<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>
tefflox
Junior Poster
174 posts since Jul 2006
Reputation Points: 12
Solved Threads: 1
 

this code will not load on my localhost, but gives no errors. Will you load this and see if it will work:

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

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.

Barnz
Junior Poster in Training
53 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

thanks so much for helping. i've been working on this hours, and earlier i had gotten it right, somehow..

tefflox
Junior Poster
174 posts since Jul 2006
Reputation Points: 12
Solved Threads: 1
 
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

$count = 1;

function test()
{	
	echo($count);
}

test();

?>


It outputs nothing.......

But this outputs 1: -

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

$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.

Barnz
Junior Poster in Training
53 posts since Jan 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You