SoulieBaby
SoulieBaby

Reputation: 5471

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 different things, but can't seem to get it working, basically on each 6th item, I want to add style="margin-right: 0px;" :)

Upvotes: 0

Views: 1252

Answers (4)

code_burgar
code_burgar

Reputation: 12323

$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>';

Upvotes: 1

LiamB
LiamB

Reputation: 18606

Have a look at the Mod function (%)

somthing like.

if($i % 6 == 0)

edit: beaten to it.

Upvotes: 1

erenon
erenon

Reputation: 19158

if($i % 6 == 0){
    //add class
}

Take a look at the arithmetic operators in the manual.

Upvotes: 10

MrWhite
MrWhite

Reputation: 193

Modulo is your friend.

Upvotes: 1

Related Questions