PHP & Lightbox (href in for loop links to last file of lightbox set)

473 Views Asked by At

EDITED: Debugged some of the code but issue persists:

The issue i'm having with the following code is that the link always takes me to the last image in the set. I've tried reversing the array of photo's but it had no effect. Any help would be appreciated.

Thanks.

            <?php
                $dir = 'pic';
                $max_albums=9;
                $albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));

                foreach ($albums as $album) {
                    $albumdir = $dir.'/'.$album;
                    $coverdir = $albumdir.'/thumbs';

                    $thumbs = array_diff(scandir($coverdir),array('..', '.'));

                    //re-index $thumbs
                    $thumbs = array_values($thumbs);

                    //1 random cover image from each album
                    $rnd_min = 0;
                    $rnd_max = count($thumbs)-1;
                    $rnd_i = mt_rand($rnd_min, $rnd_max);
                    $covers = $thumbs[$rnd_i];
                    //re-index $covers
                    echo $rnd_i.'<br>';

                    //populate hrefs
                    $photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));

                    //re-index $photos
                    $photos = array_values($photos);

                    foreach ($photos as $photo) {
                        echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">';
                    }
                    //display cover images
                    echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
                }


            ?>
3

There are 3 best solutions below

1
On

hmmm try reversing scandir

$photos = array_diff(scandir($albumdir,1),array('..', '.', 'thumbs'));
1
On

Not a very elegant solution but it seems to work:

<?php
    $dir = 'pic';
    $max_albums=9;
    $albums = array_diff(scandir($dir),array('..', '.', 'thumbs'));

    foreach ($albums as $album) {
        $albumdir = $dir.'/'.$album;
        $coverdir = $albumdir.'/thumbs';

        $thumbs = array_diff(scandir($coverdir),array('..', '.'));

        //re-index $thumbs
        $thumbs = array_values($thumbs);

        //1 random cover image from each album
        $rnd_min = 0;
        $rnd_max = count($thumbs)-1;
        $rnd_i = mt_rand($rnd_min, $rnd_max);
        $covers = $thumbs[$rnd_i];

        //populate hrefs
        $photos = array_diff(scandir($albumdir),array('..', '.', 'thumbs'));

        //re-index $photos
        $photos = array_values($photos);

        $countphoto = 0;

        foreach ($photos as $photo) {
            if ($countphoto==0) {
                echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'">'."\n";
                //display cover images
                echo '<img src="'.$coverdir.'/'.$covers.'" class="img-responsive"></a>';
            }
            else {
                echo '<a href="'.$albumdir.'/'.$photo.'" data-lightbox="'.$album.'"></a>'."\n";
            }
            $countphoto++;
        }

    }


?>
0
On

So i solved this issue with a simple if $countphotos==0 statement within the foreach ($photos as $photo) loop, if 0, it would display the thumbnail, else it would just output anchors to the other imgs.

I've also done away with the random thumbnail as cover, and just pulled the first thumbnail of the set.

I now another issue, but I will make a separate thread for that.

Thanks!