php every sixth row add class
I have a mysql query which selects 18 items from the table, but I'd like it to add a class on every 6th item.
Here's my code:
$i = 0;
foreach ($this->Items as $item) {
if ($item->image) {
echo '<div class="storeImages"> <img src="/images/store/'.$item->image.'" width="113" height="153" border="0" alt="'.$item->name.'" title="'.$item->name.'" /> </div>';
};
$i++;
};
I've tried a couple of 开发者_C百科different things, but can't seem to get it working, basically on each 6th item, I want to add style="margin-right: 0px;" :)
if($i % 6 == 0){
//add class
}
Take a look at the arithmetic operators in the manual.
Have a look at the Mod function (%)
somthing like.
if($i % 6 == 0)
edit: beaten to it.
$style = '';
if ($i % 6 == 0) {
$style = ' style="margin-right: 0px;"';
}
echo '<div class="storeImages" ' . $style . '> <img src="/images/store/'.$item->image.'" width="113" height="153" border="0" alt="'.$item->name.'" title="'.$item->name.'" /> </div>';
Modulo is your friend.
精彩评论