2

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>';
                }


            ?>
phunksta
  • 33
  • 5
  • 1
    Can you share debugging efforts made to this point? Have you tried a series of var_dumps on key variables? Hard to tell what values you might be getting in some places. – Mike Brant Nov 13 '14 at 15:50
  • I the main problem I'm having seems to be related to having the images in the albums being defined in the ? – phunksta Nov 13 '14 at 16:14
  • I've run a var dump on $photos both with array_reverse and without and it all checks out...I might have to hack into the lightbox.js in order to define the first img in the set or something...live demo available here: http://redlighttuesday.com/rlt.com-flatlandbootstrp/photos.php – phunksta Nov 13 '14 at 16:27

3 Answers3

0

hmmm try reversing scandir

$photos = array_diff(scandir($albumdir,1),array('..', '.', 'thumbs'));
mpalencia
  • 5,481
  • 4
  • 45
  • 59
0

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++;
        }

    }


?>
phunksta
  • 33
  • 5
  • So now i have a new issue where when opening the lightbox gallery it loads img 2 of the set, then if i click the back arrow (in lightbox) it loads img 1 (which is img 2). I will make a new thread... – phunksta Nov 14 '14 at 13:19
0

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!

phunksta
  • 33
  • 5