0

This is what i want to display as slider when it slides both the rows move left or right correspondingly

a b c

a b c

<>

but what i have got is given below

a  b  c
  <>

While getting images from database can't get the logic to display it by two rows.How is it possible??

<?php if($sliderimages):?>
<div class="owl-carousel client-slider owl-theme">


<?php foreach($sliderimages as $sliderimage):?>


  <div class="item">
    <div class="product-box">
      <div class="product-img">
    
 <img src="<?php echo base_url('assets/sliders/'.$sliderimage->file_name)?>" class="img-full" alt="" />
       </div>
    </div>
  </div>
<?php else: ?>
<h5 class="text-alter"> sliders are not available</h5>


 <?php endif;?>

</div>

In static condition the html code be like

<div class="owl-carousel client-slider owl-theme">
     
        <div class="item"> 
        <div class="product-box">
            <div class="product-img"><img src="images/logos/Acer.jpg" class="img-full" alt=""/> </div>
            <div class="product-img"><img src="images/logos/Alfa.jpg" class="img-full" alt=""/> </div>
    </div>
          </div> 

I have to display images in carousel in two rows dynamically from database as static condition i have given above.The images fetch from db are to be placed in two rows. How can i get the result as static condition .Can anyone help me to solve this

keerthana
  • 1
  • 3

1 Answers1

0

I have updated my answer. With a simple CSS and HTML hack you don't need to alter the Javascript code, but you need to set some margins in your CSS and at initiation of your owl-carousel. Start by creating a flat array with file names in the sort order you want. This could be done in a Codeigniter Controller method or in the View:

// CREATE A FLAT ARRAY WITH JUST THE FILE NAMES FROM YOUR ORIGINAL ARRAY OF PHP OBJECTS   
foreach($sliderimages as $sliderimage){
  $result[] = $sliderimage->file_name;
}
      
// YOU NOW HAVE A FLAT ARRAY WITH FILE NAMES IN THE SORT ORDER YOU WANT TO DISPLAY THEM
// FOR EXAMPLE: array('1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg');
    
// NOW WE NEED TO ALTERNATE THE SORT ORDER IN THE ARRAY.
// 1: SPLIT THE ARRAY IN THE MIDDLE AND CREATE TWO NEW... 
list($array1, $array2) = array_chunk($result, ceil(count($result) / 2));
    
// 2: EMPTY THE RESULT ARRAY TO MAKE ROOM FOR THE NEW RESULT
$result = array();
    
// COMBINE THE TWO NEW ARRAYS INTO ONE ARRAY WITH AN ALTERNATE SORT ORDER
array_map(function($item1, $item2) use (&$result)
                {
                    $result[] = $item1;
                    $result[] = $item2;             
                }, $array1, $array2);
    
// WE USE FILTER TO REMOVE ARRAY ITEMS WITH EMPTY STRINGS
$result = array_filter($result);

Now we got an array with alternate sort order that is needed to be able to display the images in the sort order that you want with this suggested solution.

Loop through your result array and print the HTML. Here you need to control the odd and even rows to get it right:

// CREATE A COUNTER TO CHECK ODD/EVEN ROWS
$counter = 1;
// NUMBER OF IMAGES TOTAL
$number_of_images = count($result);

// IF WE HAVE IMAGES...
if($number_of_images > 0){
        
    //START OWL CAROUSEL DIV
    echo '<div class="owl-carousel owl-theme">';
        
    foreach($result as $img) {
      if ($counter % 2 == 0){ // EVEN ROW
        echo '  <div class="product-box"><div class="product-img"><img src="'.base_url('assets/sliders/'.$img).'" class="img-full" alt=""/></div></div>';
        // CLOSE ITEM DIV ON ODD ROWS
        echo '</div>';

      } else { // ODD
        //OPEN A NEW DIV ITEM ON ODD ROWS
        echo '<div class="item">';
        // ECHO THE DIV WITH THE IMAGE 
        echo '  <div class="product-box"><div class="product-img"><img src="'.base_url('assets/sliders/'.$img).'" class="img-full" alt=""/></div></div>';
                
        // CHECK TO SEE IF THIS IS THE LAST ROW
        if($counter==$number_of_images){
          // CLOSE ITEM DIV ON LAST ROW
          echo '</div>';
        }
      } // END ODD/EVEN CHECK
      $counter++;
    } // END FOREACH
        
    //END OWL CAROUSEL DIV
    echo '</div>';
        
// NO SLIDER IMAGES AVAILABLE    
} else {
  echo '<h5 class="text-alter"> sliders are not available</h5>';    
}

Now you should have printed HTML looking something like this:

<div class="owl-carousel owl-theme">
<div class="item">
  <div class="product-box"><div class="product-img"><img src="images/logos/1.jpg" class="img-full" alt=""/></div></div>
  <div class="product-box"><div class="product-img"><img src="images/logos/4.jpg" class="img-full" alt=""/></div></div>
</div>
<div class="item">
  <div class="product-box"><div class="product-img"><img src="images/logos/2.jpg" class="img-full" alt=""/></div></div>
  <div class="product-box"><div class="product-img"><img src="images/logos/5.jpg" class="img-full" alt=""/></div></div>
</div>
<div class="item">
  <div class="product-box"><div class="product-img"><img src="images/logos/3.jpg" class="img-full" alt=""/></div></div>
  <div class="product-box"><div class="product-img"><img src="images/logos/6.jpg" class="img-full" alt=""/></div></div>
</div>
</div>

The only thing you then have left to do is to set the margins for the images in CSS:

.product-box { margin-bottom: 10px; }

And set the margin and the number of images to show per row upon initiation of you owl-carousel (change the item property to items:4 if you want to show four images/row):

$('.owl-carousel').owlCarousel({
                     margin:10,
                     items:3
                     });

And the results should become: enter image description here

Codepen example

https://codepen.io/MicKri/pen/vYpjqPy

More reading and examples

Owl carousel multiple rows

https://w3codemasters.in/multiple-rows-carousel-by-owl-carousel/

Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25
  • Hi Thanks for ur response first of all .I need two rows carousel actually its not a problem in static mode how can I change it to dynamic condition. As u mentioned code its result is one row carousel right? . I need to fetch data from db n display it in two rows carousel Can u solve this please i m in stuck for the last 1 week – keerthana Apr 06 '22 at 05:51
  • What do you mean by "two rows"? Have you tried the code in my answer? If you have two rows in your database, with the file names `Acer.jpg` and `Alfa.jpg` then the result should be like in your example with two images. – Michael Krikorev Apr 06 '22 at 15:18
  • I have tried as u given in the above code the result is a single row slider carousel .What I need is a slider which contains multiple images of 2rows n 3cols . Have u understood ? As per ur code we can get with result of single row carousel . Have u seen my output example (i e. a b c) shown in the above . Hope u can understand by seeing the output example. While fetching from db how is it possible? Can u help me ? If u even can't get my issue can u share ur email id I will share my code . I cant share image here since I m a new joiner – keerthana Apr 07 '22 at 06:53
  • Aha ok... think I understand. Then it's nothing to do with the Bootstrap or Codeigniter framework. You need to make some CSS and/or Javascript adjustments for your owl-carousel. Seems your question is a duplicate of this one: https://stackoverflow.com/questions/60429910/owl-carousel-multiple-rows – Michael Krikorev Apr 07 '22 at 23:51
  • Yes u r quite understand my query can u help me to solve this I don't HV idea to get solution – keerthana Apr 08 '22 at 03:32
  • I wanna change it to dynamic code – keerthana Apr 08 '22 at 04:32
  • It would help if you could update the question with also the css and javascript parts of your code. – Michael Krikorev Apr 08 '22 at 20:34
  • I will have an update answer for you in a couple of minutes, so wait until you tested – Michael Krikorev Apr 09 '22 at 09:08
  • First of all i thank for your response thanks a lot michael .Where should i give the script can i give in inline script.i will check and let u know about the result – keerthana Apr 09 '22 at 12:06
  • The PHP code to create and sort the array holding file names I would put in the Controller, and send the $return array to the View. But it also works to put both code blocks straight in your view containing the HTML. – Michael Krikorev Apr 09 '22 at 12:18
  • It worked great michael but one thing have to display four images at 1st row n 4 at the bottom should not repeat the items in carousel can u help me to solve – keerthana Apr 09 '22 at 12:32
  • Michael, since I m using the template styles n js the extra js code put on the carousel doesnot reflect if apply the js code u gave it disappears .could u give a html code for making 4 imgs at d top n bottom respectively. when slide the carousel, images should not repeat. I tried for existing template owl carousel, it doest work – keerthana Apr 09 '22 at 14:34
  • OK, but that is another question. If the solution works, please accept the answer. To use 4 images per row just add the property `items:4` on initiation. I've updated the answer and the codepen with example code: https://codepen.io/MicKri/pen/vYpjqPy – Michael Krikorev Apr 09 '22 at 16:12
  • I accepted ur answer at first. Thank you so much for ur help michael – keerthana Apr 09 '22 at 17:49