I use a regular slick slider on the page
$(document).ready(function() {
$('.slider-images').slick({
autoplay: true,
slidesToShow: 3,
arrows: false
});
});
.slider-images {width:100%;margin:0px auto;}
.slider-images .slider-image {position:relative;}
.slider-images .slick-slide {margin:10px;}
.slider-images .slick-slide img {width:100%;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css" rel="stylesheet">
<div class="slider-images">
<div class="slider-image">
<img src="https://i.imgur.com/CUmIpxR.jpg">
</div>
<div class="slider-image">
<img src="https://i.imgur.com/g7aX7wR.jpg">
</div>
<div class="slider-image">
<img src="https://i.imgur.com/cPToo7F.jpg">
</div>
<div class="slider-image">
<img src="https://i.imgur.com/qVbD6iK.png">
</div>
</div>
<script src="https://cdn.jsdelivr.net/jquery.slick/1.4.1/slick.min.js"></script>
I use php to display images in this slider
<div class="slider-images">
<?php foreach ($images as $image) { ?>
<div class="slider-image">
<img src="<?= $image->url?>">
</div>
<?php } ?>
</div>
The following question follows, in my slider settings there are 3 pictures for slides, that is, for it to work, I need to upload 4 pictures
But I may have such a situation that I will upload 1 or 2 pictures
How can I make it so that when I upload 1 image on the PHP side, then 3 more images will be created during the output. The same thing if I upload 2 pictures, it should copy 2 more
Accordingly, if I myself upload 4 pictures or more, then nothing needs to be done
It turns out that I need to count the number of elements, and if there are less than 4 to copy in the for loop
If there is only one image, then it seems not so difficult, but what if I have 2 images, let's say? I then need to make 2 copies of the first image, and it's not all clear how to do it
<div class="slider-images">
<?php if (count($images) < 4) { ?>
<?php } else { ?>
<?php foreach ($images as $image) { ?>
<div class="slider-image">
<img src="<?= $image->url?>">
</div>
<?php } ?>
<?php } ?>
</div>
And is it possible to do cloning in javascript itself?
$(document).ready(function() {
$('.slider-images ').slick({
autoplay: true,
slidesToShow: 3,
arrows: false
});
});
You can duplicate images with array_merge function and then slice array in case there are more than 4 images.
<div class="slider-images">
<?php if (count($images) < 4) { ?>
<?php
$images = array_merge($images, $images);
$images = array_merge($images, $images);
$images = array_slice($images, 0, 4);
foreach ($images as $image) { ?>
<div class="slider-image">
<img src="<?= $image->url ?>" alt="<?= $image->name ?>">
</div>
<?php }
?>
<?php } else { ?>
<?php foreach ($images as $image) { ?>
<div class="slider-image">
<img src="<?= $image->url ?>" alt="<?= $image->name ?>">
</div>
<?php } ?>
<?php } ?>
</div>