Randomize ImageGalleries

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-31-2004
knoak
 
Posts: n/a
Default 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
Reply With Quote
  #2 (permalink)  
Old 05-31-2004
Garp
 
Posts: n/a
Default Re: Randomize ImageGalleries


"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


Reply With Quote
  #3 (permalink)  
Old 05-31-2004
Andy Hassall
 
Posts: n/a
Default Re: Randomize ImageGalleries

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
Reply With Quote
  #4 (permalink)  
Old 05-31-2004
Pedro Graca
 
Posts: n/a
Default Re: Randomize ImageGalleries

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 :
Reply With Quote
  #5 (permalink)  
Old 05-31-2004
Andy Hassall
 
Posts: n/a
Default Re: Randomize ImageGalleries

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
Reply With Quote
  #6 (permalink)  
Old 05-31-2004
Pedro Graca
 
Posts: n/a
Default Re: Randomize ImageGalleries

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 :
Reply With Quote
  #7 (permalink)  
Old 06-02-2004
knoak
 
Posts: n/a
Default Re: Randomize ImageGalleries

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
Reply With Quote
  #8 (permalink)  
Old 06-02-2004
Andy Hassall
 
Posts: n/a
Default Re: Randomize ImageGalleries

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
Reply With Quote
  #9 (permalink)  
Old 06-03-2004
knoak
 
Posts: n/a
Default Re: Randomize ImageGalleries

Well,

It works perfetct!
Thanks a lot.

Greetings
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:32 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0