Page 1 of 1

Splitting records across divs

Posted: Wed Oct 18, 2006 5:22 pm
by airjosh
I have a 3 column design of divs, what I want to do is limit 4 records displayed per div. These are specific, named divs, so it can't just be duplicated in an "if" statement.

so div 1 would display the first 4 records, div 2 would have 5-8 and div 3 would have 9-12, if there are any more I will provide a link with 13+

essentially i want to limit the amount of records shown to 12, but split in fours amongst 3 specific divs holding content on my page.

I hope this makes sense, any help would be appreciated.

Posted: Wed Oct 18, 2006 5:34 pm
by feyd
It sounds like you want a fairly simple modification of Useful Posts #1.

Posted: Wed Oct 18, 2006 5:36 pm
by Ollie Saunders
Completely untested. Something like this:

Code: Select all

$divsPerColumn = 3;
$maxColumns = 4;

for ($i=0; $row = mysql_fetch_object($result); ++$i) {
    if ($i > $divsPerColumn * $maxDivs) {
        echo '<a href="foo">More...</a>';
        break;
    }
    if ($i % $divsPerColumn == 0) {
        echo '<div>';
    }
    echo $row->field;
    if ($i % $divsPerColumn == $divsPerColumn -1) {
        echo '</div>';
    }
}

Posted: Wed Oct 18, 2006 5:45 pm
by airjosh
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I looked through this and while it shows how to split records across a table, it generates the table while it gets the data. what I am talking about is displaying into existing containers, not creating the 3 containers as it goes.

this may be a simple modification of the ideas you pointed out, but I am not seeing it. here is the code as I have it, which is just querying the DB and then displaying everything in the first column.

Code: Select all

mysql_select_db($database_chinadirectflooring, $chinadirectflooring);
$query_rebond = "SELECT rebond_id, density, thickness, single_price, volume_price, product_thumb FROM rebond";
$rebond = mysql_query($query_rebond) or die(mysql_error());
$row_rebond = mysql_fetch_assoc($rebond);
$totalRows_rebond = mysql_num_rows($rebond);

...

<div id="leftColumn">
  <?php do { ?>
  <div id="hotProducts">
      <div id="hotProductPix"><img src="<?php echo $row_rebond['product_thumb']; ?>" alt="<?php echo ucwords($row_rebond['density']); ?>"></div>
    </div>
  <?php } while ($totalRows_rebond = mysql_fetch_object($rebond)); ?>
  </div>
 
 <div id="middleColumn">
   </div>
  
<div id="rightColumn">
  </div>
So what I want to do is display only the first four in "left column", the next four in "middleColumn" and the next four in "rightColumn"


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Oct 18, 2006 6:01 pm
by feyd
You could use an array to accumulate the values from the database into, then array_slice() or array_shift() the first four out, move to the next column and repeat, etc.

Posted: Wed Oct 18, 2006 6:44 pm
by Ollie Saunders
what I am talking about is displaying into existing containers, not creating the 3 containers as it goes.
PHP doesn't work like that. Once you have output something, its gone and you can't change it. The alternative (and a fine one it is too) is to do what feyd just said.