ZenphotoCMS Forum
"fixed" getRandomImagesAlbum() - Printable Version

+- ZenphotoCMS Forum (https://forum.zenphoto.org)
+-- Forum: Support (https://forum.zenphoto.org/forum-1.html)
+--- Forum: Themes (https://forum.zenphoto.org/forum-5.html)
+--- Thread: "fixed" getRandomImagesAlbum() (/thread-1664.html)



"fixed" getRandomImagesAlbum() - ulfben - 22-09-2007

I was having problems with the random image header crapping out when a sub-album had only child albums and no pictures of it's own. The original getRandomImagesAlbum() is built on a very long SQL-query which I couldn't quite wrap my head around.

Thus I set out to rebuild and improve upon it, and this is my solution. It'll select 1 random image from the current dir or any of it's subdirs - and is not limited by the depth of the directory tree.

[b]EDIT:[/b]
Sorry. BBPress pissed the code to pieces due to all SQL-backticks. I'll fix it and get it back up soon.

[b]EDIT2:[/b]
Backticks removed from the SQL-queries. Still looks like crap though. Nothing I can do about I'm afraid, sorry. :\

`/*

Returns the ID of all sub-albums, relative to the current album. If $_zp_current_album is not set, it'll return null.*/

function getAllSubAlbumIDs()

{

global $_zp_current_album;

if (!isset($_zp_current_album)) { return null; }

$query = "SELECT id FROM " . prefix('albums') . " WHERE folder LIKE '" . $_zp_current_album->getFolder() . "%'";

$subIDs = query_full_array($query);

return $subIDs;

}

/* Returns an Image-object, randomly selected from current directory or any of it's subdirectories.

returns null if $_zp_current_album isn't set, and if no images can be found. You might want it to fall back on a

placeholder image instead. */

function getRandomImagesAlbum() {

$images;

$subIDs = getAllSubAlbumIDs($rootAlbum);

if($subIDs == null) {return null;}; //no subdirs avaliable

foreach ($subIDs as $ID)

{

    $query = 'SELECT id , albumid , filename , title FROM images WHERE albumid = "'. $ID['id'] .'"';

    $images = array_merge($images, query_full_array($query));

}

if(count($images) < 1){return null;}; //no images avaliable in _any_ subdirectory

$randomImage = $images[array_rand($images)];

$folderPath = query_single_row("SELECT folder FROM " .prefix('albums'). " WHERE id = '" .$randomImage['albumid']. "'"  );

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

return $image;

}

`




"fixed" getRandomImagesAlbum() - aitf311 - 22-09-2007

Good fix, I put this in the latest SVN




"fixed" getRandomImagesAlbum() - ulfben - 22-09-2007

Sorry, I seem to have mushed a prefix while cleaning this up for BBPress.

$query = 'SELECT id , albumid , filename , title FROM ' .prefix('images'). ' WHERE albumid = "'. $ID['id'] .'"';

Is what line 60 should say.




"fixed" getRandomImagesAlbum() - aitf311 - 22-09-2007

sbillard did some improvements to it and it should be working like a champ now in the latest SVN.