Jump to content

tuff one... use rand() to place <div> boxes different


Recommended Posts

Hey

 

Im trying to make a notice_board where all notes are placed randomly. The code works fine, exept that the

notes is laying over each ohter, because they are just giving a random absolute position and they dont care

if there is allready a note in that position.

 

Is it possible to code in PHP that a note can not position is self at a position where there allready are an

ohter note?

 

Here is the code:

 


<?php
$sql = dbquery("SELECT text, date, title FROM ".DB_PREFIX."notice_board");
$notices = array();
while($data = dbarray($sql)){
extract($data);
$notices[] = array('title'=>$title, 'text'=>$text, 'date'=>$date);
}

class NoticeBoard{

var $date;
var $html;
var $notices;

function NoticeBoard($array){
	$this->notices = $array;
	$this->createHeader();
	$this->createNoticeBoardNote();
	$this->createFooter();
}

function createHeader(){
	$this->html = "<div id='notice_board_content'>";
}

function createNoticeBoardNote(){
	foreach($this->notices as $val){
		$randomLeft = rand(430, 1045);
		$randomTop = rand(270, 800);
		$this->html .= "<div style='position: absolute; left: ".$randomLeft."; top: ".$randomTop.";'><div id='note_content'><div id='note_title'>".$val['title']."</div><div id='note_text'>".$val['text']."</div><div id='note_date'>".$val['date']."</div></div></div>";
	}
}

function createFooter(){
	$this->html .= "</div>";
}

function render(){
	echo $this->html;
}
}

$notice_board = &new NoticeBoard($notices);

echo "<div>";
$notice_board->render();
echo "</div>";
?>

 

Stylesheet is not included. I can post if you think it is nessysary.

i would make a 2nd array to store used positions and then check against that array so you don't end up using the same positions again.

 

This might be a problem if you don't want them to overlap at all, however it would require more math on your part - but actually that might not be too hard if you think about it

well if you know the coordinates, and you know the length and width of the DIV (i assume) then you know what area you don't want the next DIV to overlap in:

 

Example:

if it's a square, and it's sides are of length 100, and the coords of your first div are (0,0) then you know that you can't have anything from (0,0) to (100,100).

Instead of randomly positioning your divs, just position your divs to a specific location on the page. Then, put all the content into an array and randomly choose a content from the array. Put that content into the one of the divs and so on. So, something like this:

 

<?php
$contents = array('content1', 'content2', 'content3');
?>
<div style="position: absolute; left: 0px; top: 100px; width: 100px; height: 200px;">
<?php
$rand = mt_rand(0, count($contents));
echo $contents[$rand];
unset($contents[$rand]);
</div>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.