简体   繁体   English

图像 slider 对于 no_image 无法正常工作

[英]image slider is not working properly for no_image

I have image slider showing images which user uploaded.我有图像 slider 显示用户上传的图像。 Every user has to uploaded at least 1 image and upto 4 as his interest.每个用户必须上传至少 1 张图片,最多 4 张作为他的兴趣。 Uploading image 1 is compulsory.上传图片 1 是强制性的。 Rest which user didn't upload save as no_image.jpg file.用户未上传的 Rest 保存为 no_image.jpg 文件。

If someone upload 1 image, it repeats image1 2 times.如果有人上传 1 张图片,它会重复 image1 2 次。 it works fine and the way it's showing is ok.它工作正常,它的显示方式还可以。 Problem is if someone upload 2 or 3 image, it repeats some images 2 times.问题是如果有人上传 2 或 3 张图片,它会重复一些图片 2 次。 It's bit odd.有点奇怪。

I don't know much about coding.我对编码了解不多。 Please someone help me to solve this issue and work this perfectly.请有人帮我解决这个问题并完美地工作。 Your help is highly appreciated.非常感谢您的帮助。

Edited: image1 code is not displaying in here properly.已编辑:image1 代码未正确显示在此处。 I don't know issue.我不知道问题。 it is just shows image1.它只是显示image1。 No if loop there.没有 if 循环。

<div class="product" >
<div class="thumbnail"><img src="<?php echo site_url(); ?>assets/images/adsimages/<?php
                                        $a = 'no_image.jpg';
                                        $b = $post['image2'];
                                        $c = $post['image3'];
                                        $d = $post['image4'];

                                        if ($b != $a) {
                                            echo $post['image2'];
                                        } elseif ($c != $a){
                                            echo $post['image3'];
                                        }elseif ($d != $a){
                                            echo $post['image4'];
                                        }else echo $post['image1']
                                        ?>" ></div>
</div>
<div class="product" >
<div class="thumbnail"><img src="<?php echo site_url(); ?>assets/images/adsimages/<?php
                                        $a = 'no_image.jpg';

                                        $c = $post['image3'];
                                        $d = $post['image4'];

                                        if ($c != $a){
                                            echo $post['image3'];
                                        }elseif ($d != $a){
                                            echo $post['image4'];
                                        }else echo $post['image1']
                                        ?>" ></div>
 </div>
 <div class="product" >
 <div class="thumbnail"><img src="<?php echo site_url(); ?>assets/images/adsimages/<?php
                                        $a = 'no_image.jpg';
                                        $d = $post['image4'];
                                            if ($d != $a){
                                            echo $post['image4'];
                                        }else echo $post['image1']
                                        ?>"></div>
                                </div>

You can use foreach to show the date and prevent yourself from repeating the code.您可以使用foreach显示日期并防止自己重复代码。 For your problem, you can take an empty array and fill it on every iteration and check if there is a duplicate value, if not then show the image, if yes, then don't.对于您的问题,您可以获取一个空array并在每次iteration时填充它并检查是否存在重复值,如果没有则显示图像,如果是,则不要。

I've written a possible solution for your code, comments are mentioned in the code itself.我已经为您的代码编写了一个可能的解决方案,代码本身中提到了注释。 See if it helps you.看看对你有没有帮助。

<?php 
    $imageArr = array(); // make an empty array

    foreach($post as $key => $singleImage){ // loop all the images 

        // if( !in_array($singleImage, $imageArr) ){ // check if current value already exists in array ie no_image 
        if( $key != 'id' && $singleImage != 'no_image.jpg' ) ){ // check if key is not id and its value is not no_image.jpg

            // $imageArr[] = $singleImage; // push the current value in array
?>
            <!-- Your view code here -->
            <div class="product" >
                <div class="thumbnail">
                    <img src="<?php echo site_url(); ?>assets/images/adsimages/$singleImage; ?>" /> 
                </div>
            </div>
<?php 
        }
    } 
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM