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:
- 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 ).
- 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.
My Answer:
'path/to/folder/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$count = 0;
foreach ($files as $file) {
if ($count % 5 == 0) {
echo '
';
}
echo '
';
$count++;
if ($count % 5 == 0) {
echo '
';}
echo '
$count++;
if ($count % 5 == 0) {
echo '
}
}
if ($count % 5 != 0) {
echo '
Rate this post
3 of 5 based on 7812 votesComments