Every N replicates display X in PHP

2

I'm exercising the knowledge in PHP and I've had a question. How do I display a text after N DO loop repeats?

    <?php do { ?>
    <div class="fotos">
    <h1><?php echo $titulo_da_foto; ?></h1>
    <img src="upload/<?php echo $arquivo; ?>" />
    </div>
    <?php } while ($rsVarejo > 0); ?>

In this case, I would like to hide the background of the class photos in the 4th repetition, any suggestions?

    
asked by anonymous 19.03.2014 / 18:48

4 answers

5

You could do this:

<?php
$counter = 0;
do { 
  $counter++;
  if ($counter == 4){ 
    ?>
      <div class="fotos ocultar">
    <?php 
  }
  else {
    ?>
      <div class="fotos">
    <?php
       }
    ?>
  <h1><?php echo $titulo_da_foto; ?></h1>
  <img src="upload/<?php echo $arquivo; ?>" />
  </div>
<?php
   } while ($rsVarejo > 0); ?>

And add a CSS class "ocultar" :

.ocultar {
  background: none !important;
}
    
19.03.2014 / 19:25
2

One possible solution would be in jquery:

 $( ".classdesejada:nth-child(4)" ).css("background", "rgba(0, 0, 0, 0)" );
    
19.03.2014 / 19:02
1

I think this solves your problem:

<?php 
    $aux = 0;
    do { 
          $aux++;
          if($aux >= 4){
              $nomeDaClasse = 'classe';
              $aux = 0;
          }
?>
          <div class="<?php echo $nomeDaClasse;?>">
             <h1><?php echo $titulo_da_foto; ?></h1>
             <img src="upload/<?php echo $arquivo; ?>" />
          </div>
<?php 
       } while ($rsVarejo > 0);
?>
    
19.03.2014 / 19:00
-5
<?php    
    $fim = 100;
    $incremento = 1;
    $diferente = 4;
    $contador=0;
    do {  
        $contador = $contador + $incremento;
        $resto = contador % diferente;
        ?><div class="<?php
        if ( $resto != 0 ) {
        echo $id_do_estilo_css_normal;
       } else {
       echo $id_do_estilo_css_diferente;
     }
     ?>">Conteudo do elemento, tag, etiqueta
    </div>
    } while ( $contador < $fim); 
?>

Recommendations: The original coding style of this post should be rethought. In it there is a mixture of conceptual layers. This style of project design is not used for several reasons. Study and use Model, View, Controller among other models to update your learning.

I wish you all a lot of luck in your studies and thank you for reading this text.

    
20.02.2017 / 20:33