JJohnsenDK Posted January 10, 2008 Share Posted January 10, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/ Share on other sites More sharing options...
nikefido Posted January 10, 2008 Share Posted January 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/#findComment-435549 Share on other sites More sharing options...
JJohnsenDK Posted January 10, 2008 Author Share Posted January 10, 2008 well yes the it takes alot of math. Your suggention wont work because it only stores the percis coords and the next could position just 1px away from that. Any math genius here? Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/#findComment-435582 Share on other sites More sharing options...
nikefido Posted January 10, 2008 Share Posted January 10, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/#findComment-435605 Share on other sites More sharing options...
duclet Posted January 10, 2008 Share Posted January 10, 2008 You are better off having the div set in stone and then random the content in the divs. Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/#findComment-435609 Share on other sites More sharing options...
JJohnsenDK Posted January 10, 2008 Author Share Posted January 10, 2008 duclet... plx explain more, dont quite understand what you mean... nikefido... i understand your point and will try that Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/#findComment-435619 Share on other sites More sharing options...
duclet Posted January 10, 2008 Share Posted January 10, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/#findComment-435621 Share on other sites More sharing options...
JJohnsenDK Posted January 10, 2008 Author Share Posted January 10, 2008 aaah.... now i understand what you mean... its doesnt demand all that math, i might do with this solution, eventho i have to create alot of <div> manually... thanks to both of you Quote Link to comment https://forums.phpfreaks.com/topic/85369-tuff-one-use-rand-to-place-boxes-different/#findComment-435633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.