random image from within an album

Can anyone give me an SQL query that would get a random image from within the images in an album/subalbum set? I'd like something like the getRandomImage function, but would like to limit the selection to those images within a particular album and its children.

Comments

  • Hello,

    It's a good functionnality that I also wanted, so I tried to work on it and it seems to work !

    I did it on the efferverscence theme.

    I added this function to the customfunctions.php in the effervescence them directory :
    `function getRandomImagesAlbum() {

    $result = query_single_row("(SELECT zenphoto_images.filename, zenphoto_images.title, zenphoto_albums.folder FROM zenphoto_images INNER JOIN zenphoto_albums ON zenphoto_images.albumid = zenphoto_albums.id WHERE zenphoto_albums.id = ".getAlbumId().")UNION(SELECT zenphoto_images.filename, zenphoto_images.title, zenphoto_albums.folder FROM zenphoto_images INNER JOIN zenphoto_albums ON zenphoto_images.albumid = zenphoto_albums.id WHERE zenphoto_albums.parentid = ".getAlbumId().") ORDER BY RAND( ) LIMIT 1");

    $image = new Image(new Album(new Gallery(), $result['folder']), $result['filename']);

    return $image;

    }`

    It uses the function getAlbumId() that I added in the template-functions.php in the zen directory :
    `function getAlbumId() {

    global $_zp_current_album;

    return $_zp_current_album->getAlbumId();

    }`

    And then you just have to add the same code as in the index.php to add the random picture but with `$randomImage = getRandomImagesAlbum();`

    That's it :)
  • Works great, almost. I changed the `zenphoto_images` and `znephoto_albums` references to `".prefix('images')."` and `".prefix("albums")."` so it would work on my site.

    `function getRandomImagesAlbum() {

    $result = query_single_row("(SELECT ".prefix('images').".filename, ".prefix('images').".title, ".prefix('albums').".folder FROM ".prefix('images').

    " INNER JOIN ".prefix('albums')." ON ".prefix('images').".albumid = ".prefix('albums').".id WHERE ".prefix('albums').".id = ".getAlbumId().

    ")UNION(SELECT ".prefix('images').".filename, ".prefix('images').".title, ".prefix('albums').".folder FROM ".prefix('images').

    " INNER JOIN ".prefix('albums')." ON ".prefix('images').".albumid = ".prefix('albums').".id WHERE ".prefix('albums').

    ".parentid = ".getAlbumId().") ORDER BY RAND( ) LIMIT 1");

    $image = new Image(new Album(new Gallery(), $result['folder']), $result['filename']);

    return $image;

    }

    `
  • Non programmer here so my question might be sadly simple.

    I'm not using the efferverscence theme (using zenpage default) and do not have a customfunctions.php to add code to.

    Any suggestion how/where to modify zenpage theme to accomplish the same random image idea?
  • You can always create a customfunctions.php script and load it in the theme. As you have probably guessed, looking at how Efferevescence+ does this would be a good start.
Sign In or Register to comment.