Select specific items within a while loop

Asked

Viewed 113 times

1

I have the following HTML inside a while PHP:

<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 with PHP to select specific results within the loop:

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

Would select 3n+1 exactly.

  • 1

    You already got something done?

  • @Cesarmiguel managed to get <article> received data-id++ that is, that it was incremented +1 each loop. It was written in the code above. @Bacco, "select" is referencing this link -> http://catalogos.axitech.com.br> where the highlighted images receive different sizes.

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 selected initial value just change the initial value of $i.

  • It worked perfectly as you can see on this link http://catalogos.axitech.com.br ... Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.