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
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++;

}

Link to comment
Share on other sites

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.