Counter that every 20 images he prints a block

Asked

Viewed 41 times

0

I’m making a page where there will be a slide, and each slide of this will have 20 photos, I found a code on the internet, where you just specify the folder where ta images and it generates all the code. The problem I’m facing is that to function properly, every 25 images it has to involve those images with a class. Ex: I will illustrate here with the example of only 3 photos, it will look like this

<div class="item">
imagem
imagem
imagem
</div>

<div class="item">
imagem
imagem
imagem
</div>

Here below I’ll post the code I’m using

        <?php
    // variável que define o diretório das imagens
    $dir = "imagens/galeria/";
    $dh = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        // verificando se o arquivo é .jpg
        if (substr($filename, -4) == ".jpg") {
            ?>
        <li><a  data-fancybox="galeria" href="<?php echo $dir; ?><?php echo $filename; ?>" style="background-image: url(<?php echo $dir; ?><?php echo $filename; ?>)"></a></li>
        <?php
    }
}
?>
  • It was not clear, the division will only be made if you have from 25 images? if you have 6 images, should not be done?

  • 25, 20 or 3 or other value?

  • the division is every 25 images, I made with three just to exemplify, but by the way I confused more rs

1 answer

1


<?php
    $contador = 0;
    $abreTag = TRUE;

    // variável que define o diretório das imagens
    $dir = "imagens/galeria/";
    $dh = opendir($dir);
    while (false !== ($filename = readdir($dh))) {
        // verificando se o arquivo é .jpg
        if (substr($filename, -4) == ".jpg") {    
            if ($contador % 25 == 0 && $abreTag) {
                $abreTag = FALSE;
                $contador = 1;
        ?>
            <div class="bendita-classe">
        <?php
            } ?>
    <li><a  data-fancybox="galeria" href="<?php echo $dir; ?><?php echo $filename; ?>" style="background-image: url(<?php echo $dir; ?><?php echo $filename; ?>)"></a></li>
        <?php
            if ($contador % 25 == 0 && !$abreTag) {
                $abreTag = TRUE;
                $contador = -1;
        ?>
            </div>
        <?php 
            }
        $contador += 1;
    }
}
?>
  • Was it right, man, could you explain this code so I could understand better? yes makes a correction there, missing to make the closing of the php tag before the li

  • Blz, vlw! Fixed the code. The variable $contador counts the lines. When it is 0 or 25, it enters both ifs and checks $abreTag. If this is true, it opens the tag, as the name says, and if it is false, closes the tag.

  • Massa, Marcelo, is it possible to sort by name? after I put it on the server, the photos were all out of order, see http://ggetic.hostdime.com.br/

Browser other questions tagged

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