cyb-php Posted December 9, 2020 Share Posted December 9, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/311840-display-pictures-from-a-folder-in-specific-way-php/ Share on other sites More sharing options...
gw1500se Posted December 9, 2020 Share Posted December 9, 2020 Try using <div> or <p> rather than <br />. that having been said, I'd use a table myself. As an aside, syntactically it is preferred to use <br /> rather than just <br>. Quote Link to comment https://forums.phpfreaks.com/topic/311840-display-pictures-from-a-folder-in-specific-way-php/#findComment-1582916 Share on other sites More sharing options...
requinix Posted December 9, 2020 Share Posted December 9, 2020 4 hours ago, gw1500se said: As an aside, syntactically it is preferred to use <br /> rather than just <br>. Not since the death of XHTML. <br> is correct. HTML does not do self-closing tags. 1 Quote Link to comment https://forums.phpfreaks.com/topic/311840-display-pictures-from-a-folder-in-specific-way-php/#findComment-1582917 Share on other sites More sharing options...
cyb-php Posted December 10, 2020 Author Share Posted December 10, 2020 "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 Quote Link to comment https://forums.phpfreaks.com/topic/311840-display-pictures-from-a-folder-in-specific-way-php/#findComment-1582922 Share on other sites More sharing options...
Barand Posted December 10, 2020 Share Posted December 10, 2020 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/311840-display-pictures-from-a-folder-in-specific-way-php/#findComment-1582925 Share on other sites More sharing options...
cyb-php Posted February 9, 2021 Author Share Posted February 9, 2021 (edited) 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 February 9, 2021 by cyb-php Quote Link to comment https://forums.phpfreaks.com/topic/311840-display-pictures-from-a-folder-in-specific-way-php/#findComment-1584334 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.