0

I am trying to build several carousels with Bootstrap using parameters and for this, and to avoid repeating code, I used a variable $i to fetch the images. The problem is that each folder has different amount of images. Using ($i=1; $i <= 30; $i++) shows empty sliders for the carousels with less than 30 files and it does not show all sliders for the carousels with more than 30 files. Is there a way to calculate the numbers of files in each project folder?

Here is my code:

<div id="carouselExampleControls" class="carousel slide" data-ride="carousel" data-interval="false">
    <div class="carousel-inner">
        <?php
            for ($i=1; $i <= 30; $i++) {
                if ($i==1) {
                        print '<div class="carousel-item active"><img class="d-block w-100 img-fluid" src="img/project_'.$project.'/slider_'.$i.'.jpg" alt="project slider"></div>';
                }
                else {
                    print '<div class="carousel-item"><img class="d-block w-100 img-fluid" src="img/project_'.$project.'/slider_'.$i.'.jpg" alt="projectslider"></div>';
                }
            }
        ?>
    </div>
    <a class="carousel-control-prev carousel-project-prev" href="#carouselExampleControls" role="button" data-slide="prev">
        <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next carousel-project-next" href="#carouselExampleControls" role="button" data-slide="next">
        <span class="sr-only">Next</span>
    </a>
</div>
ohg
  • 3
  • 2

1 Answers1

0

Did you search here first?

List all files in one directory PHP is a potential answer, which you can use, and get a list of files, add them to an array, and then get the count($array); as total count, or even better use foreach in that array to show each one of them.

Ron
  • 5,900
  • 2
  • 20
  • 30