More than 1 random image

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

More than 1 random image

Post 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.
Last edited by rhosk on Tue Dec 07, 2004 12:36 pm, edited 1 time in total.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

array_rand($list, 5); will return an array since you select more than one random item.
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post by rhosk »

Well, how can I output 5 random image paths from the array? Thanks.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Please read:
http://www.php.net/array_rand

kettle_drum has provided the answer.
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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";
}



?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post 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!
Post Reply