Jump to content

Separating MySQL Rows into <div>'s by group


aspacecodyssey

Recommended Posts

I have a MySQL table set up that stores image paths and descriptions. It has 30 or so rows in it (more are added periodically) -- when displaying them, I'd like to separate them so that a <div> wraps every 9 rows, i.e.

 

<div class="imagegroup">
<a href="image1big.jpg"><img src="image1sm.jpg" /></a>
<a href="image2big.jpg"><img src="image2sm.jpg" /></a>
<a href="image3big.jpg"><img src="image3sm.jpg" /></a>
<a href="image4big.jpg"><img src="image4sm.jpg" /></a>
<a href="image5big.jpg"><img src="image5sm.jpg" /></a>
<a href="image6big.jpg"><img src="image6sm.jpg" /></a>
<a href="image7big.jpg"><img src="image7sm.jpg" /></a>
<a href="image8big.jpg"><img src="image8sm.jpg" /></a>
<a href="image9big.jpg"><img src="image9sm.jpg" /></a>
</div>
<div class="imagegroup">
<a href="image10big.jpg"><img src="image10sm.jpg" /></a>
<a href="image11big.jpg"><img src="image11sm.jpg" /></a>
<a href="image12big.jpg"><img src="image12sm.jpg" /></a>
<a href="image13big.jpg"><img src="image13sm.jpg" /></a>
<a href="image14big.jpg"><img src="image14sm.jpg" /></a>
<a href="image15big.jpg"><img src="image15sm.jpg" /></a>
<a href="image16big.jpg"><img src="image16sm.jpg" /></a>
<a href="image17big.jpg"><img src="image17sm.jpg" /></a>
<a href="image18big.jpg"><img src="image18sm.jpg" /></a>
</div>

 

...and so on. Been toying with it for a while, can't figure it out. Anyone have an idea?

Link to comment
https://forums.phpfreaks.com/topic/177794-separating-mysql-rows-into-s-by-group/
Share on other sites

% divides and gives you a remainder. So if the remainder is zero then your variable is divisible by whatever number you picked.

 

so something like this should be close

 
$counter=0
yourloophere 
{
if($counter==0) { echo "<div>"; }
else if($counter%9==0 && $counter==$numberofdatabaserecords) { echo "</div>"; } 
else if($counter%9==0) echo "</div><div>"; } 

// echo your html here 

$counter++; 
}

 

 

 

$counter++;

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.