Jump to content

Display pictures from a folder in specific way -php


cyb-php

Recommended Posts

Hi

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.

Link to comment
Share on other sites

"gw1500se"  and "requinix" thanks for reply, but the problem I am trying to solve is in PHP and not in HTML. The code is simplified there, I don't use <br> or <br /> at all on my pages. Obviously there will be a <div class="whatever"> around there. Thanks

Link to comment
Share on other sites

Something like this? ...

    $rows = array_chunk(array_slice($files, 0, 9), 5);                // get first 9 files, in chunks of 5 and 4
    foreach ($rows as $row) {
        echo "<div class='row'>\n";
            foreach ($row as $img) {
                $w = 100 / count($row);
                echo "<div class='image' style='width: $w%; '>
                          <img src='$img' width='120px' >
                      </div>
                     ";
            }
        echo "</div>\n";
    }    

image.png.d1b5af5757e565d49e2f72dfde39df6b.png

Link to comment
Share on other sites

  • 1 month later...

Thanks "Barand", I solved the problem and it is almost like yours:

$files = glob("layout/gallery/1thumbs/200x200/*.webp");
            rsort($files, SORT_NATURAL);
            $rows=[];
               while(count($files) != 0) {
                $splice = count($rows) % 2 == 0 ? 5 : 4; // This part resolves my problem!
                $rows[] = array_splice($files, 0, $splice);
              }
            foreach($rows as $row) {
              echo '<div class="stage stage_one">'."\n";
                foreach($row as $file) {
                  $fnames = pathinfo($file, PATHINFO_FILENAME); 
                  echo '<a data-fancybox="images" data-caption="'.$fnames.'" href="layout/gallery/'.$fnames.'/'.$fnames.'.webp" alt=""><img src="'.$file.'"></a>'."\n";
                }
              echo '</div>'."\n";
            }

 

Edited by cyb-php
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.