Different divs in loop

0

I find the following problem, with the code below I wanted to add more fields without repeating 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>
                    <?}?>
    
asked by anonymous 24.04.2015 / 02:08

1 answer

2

First of all, do not try to use id when in place it should be class . Id's alike make HTML invalid. The correct one is to use class , especially if the intention is to use the property in css.

Use foreach as follows:

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