Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6113965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:55:10+00:00 2026-05-23T14:55:10+00:00

I tried to use PHP to make DIV random positions. we can look each

  • 0

I tried to use PHP to make DIV random positions. we can look each 100px as a unit, type one small div is 1*1 ,type two small div is 1*2, type three small div is 2*1. All divs only allow show in a big div box 10*6. Here is my code.

<?php
function DIV1()
{
    $choose1 = array("0","100","200","300","400","500","600","700","800","900");
    $choose = array("0","100","200","300","400","500");
    $rand_keys = array_rand($choose, 1);
    $rand_keys1 = array_rand($choose1, 1);
    echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:100px;background:#ff0 none;\"></div>";
}
function DIV2()
{
    $choose1 = array("0","100","200","300","400","500","600","700","800","900");
    $choose = array("0","100","200","300","400");
    $rand_keys = array_rand($choose, 1);
    $rand_keys1 = array_rand($choose1, 1);
    echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:200px;background:#f00 none;\"></div>";
}
function DIV3()
{
    $choose1 = array("0","100","200","300","400","500","600","700","800");
    $choose = array("0","100","200","300","400","500");
    $rand_keys = array_rand($choose, 1);
    $rand_keys1 = array_rand($choose1, 1);
    echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:200px;height:100px;background:#00f none;\"></div>";
}
echo '<div style="width:1000px;height:600px;">';

$sizes = array();
for($i = 0; $i < 15; $i ++) $sizes[] = DIV1($row);
for($i = 0; $i < 10; $i ++) $sizes[] = DIV2($row);
for($i = 0; $i < 5; $i ++) $sizes[] = DIV3($row);
shuffle($sizes);

for($i = 0; $i < 30; $i ++) echo $sizes[$i];
echo '</div>';
?>

I still have some css questions. in my code, I make postion:absolute and top left setting, but some divs will overlapping. how to solve it? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:55:11+00:00Added an answer on May 23, 2026 at 2:55 pm

    1) You should use “return” instead of “echo” in the end line of 3 div functions so that array sizes can have any effect

    2) Your problem is not standard, it is a little bit mathematical challenge, you must do “remembering” of positions and shuffling by hand

    3) can this below code be your solution ? ( test here )

    <?php
    function divHtml($dtype, $x, $y) //$dtype: (1)=1x1yellow, (2)=1x2, (3)=2x1, (4)=1x1white
    {  $ww =($dtype ==3) ?'200px':'100px';    $hh =($dtype==2)?'200px':'100px'; $bgcols =array('#ff0', '#f00', '#00f', '#fff');
       $xx =($x*100) .'px';                   $yy =($y*100) .'px';              $bgc    =$bgcols[$dtype-1];
       return "<div style='position:absolute; width:$ww;height:$hh; left:$xx;top:$yy; background:$bgc;'> $dtype </div>";
    }
    $divs =array(); //real divs array (html inside)
    $cells =array(); //logical filled/notFilled (0/1) array
    for ($i=0; $i<60; $i++) $cells[$i] =0; 
    
    function reserve($dtype, $x, $y)
    {  global $cells, $divs;      if ($y*10+$x >59 ||($cells[$y*10+$x])) return false;
       switch ($dtype)
       {  case 2: if ($y ==5 || $cells[$y*10+$x] ||$cells[($y+1)*10+$x]) return false;  $cells[($y+1)*10+$x] =1; break;
          case 3: if ($x ==9 || $cells[$y*10+$x] ||$cells[$y*10+$x+1])   return false;  $cells[$y*10+$x+1] =1;   break;
       }
       $cells[$y*10+$x] =1;      $divs[] =divHtml($dtype, $x, $y);       return true;
    }
    
    for($i = 0; $i < 10; $i ++) while (!reserve(2, rand(0,9), rand(0,5))) ; //adding 10 blocks of type2 (20cellsTotal)
    for($i = 0; $i < 5; $i ++)  while (!reserve(3, rand(0,9), rand(0,5))) ; //adding  5 blocks of type3 (10cellsTotal)
    for($i = 0; $i < 15; $i ++) while (!reserve(1, rand(0,9), rand(0,5))) ; //adding 15 blocks of type1 (15cellsTotal)
    for($i = 0; $i < 60; $i ++) if (!$cells[$i]) reserve(4, $i%10, floor($i/10)) ; //filling rest 15 cells with type4
                   //^go through all cells, but filling only 15
    
    echo '<div style="position:absolute; width:1000px; height:600px;">';
       foreach ($divs as $single) echo $single;
    echo '</div>';
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly poor of my mathematica. I tried to use php to make a number
I tried to use the Http Authentication Digest Scheme with my php (apache module)
I tried to use InvokeSelf for silverlight to communicate with html: InvokeSelf can take
I have tried to use templates to be able to make a multilingual web
I am retro-fitting an application to make use of a PHP HTTP proxy (for
I'm currently developing a website that will make use of PHP scripts and I'd
I tried to use DriveInfo.IsReady, but it returns false if an unformatted floppy is
I tried to use this on my class library mylib.core.data.dll and got a successful
I tried to use OPTION (MAXRECURSION 0) in a view to generate a list
I tried to use an image file in my HTML template. But when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.