Re: Create array from directory

From: Date: Mon, 14 Apr 2003 10:08:14 +0000
Subject: Re: Create array from directory
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-143733@lists.php.net to get a copy of this message
Greetings Mr. Wong, and thanks for your input. I have amended things as follows, and all is working to my satisfaction : -------------------------------------------------------- <?php $image_dir = '../graphics/'; $dir = opendir($image_dir); $gallery = array(); while ($file = readdir($dir)) { $filepath = ($image_dir . $file) ; if (is_file($filepath)) { $gallery[] = $filepath; } } closedir($dir); srand ((float)microtime()*1000000); shuffle ($gallery); //==================================================== for ($i = 0; $i < 4; $i++) { print '<td>'; print '<img src="' . ($gallery[$i]) . '" width="100" height="100"></img></td>' . "\n"; } ?> Thanks again for helping........ Chris On Mon, 2003-04-14 at 11:19, Jason Wong wrote: > On Monday 14 April 2003 15:56, Chris Blake wrote: > > > First off, thanks for the assistance...I found a similar example at > > php.net after looking up is_file(), however, my problem is still there. > > > > Running the code below just turns up an empty page : > > ----------------------------------------------------------- > > <?php > > > > $image_dir = '../graphics/'; > > $dir = opendir($image_dir); > > $gallery = array(); > > > > while ($file = readdir($dir)) > > { > > $filepath = $image_dir . $file ; > > if (($file !='.' && $file !='..') && > > is_file($filepath)) > > { > > array_push($gallery, [$file]); > > } > > } > > closedir($dir); > > sort($gallery); > > srand ((float)microtime()*1000000); > > shuffle ($gallery); > > ?> > > <html> > > <head> > > <title>Shuffling Arrays</title> > > </head> > > <body> > > <center> > > <H1>Shuffling Arrays</H1> > > <table width="100%"> > > <tr> > > <?php > > for ($i = 0; $i < 3; $i++) > > { > > print '<td align = "center"><img src = "'; > > print ($filepath); > > print '" width = "100" height = > > "100"></img></td>'; > > } > > > > print $filepath; > > ?> > > </tr> > > </table> > > </center> > > </body> > > </html> > > ----------------------------------------------------------------- > > The intention behind this is to open a dir, place all the images into > > the array and then randomly select 3 images for display on the page. > > > > I`ve tried a few things now but still a blank page ! > > 1) You've stored your filenames in $gallery, use $print_r() to check that they > contain what you expect it to contain. > > 2) No point using sort() then promptly shuffle() it. Just shuffle() it. > > 3) Although you've prepared $gallery you're not using it in your for-loop. > > -- > Jason Wong -> Gremlins Associates -> www.gremlins.biz > Open Source Software Systems Integrators > * Web Design & Hosting * Internet & Intranet Applications Development * > ------------------------------------------ > Search the list archives before you post > http://marc.theaimsgroup.com/?l=php-general > ------------------------------------------ > /* > Once I finally figured out all of life's answers, they changed the questions. > */ > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- Chris Blake Office : (011) 782-0840 Cell : 083 985 0379 Linux vs. Windows is a no WIN situation.

« previous php.general (#143733) next »