tags:

views:

68

answers:

4

Hi all, 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;" :)

A: 

Modulo is your friend.

MrWhite
+8  A: 
if($i % 6 == 0){
    //add class
}

Take a look at the arithmetic operators in the manual.

erenon
+1  A: 

Have a look at the Mod function (%)

somthing like.

if($i % 6 == 0)

edit: beaten to it.

Pino
+1  A: 
$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>';
code_burgar
Don't forget to set `$style` back to empty for the non-multiple of 6 lines!
Wim