英文:
copy elements in foreach if there are no more than 3
问题
I use a regular slick slider on the page
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('.slider-images').slick({
autoplay: true,
slidesToShow: 3,
arrows: false
});
});
<!-- language: lang-css -->
.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%;}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
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
});
});
英文:
I use a regular slick slider on the page
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
$(document).ready(function() {
$('.slider-images').slick({
autoplay: true,
slidesToShow: 3,
arrows: false
});
});
<!-- language: lang-css -->
.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%;}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
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
});
});
答案1
得分: 1
你可以使用array_merge函数复制图像,然后在图像数量超过4张的情况下使用array_slice来处理。
<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>
英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论