Different divs in loop

Asked

Viewed 265 times

0

I came across the following problem, with the code below I intended to add more fields without repeating the divs.

Code:

<? foreach($itens as $myrow){ ?>
                    <div id="LISTA1_FUNDO_PRETO">
                    TITULO</div>
                <?}?>

What I really wanted was to do this without divs:

<? foreach($itens as $myrow){ ?>
                        <div id="LISTA1_FUNDO_PRETO">
                        TITULO</div>

<div id="LISTA1_FUNDO_BRANCO">
                        TITULO</div>
                    <?}?>
  • 1

    Now I doubt you want the Divs with numbered id or put a class in each div?

  • your question is on the boundary between "close" or keep open.. If you can answer the @Adir question, I think the question will be more valid.

  • put a class in each div, to make this effect: link that is, in each data that is inserted, first comes the white background, then the black, and then comes back the white and so on ..

1 answer

2


First of all, don’t try to use the id when in place should be class. Equal id’s make HTML invalid. Correct is to use class, especially if the intention is to use the property in css.

Use the foreach thus:

<? foreach($itens as $index => $myrow) {
  $className = $index % 2 == 0 ? "LISTA1_FUNDO_PRETO" : "LISTA1_FUNDO_BRANCO" ?>
  <div class="<? echo $className ?>">TITULO</div>
<? } ?>

Browser other questions tagged

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