Select specific items within a while loop

1

I have the following HTML inside a PHP while fault:

<article data-id="1">...</artcile>
<article data-id="2">...</artcile>
<article data-id="3">...</artcile>
<article data-id="4">...</artcile>
<article data-id="5">...</artcile>
<article data-id="6">...</artcile>
<article data-id="7">...</artcile>
<article data-id="8">...</artcile>
<article data-id="9">...</artcile>
<article data-id="10">...</artcile>
<article data-id="11">...</artcile>
<article data-id="12">...</artcile>
<article data-id="13">...</artcile>
<article data-id="14">...</artcile>
<article data-id="..."></artcile>

I would like PHP to select specific results within the loop:

1, 4, 7, 10, 13 ...

It would select 3n+1 exactly.

    
asked by anonymous 01.09.2014 / 17:19

1 answer

4

For example:

$i = 0;
while .... {
   if ( $i++ % 3 == 0 ) {
      // selecionado
   } else {
      // não selecionado
   }
}

Or even:

$i = 0;
while .... {
   echo '<article data-id="..."'.( $i++ % 3 == 0 ? ' class="selected"' : '' ).'>...</article>'
}
  

To change the offset of the initial selection, simply change the initial value of $i .

    
01.09.2014 / 17:31