468,161 Members | 2,090 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

Randomize ImageGalleries

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
Jul 17 '05 #1
8 1634

"knoak" <kn******@hotmail.com> wrote in message
news:a5**************************@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
Jul 17 '05 #2
On 31 May 2004 08:22:34 -0700, kn******@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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #3
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 :
Jul 17 '05 #4
On 31 May 2004 15:48:44 GMT, Pedro Graca <he****@hotpop.com> wrote:
# then reorder the array (randomnly)
sort($the_array);


shuffle?

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #5
Andy Hassall wrote:
On 31 May 2004 15:48:44 GMT, Pedro Graca <he****@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 :
Jul 17 '05 #6
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?

Thanks for the reply!!

Greetings
Jul 17 '05 #7
On 2 Jun 2004 13:00:26 -0700, kn******@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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #8
Well,

It works perfetct!
Thanks a lot.

Greetings
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Fieldmedic | last post: by
3 posts views Thread by Gaffer | last post: by
1 post views Thread by Ellen Manning | last post: by
4 posts views Thread by Mr. x | last post: by
1 post views Thread by Badass Scotsman | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.