Page 1 of 1

More than 1 random image

Posted: Tue Dec 07, 2004 10:27 am
by rhosk
I've been trying to get this for a few hours and can't seem to do it. Trying to select say 5 random images form a directory for a Gallery. I can sort them all fine, but rand() doesn't seem to work.

Code: Select all

<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder></image_folder>
<time>3</time><fade>2</fade><repeat>true</repeat>
<captions>true</captions></settings><images>';
create_tree("albums/userpics/10010", '/^thumb_/');
function create_tree($file_dir, $filter = '') {
global $xml;
if ($handle = @opendir($file_dir))
{
$list = array();
while (false !== ($file = @readdir($handle))) { 
if ($file != "." && $file != "..") {
if( !empty($filter) && !is_dir($file_dir . '/' . $file) )
if( !preg_match($filter, $file) ) // get only "thumbl_" prefixed images
continue;
$list[] = $file;
}
}
$random = array_rand($list, 5);
foreach($list as $file) {
if( is_dir($file_dir . '/' . $file) ) {
create_tree($file_dir . "/" . $file, $filter);
}
else
$xml .= '<image>' . '<file><![CDATA[' . $file_dir . '/' . $file[$random] . ']]></file>' . '<caption>' . '</caption>' . '</image>' . "\n";
}
closedir($handle);
}
}
$xml .= '</images></slideshow>';
echo $xml;
?>
This particular setup gives me 'h' for file names??? Thanks.

Posted: Tue Dec 07, 2004 10:41 am
by kettle_drum
array_rand($list, 5); will return an array since you select more than one random item.

Posted: Tue Dec 07, 2004 10:52 am
by rhosk
Well, how can I output 5 random image paths from the array? Thanks.

Posted: Tue Dec 07, 2004 11:25 am
by []InTeR[]
Please read:
http://www.php.net/array_rand

kettle_drum has provided the answer.

Posted: Tue Dec 07, 2004 12:17 pm
by rhosk
[]InTeR[] wrote:Please read:
http://www.php.net/array_rand

kettle_drum has provided the answer.
Well, some are helpful here, and some just seem to refer to php.net. How 'bout I've been there, didn't understand it totally, and came here for help.

Thanks anyway.

Posted: Tue Dec 07, 2004 12:26 pm
by rehfeld

Code: Select all

<?php

$list = array('foo.gif', 'bar.gif', 'baz.gif');

$random = array_rand($list, 2);
// $random now has 2 elements of the $list array


foreach ($random as $file) {
    echo "xml.....$file";
}



?>

Posted: Tue Dec 07, 2004 12:29 pm
by kettle_drum
It returns an array and you try to use it as a string or int value as a key for an array. It therefore doesnt work as its not returning what you expect it to. As said before it returns an array and so must be treated as one - im sure you know how to do this as you do it already in your code:

Code: Select all

foreach($random as $x){
   echo $file[$x];
}

//or

echo $file[0];
echo $file[1];
...
...
We are trying to help you learn by delibrately not telling you the full answer and making you search a little more for it yourself.

Posted: Tue Dec 07, 2004 12:35 pm
by rhosk
Appreciate it rehfeld, I tried that and it didn't work for some reason with $list being the array. Output numbers. I think I've just been at it too long today, sorry for the rant.

I resorted to shuffle(). Works just as good, thanks!