Every N repetitions display X in PHP

Asked

Viewed 349 times

2

I am exercising my knowledge in PHP and I have a question. How do I display a text after N repetitions of the DO loop?

    <?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, some suggestion?

  • Do you want every four photos to hide and re-show in the next three, and re-hide, or permanently hide from the fourth? It would be good for you for an example of the desired output in the question.

4 answers

5


You could do it:

<?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;
}

2

A possible solution would be in jquery:

 $( ".classdesejada:nth-child(4)" ).css("background", "rgba(0, 0, 0, 0)" );

1

I think that 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);
?>

-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 design is not used for several reasons. Study and use the model Model, View, Controller and other models to update your learning.

I wish you all good luck in your studies and thank you for reading this text.

  • 1

    $resto = $contador % $n_interacoes;

Browser other questions tagged

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