M getting stuck with this code, where i am trying to pull all images inside a folder & put them in list or div, to create Bootstrap Carousel.
<div id="spa_gallery" class="carousel slide" data-ride="carousel">
<!-- Carousel indicators -->
<!-- Wrapper for carousel items -->
<div class="carousel-inner">
<?php
$dirname = "spa_gallery/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
$imagelist.= '<li class="item"><a href=""><img src="'.$image.'" /></a> </li>';
}
?>
<!-- Carousel controls -->
<a class="carousel-control left" href="#spa_gallery" data-slide="prev"> <span class="glyphicon glyphicon-chevron-left"></span> </a>
<a class="carousel-control right" href="#spa_gallery" data-slide="next"> <span class="glyphicon glyphicon-chevron-right"></span> </a>
</div>
</div>
where previous code was:
<div class="carousel-inner">
<div class="item active"> <img src="spa_gallery/01.jpg" alt="First Slide">
</div>
<div class="item"> <img src="spa_gallery/02.jpg" alt="Second Slide"> </div>
<div class="item"> <img src="spa_gallery/03.jpg" alt="Third Slide"> </div>
<div class="item"> <img src="spa_gallery/04.jpg" alt="Fourth Slide"> </div>
<div class="item"> <img src="spa_gallery/05.jpg" alt="Fifth Slide"> </div>
</div>
try to use
<?php
$dirname = "spa_gallery/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
?>
<div class="item"><img src="<?php echo $image; ?>" /></div>
<?php
}
or
<?php
$dirname = "spa_gallery/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
$imagelist.= '<div class="item"><img src="'.$image.'" /></div>';
}
echo $imagelist; //<<<<<
?>
for me I used something like the next code
$dir_imgs = "spa_gallery";
$files_imgs = scandir($dir_imgs);
foreach ($files_imgs as $key_imgs => $value_imgs){
$php_imgs = explode(".", $value_imgs);
$php_imgs = end($php_imgs);
if ($php_imgs == "jpg"){
?>
<div class="item"><img src="<?php echo $dir_imgs.'/'.$value_imgs; ?>" /></div>
<?php
}
}
to add class active to img
$class_active = 0; // before foreach
<div class="item <?php echo (($class_active == 0)? 'active' : '') ?>">
then
$class_active = 1;
so your code will be
<?php
$class_active = true;
$dirname = "spa_gallery/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
?>
<div class="item <?php if($class_active == true){ echo 'active' ; $class_active = false} ?>"><img src="<?php echo $dirname.$image; ?>" /></div>
<?php
}