在foreach中复制元素,如果不超过3个。

huangapple go评论80阅读模式
英文:

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() {
    $(&#39;.slider-images&#39;).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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/slick-carousel@1.8.1/slick/slick.css&quot; rel=&quot;stylesheet&quot;&gt;

&lt;div class=&quot;slider-images&quot;&gt;
        &lt;div class=&quot;slider-image&quot;&gt;
            &lt;img src=&quot;https://i.imgur.com/CUmIpxR.jpg&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;slider-image&quot;&gt;
            &lt;img src=&quot;https://i.imgur.com/g7aX7wR.jpg&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;slider-image&quot;&gt;
            &lt;img src=&quot;https://i.imgur.com/cPToo7F.jpg&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;slider-image&quot;&gt;
            &lt;img src=&quot;https://i.imgur.com/qVbD6iK.png&quot;&gt;
        &lt;/div&gt;
&lt;/div&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/jquery.slick/1.4.1/slick.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

I use php to display images in this slider

&lt;div class=&quot;slider-images&quot;&gt;
    &lt;?php foreach ($images as $image) { ?&gt;
        &lt;div class=&quot;slider-image&quot;&gt;
            &lt;img src=&quot;&lt;?= $image-&gt;url?&gt;&quot;&gt;
        &lt;/div&gt;
    &lt;?php } ?&gt;
&lt;/div&gt;

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

&lt;div class=&quot;slider-images&quot;&gt;
    &lt;?php if (count($images) &lt; 4) { ?&gt;
        
    &lt;?php } else { ?&gt;
        &lt;?php foreach ($images as $image) { ?&gt;
            &lt;div class=&quot;slider-image&quot;&gt;
                &lt;img src=&quot;&lt;?= $image-&gt;url?&gt;&quot;&gt;
            &lt;/div&gt;
        &lt;?php } ?&gt;
    &lt;?php } ?&gt;
&lt;/div&gt;

And is it possible to do cloning in javascript itself?

$(document).ready(function() {
    $(&#39;.slider-images &#39;).slick({
        autoplay: true,
        slidesToShow: 3,
        arrows: false
    });
});

答案1

得分: 1

你可以使用array_merge函数复制图像,然后在图像数量超过4张的情况下使用array_slice来处理。

&lt;div class=&quot;slider-images&quot;&gt;
    &lt;?php if (count($images) &lt; 4) { ?&gt;
        &lt;?php
        $images = array_merge($images, $images);
        $images = array_merge($images, $images);
        $images = array_slice($images, 0, 4);

        foreach ($images as $image) { ?&gt;
            &lt;div class=&quot;slider-image&quot;&gt;
                &lt;img src=&quot;&lt;?= $image-&gt;url ?&gt;&quot; alt=&quot;&lt;?= $image-&gt;name ?&gt;&quot;&gt;
            &lt;/div&gt;
        &lt;?php }
        ?&gt;
    &lt;?php } else { ?&gt;
        &lt;?php foreach ($images as $image) { ?&gt;
            &lt;div class=&quot;slider-image&quot;&gt;
                &lt;img src=&quot;&lt;?= $image-&gt;url ?&gt;&quot; alt=&quot;&lt;?= $image-&gt;name ?&gt;&quot;&gt;
            &lt;/div&gt;
        &lt;?php } ?&gt;
    &lt;?php } ?&gt;
&lt;/div&gt;
英文:

You can duplicate images with array_merge function and then slice array in case there are more than 4 images.

&lt;div class=&quot;slider-images&quot;&gt;
    &lt;?php if (count($images) &lt; 4) { ?&gt;
        &lt;?php
        $images = array_merge($images, $images);
        $images = array_merge($images, $images);
        $images = array_slice($images, 0, 4);

        foreach ($images as $image) { ?&gt;
            &lt;div class=&quot;slider-image&quot;&gt;
                &lt;img src=&quot;&lt;?= $image-&gt;url ?&gt;&quot; alt=&quot;&lt;?= $image-&gt;name ?&gt;&quot;&gt;
            &lt;/div&gt;
        &lt;?php }
        ?&gt;
    &lt;?php } else { ?&gt;
        &lt;?php foreach ($images as $image) { ?&gt;
            &lt;div class=&quot;slider-image&quot;&gt;
                &lt;img src=&quot;&lt;?= $image-&gt;url ?&gt;&quot; alt=&quot;&lt;?= $image-&gt;name ?&gt;&quot;&gt;
            &lt;/div&gt;
        &lt;?php } ?&gt;
    &lt;?php } ?&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月26日 03:02:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定