Re: Create array from directory
| From: | Jason Wong | Date: | Mon, 14 Apr 2003 09:19:18 +0000 |
| Subject: | Re: Create array from directory | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-143730@lists.php.net to get a copy of this message | ||
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.
*/