This is a discussion on Randomize ImageGalleries within the PHP Language forums, part of the PHP Programming Forums category; Hi there, I have i site with photo-albums on it. to create a new album all i do is ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi there,
I have i site with photo-albums on it. to create a new album all i do is create a new folder. I use the following code. My only question is, how can i randomize the output, so the order of the folders is different every time? ++++++++++++++++++++++++++++++++++++++++++++++++++ ++ function CreateFolders() { $the_array = Array(); $handle = opendir('.'); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.php") { $the_array[] = $file; } } closedir($handle); foreach ($the_array as $PicFolder) { echo 'table with clickable 1st thumb' }; ++++++++++++++++++++++++++++++++++++++++++++++++++ ++ I guess it's easy, but since i'm a newbie, i don't know how to get it to work.. Any help would be greatly appreciated. greetings knoak |
|
|||
|
"knoak" <knoakske@hotmail.com> wrote in message news:a5e41e0e.0405310722.45194061@posting.google.c om... > Hi there, > > I have i site with photo-albums on it. > to create a new album all i do is create a new > folder. I use the following code. > My only question is, how can i randomize the output, > so the order of the folders is different every time? > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++ > > function CreateFolders() > { > $the_array = Array(); > $handle = opendir('.'); > > while (false !== ($file = readdir($handle))) { > if ($file != "." && $file != ".." && $file != "index.php") { > $the_array[] = $file; > } > } > > > closedir($handle); > > foreach ($the_array as $PicFolder) { > echo 'table with clickable 1st thumb' > }; > > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++ > > I guess it's easy, but since i'm a newbie, i don't know > how to get it to work.. > > Any help would be greatly appreciated. > > greetings knoak Try $rand_dir=$the_array[rand() % count(the_array)]; Garp |
|
|||
|
On 31 May 2004 08:22:34 -0700, knoakske@hotmail.com (knoak) wrote:
>I have i site with photo-albums on it. >to create a new album all i do is create a new >folder. I use the following code. >My only question is, how can i randomize the output, >so the order of the folders is different every time? > >+++++++++++++++++++++++++++++++++++++++++++++++++ +++ > >function CreateFolders() >{ > $the_array = Array(); > $handle = opendir('.'); > > while (false !== ($file = readdir($handle))) { > if ($file != "." && $file != ".." && $file != "index.php") { > $the_array[] = $file; > } >} >foreach ($the_array as $PicFolder) { foreach (shuffle($the_array) as $PicFolder) { -- Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space |
|
|||
|
knoak wrote:
> My only question is, how can i randomize the output, > so the order of the folders is different every time? > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++ > > function CreateFolders() > { > $the_array = Array(); > $handle = opendir('.'); > > while (false !== ($file = readdir($handle))) { > if ($file != "." && $file != ".." && $file != "index.php") { > $the_array[] = $file; # try this instead $the_array[] = array(rand(), $file); > } > } > > > closedir($handle); # then reorder the array (randomnly) sort($the_array); > foreach ($the_array as $PicFolder) { # don't forget to use $PicFolder[1] for the $file part # of the array $the_array > echo 'table with clickable 1st thumb' > }; > > > ++++++++++++++++++++++++++++++++++++++++++++++++++ ++ Happy Coding :-) -- USENET would be a better place if everybody read: : mail address : http://www.catb.org/~esr/faqs/smart-questions.html : is valid for : http://www.netmeister.org/news/learn2quote2.html : "text/plain" : http://www.expita.com/nomime.html : to 10K bytes : |
|
|||
|
On 31 May 2004 15:48:44 GMT, Pedro Graca <hexkid@hotpop.com> wrote:
> # then reorder the array (randomnly) > sort($the_array); shuffle? -- Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space |
|
|||
|
Andy Hassall wrote:
> On 31 May 2004 15:48:44 GMT, Pedro Graca <hexkid@hotpop.com> wrote: > >> # then reorder the array (randomnly) >> sort($the_array); > > shuffle? Oops ... missed that one when I read the manual :-) Yes! shuffle() is better/simpler/faster, thanks for pointing it out! -- USENET would be a better place if everybody read: : mail address : http://www.catb.org/~esr/faqs/smart-questions.html : is valid for : http://www.netmeister.org/news/learn2quote2.html : "text/plain" : http://www.expita.com/nomime.html : to 10K bytes : |
|
|||
|
On 2 Jun 2004 13:00:26 -0700, knoakske@hotmail.com (knoak) wrote:
>Hmm. i'm sorry guys, > >But i still can't get it to work... > >Can someone please give me a ready example >how to read a dir, randomize the output, >and then echo the file names or so? Immediately after your closedir($handle); put: shuffle($the_array); -- Andy Hassall <andy@andyh.co.uk> / Space: disk usage analysis tool http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space |