Member
Member
sbillard   28-07-2007, 23:40
#1

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.

Member
Member
zittoune   30-07-2007, 15:11
#2

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

Member
Member
sbillard   31-07-2007, 01:31
#3

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;

}

`

Member
Member
Photoxite   09-07-2009, 02:21
#4

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?

Member
Member
sbillard   09-07-2009, 03:25
#5

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.

  
Powered By MyBB, © 2002-2026 MyBB Group.
Made with by Curves UI.