1

I am trying to create a dynamic gallery in php with specific order of pictures on the page, but I can't find the function or piece of php code to do so.

Conditions:

  1. The gallery must be dynamic, pictures will be coming from a folder via php, because when I'll add/upload more pictures in the folder they must be displayed on the page without adding manually the html tags in. ( this part is easy, problem is the condition 2 ).
  2. The first row will have 5 pictures, the second - 4 pictures (important for the specific effect).

My code:

    $files = glob("layout/gallery/*.jpg");

      rsort($files, SORT_NATURAL);

      for ($i=0; $i < count($files); $i++) {

        for( ; $i<5; $i++){
        $one = $files[$i];
        echo '<img src="'.$one.'">' . '<br><br>';
        }

echo '<br>';

        for( ; $i<9; $i++){
        $two = $files[$i];
        echo '<img src="'.$two.'">' . '<br><br>';
        }

      }

The code works well, but it just displays 9 pictures obviously. I was unable to make it dynamic displaying 5 pictures first, 4 pictures after and stay this way in a loop till displays all pictures from that folder.

cykvi
  • 23
  • 3
  • Do you want to keep the pattern like this, first row 5 photos other rows 4 photos? – Gazmend Sahiti Dec 09 '20 at 16:42
  • Hi. No, I try to have 5 photos on the first row, 4 photos on the second row and then repeat it again and again like: 5 - 4 - 5 - 4 - 5 - 4.. so on – cykvi Dec 09 '20 at 20:04

1 Answers1

0

You can take advantage of the array_splice function that removes elements from the array everytime it returns those elements :

$files = glob("layout/gallery/*.jpg");

rsort($files, SORT_NATURAL);

// split the files in rows
$rows = [];
while(count($files) != 0) {
    // even rows have 5 elements, odd ones have 4
    $num_files_to_splice = count($rows) % 2 == 0 ? 5 : 4;
    $rows[] = array_splice($files, 0, $num_files_to_splice);
}

// display them accordingly
foreach($rows as $row) {
    foreach($row as $file) {
        echo '<img src="'.$file.'">';
    }
    echo '<br><br>';
}
challet
  • 870
  • 9
  • 21
  • Thanks challet, your code is closer to my goal as it displays: 5 - 4 - 4 - 4... so on per row. I think I need to tweak around "0 ? 5 : 4" part of the code and find out more about "array_splice" on php[dot]net. Thanks for help and I'll post the code here when sort it out =D – cykvi Dec 09 '20 at 19:31
  • @cykvi Now I have understood that you want to continue to alternate on the next rows, I have changed the code in order to do it. A related question about testing for odd or even numbers can also be found here https://stackoverflow.com/questions/7959247/test-if-number-is-odd-or-even – challet Dec 10 '20 at 13:40
  • YES, I love you. that's it "% 2 == 0 ? 5 : 4". I am on this from yesterday, I researched about "array_splice", "array_slice", "array_switch", array_whatevet and did not find what I needed. Now I was looking at "PHP Array Operators" and trying to tweak that part of code and I said above. I opened this page after and found your solution. Thanks you very much !!! – cykvi Dec 10 '20 at 16:56