make duplicate elements if their number is less than 4 and they are not empty

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

make duplicate elements if their number is less than 4 and they are not empty

问题

我有一个用于浏览图片的滑块,并通过PHP将所有图片添加到滑块中。

情况是,滑块至少需要4张图片才能正常工作,如果我们只添加一两张图片,我编写了相应的代码来复制和粘贴图片。

这是可工作的代码:

<?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) {
        if ($image->image_id) { ?>
            <div class="slider-image">
                <img src="<?= $image->url ?>" alt="<?= $image->name ?>">
            </div>
        <?php } ?>
    <?php } ?>
<?php } ?>

还有这一行:

if ($image->image_id)

也就是说,我可能会遇到这样一种情况,即数据库中没有这样的图片,为了避免错误,我添加了适当的检查。

因此,如果我有一张不在数据库中的图片,那么这段复制代码不起作用,因为它会复制一个空白的图片,然后不会显示它。

我该如何考虑图片不在数据库中的因素并修正复制的代码?或者我需要在这里使用另一种实现方法吗?

英文:

I have a slider for scrolling through images, and I add all the images to the slider through php

The situation is that the slider needs at least 4 images to work, and I wrote the corresponding code that copies and pastes the images, if we add only one or two

Here is the working code

&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) {
        if ($image-&gt;image_id) { ?&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;?php } ?&gt;

And there is this line

if ($image-&gt;image_id)

That is, I may have a situation that such an image is not in the database, and so that there are no errors, I added the appropriate check

So, if I have an image that is not in the database, then this copy code does not work, since it copies an empty image, which is then not displayed

$images = array_merge($images, $images);
$images = array_merge($images, $images);
$images = array_slice($images, 0, 4);

How can I take into account the factor that the picture is not in the database and correct the code for copying? Or do I need to use another implementation here?

答案1

得分: 3

我假设你的`$images`不为空且是一个常规列表(具有顺序的从零开始的索引)。在循环中将图像数组填充如下:

```php
$images = [0,1];
$i = 0;
while( count( $images ) &lt; 4 )
    $images[] = $images[ $i++ ];


print_r($images); # [0,1,0,1]


/*
对于给定的初始数组,我们将得到以下结果:

[0] -&gt; [0,0,0,0]
[0,1] -&gt; [0,1,0,1]
[0,1,2] -&gt; [0,1,2,0]
[0,1,2,3] -&gt; [0,1,2,3]
*/

如果你有一些虚拟的图像对象 - 先将它们过滤掉:

$images = array_filter($images, fn($image) =&gt; !empty($image-&gt;image_id) );
英文:

I presume your $images is not empty and is a regular list (has sequental zero-based indices). Just populate the images array in the loop as:

$images = [0,1];
$i = 0;
while( count( $images ) &lt; 4 )
    $images[] = $images[ $i++ ];


print_r($images); # [0,1,0,1]


/*
For given initial arrays we will get following results:

[0] -&gt; [0,0,0,0]
[0,1] -&gt; [0,1,0,1]
[0,1,2] -&gt; [0,1,2,0]
[0,1,2,3] -&gt; [0,1,2,3]
*/

If you have some dummy images objects - filter them out first:

$images = array_filter($images, fn($image) =&gt; !empty($image-&gt;image_id) );

huangapple
  • 本文由 发表于 2023年6月29日 23:15:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582412.html
匿名

发表评论

匿名网友

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

确定