Вопрос

I have following code:

    $path=$_SERVER['DOCUMENT_ROOT'] . "/WOWSlider/data1/images";

    $files= array_diff(scandir($path), array('..',".")); 

    $path2=$_SERVER['DOCUMENT_ROOT'] . "/WOWSlider/data1/images/thumbs";

    $files2= array_diff(scandir($path2), array('..',"."));

foreach($files as $plaatje){

    print("

    <a href='WOWSlider/data1/images/$plaatje' class='highslide' onclick='return hs.expand(this)'>
        <img src='WOWSlider/data1/images/thumbs/$thumbnail' alt='Highslide JS'
            title='Click to enlarge' /></a>

    <div class='highslide-caption'>
        $plaatje
    </div>

    ");


}

Now I need something like this:

        $path=$_SERVER['DOCUMENT_ROOT'] . "/WOWSlider/data1/images";

        $files= array_diff(scandir($path), array('..',".")); 

        $path2=$_SERVER['DOCUMENT_ROOT'] . "/WOWSlider/data1/images/thumbs";

        $files2= array_diff(scandir($path2), array('..',"."));

    foreach($files as $plaatje){
foreach($files2 as $thumbnail){

        print("

        <a href='WOWSlider/data1/images/$plaatje' class='highslide' onclick='return hs.expand(this)'>
            <img src='WOWSlider/data1/images/thumbs/$thumbnail' alt='Highslide JS'
                title='Click to enlarge' /></a>

        <div class='highslide-caption'>
            $plaatje
        </div>

        ");


    }
}

But it does not work. It doesn't print anything on the screen.

Does somebody know what to do ?

I am trying to make a sort of photo album. The $plaatjes are photo's and the $thumbnails are thumbnails of the photo's.

And because the thumbnails have a different filename I need to have a different variable in the foreach and therefore also in the print().

I dont't want to make the filenames different because if I need to add other photo's I don't have to do something in the code.

Это было полезно?

Решение

Unless the filenames are related to each other, e.g.

kittens.jpg   -> kittens_thumb.jpg

where the extra stuff (_thumb) is constant, there is nothing you can do in an automated fashion like this. You'd have to manually build a list that relates the images to their thumbnails. Once you've got that array, THEN you can use a simple foreach().

With the "related" filenames, you could do something like:

foreach($files as $file) {
   $base = pathinfo($file, PATHINFO_BASENAME);
   $ext = pathinfo($file, PATHINFO_EXTENSION);
   echo "real file: $base.$ext";
   echo "thumb nail: {$base}_thumb.$ext";
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top