Issues getting images from a folder and using a path for href and image source

93 views Asked by At

I'm new to PHP and having some troubles.
I need to get some images to display as:

<a rel="example_group" href="slidesstartpagina/DESOMVIELE-Bloemen-01.jpg"     title="Desomviele bloemen en decoraties"><img src="slidesstartpagina/DESOMVIELE-Bloemen-01-tn.jpg" alt="" /></a>

The PHP code I wrote does display the first path perfectly for the <a href>, but for the <img src> it files with Array instead of using the filename.
I must be doing something wrong using of that array.

Any Idea?

My code:

<?php
$paththumb = "img/bloemen_thumb/";
$pathimg = "img/bloemen_img/";
$images = preg_grep('/^([^.])/', scandir($pathimg));
$thumbs = preg_grep('/^([^.])/', scandir($paththumb));
$i=1;
foreach ($thumbs as $block) {
if ($i%16 == 1)
{  
echo '<div class="block2 gallery">';
}
if ($thumbs[0] == '.');
foreach ($images as $value) {echo '<a rel="example_group"        href="img/bloemen_img/'.$value.'" title="Desomviele bloemen en decoraties"><img src="img/bloemen_thumb/'.$thumbs.'" alt="" /></a>';
};
if ($i%4 == 0)
{
    echo '</div>';
};
 $i++;
if ($i%16 != 1) echo '</div>';
}
?>
1

There are 1 answers

6
Mateusz Petkiewicz On BEST ANSWER

You must put those div's inside "foreach" loop. If filenames are the same for thimbs and images you can use one loop:

$paththumb = 'img/bloemen_thumb/';
$pathimg = 'img/bloemen_img/';
$thumbs = preg_grep('/^([^.])/', scandir($paththumb));
$i=1;

echo '<div class="block2 gallery">';
foreach ($images as $key => $value) {

    echo '<a rel="example_group" href="img/bloemen_img/'.$images[$key].'" title="Desomviele bloemen en decoraties"><img src="img/bloemen_thumb/'.$thumbs[$key].'" alt="" /></a>';

    if($i%16==0){echo '</div><div class="block2 gallery">';}
    $i++;   
}
echo '</div>';

Would be also great to check if file exists...