-1

I have image slider showing images which user uploaded. Every user has to uploaded at least 1 image and upto 4 as his interest. Uploading image 1 is compulsory. Rest which user didn't upload save as no_image.jpg file.

If someone upload 1 image, it repeats image1 2 times. 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. 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. I don't know issue. it is just shows image1. No if loop there.

<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>
  • Please show what is the value inside `$post` – sauhardnc Jun 19 '20 at 04:36
  • $post array contains all 4 images(image1,image2, image3, image4). image1 contains different images since user has to be upload image1 since it is compulsory. Other 3 images may not have uploaded images because it is optional. Rest is save as no_image.jpg image file. ex: if someone upload first 2 images. They are saved as no_image.jpg file. What I want in here is slider shows only uploaded images.skip no_image.jpg image. I think you got your answer. if you need anything let me know. thanks @sauhardnc for your help. – Steve Daran Jun 19 '20 at 06:11
  • so, if the user uploads 2 images, you'd want only 3 images in the slider, right? 2 the user uploaded and 1 no_image.jpg and ignore duplicate no_image.jpg, right? – sauhardnc Jun 19 '20 at 06:14
  • No. I want to show only that 2 images which are user uploaded in that slider. I don't want to show no_image.jpg. – Steve Daran Jun 19 '20 at 06:18
  • ok, so if you use my code then it shows 3 images in this case, right? – sauhardnc Jun 19 '20 at 06:20
  • Actually It's not working properly. I tried several times. Can you give another method if you can. – Steve Daran Jun 19 '20 at 06:21
  • I think it should show at least 3 images in this case. Can you do a quick `print_r($post);` and show the result? – sauhardnc Jun 19 '20 at 06:23
  • your code isn't give any error. but it's not show images. Array ( [id] => 417 [image1] => 1592386750about_img4.jpg [image2] => no_image.jpg [image3] => 1592386554p8.jpg [image4] => no_image.jpg – Steve Daran Jun 19 '20 at 06:34
  • I've updated the answer, see if it fixes your issue. – sauhardnc Jun 19 '20 at 07:28
  • It didn't work. But I tried this one. – Steve Daran Jun 19 '20 at 08:50
  • Likewise check other image3 and image4. It may not the professional way. But it works fine. – Steve Daran Jun 19 '20 at 08:56
  • Ok, whatever works for you. :) – sauhardnc Jun 19 '20 at 09:06
  • 1
    Thanks for your support. Highly appreciated. :) – Steve Daran Jun 19 '20 at 09:17
  • HI I have another problem. If you have time please help me. Thanks in advance. https://stackoverflow.com/questions/62688415/image-slider-modification – Steve Daran Jul 02 '20 at 06:58

1 Answers1

0

You can use foreach to show the date and prevent yourself from repeating the code. 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.

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 
        }
    } 
?>
sauhardnc
  • 1,961
  • 2
  • 6
  • 16