|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
This little program reads a directory of files, and prints out a
random thumbnail from that directory. I cleaned it to shorten as much as possible. I have two questions: 1) How can I make it shorter and remain readable. (I know there must be faster methods and shorter code). 2) I want to require this file at the beginning of a page, get my directory array just once, let some html pass and then run the function later a few times. When I tried that, I could not get the $filesread array to carry over to the function (a scope issue i guess). I just don't want to repeat reading the directory files into the array every time I call the function. For reference this file the working file is at: http://styletattoo.com/new.php. Hopefully the code will post verbatim/ ----------------------------------------------------------------------------------------------- <? function RandomStencil() { $path = $_SERVER['DOCUMENT_ROOT']; $fullpath = $path . "/stencil/thumb/"; //-----open the directory $d=$fullpath; #define which dir you want to read $dir = opendir($d); #open directory //-----read all files into an array while removing the 'th.gif' from the end of each filename while ($f = readdir($dir)) {$filesread[] = str_replace("th.gif", "", $f);} //-----sort and shift off the two . and .. files sort($filesread,SORT_NUMERIC); $trash=array_shift($filesread); $trash=array_shift($filesread); //-----choose a random stencil and print it with link to big stencil page $countera = count($filesread); srand ((double) microtime() * 1000000); $randomstencil = rand(0,countera-1); $randstenc = $filesread[$randomstencil]; print ("<a href='stencil/stencil.php?image=" . $randstenc . "' title=''><img class='navlastimg' border='0' src='stencil/thumb/" . $randstenc . "th.gif' alt='' /></a>"); } ?> ----------------------------------------------------------------------------------------------- |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Feb 25, 3:50pm, howdy <i...@mauicomputer.com> wrote:
> This little program reads a directory of files, and prints out a > random thumbnail from that directory. > > I cleaned it to shorten as much as possible. > > I have two questions: > 1) How can I make it shorter and remain readable. (I know there must > be faster methods and shorter code). > > 2) I want to require this file at the beginning of a page, get my > directory array just once, let some html pass and then run the > function later a few times. When I tried that, I could not get the > $filesread array to carry over to the function (a scope issue i > guess). > I just don't want to repeat reading the directory files into the array > every time I call the function. > > For reference this file the working file is at:http://styletattoo.com/new.php. > Hopefully the code will post verbatim/ > ----------------------------------------------------------------------------------------------- > <? > function RandomStencil() > { > $path = $_SERVER['DOCUMENT_ROOT']; > $fullpath = $path . "/stencil/thumb/"; > > //-----open the directory > $d=$fullpath; #define which dir you want to read > $dir = opendir($d); #open directory > > //-----read all files into an array while removing the 'th.gif' from > the end of each filename > while ($f = readdir($dir)) > {$filesread[] = str_replace("th.gif", "", $f);} > > //-----sort and shift off the two . and .. files > sort($filesread,SORT_NUMERIC); > $trash=array_shift($filesread); > $trash=array_shift($filesread); > > //-----choose a random stencil and print it with link to big stencil > page > $countera = count($filesread); > srand ((double) microtime() * 1000000); > $randomstencil = rand(0,countera-1); > $randstenc = $filesread[$randomstencil]; > print ("<a href='stencil/stencil.php?image=" . $randstenc . "' > title=''><img class='navlastimg' border='0' src='stencil/thumb/" . > $randstenc . "th.gif' alt='' /></a>"); > } > ?> > ----------------------------------------------------------------------------------------------- The simple one that I see, is to take $path = $_SERVER['DOCUMENT_ROOT']; $fullpath = $path . "/stencil/thumb/"; //-----open the directory $d=$fullpath; #define which dir you want to read $dir = opendir($d); #open directory And change that to: $dir = opendir( $_SERVER['DOCUMENT_ROOT'] . "/stencil/thumb" ); And move the while loop for reading the directory to be one line: while ($f = readdir($dir)) $filesread[] = str_replace("th.gif", "", $f); Also, it looks like, later in your print statement, you're just stuffing that th.gif right back on. The following is only if you expect there to be no missing numbers: while( $f = readdir( $dir ) ) $filesread[] = $f; You also don't need to store those things that you're popping off the top of the array, sort($filesread,SORT_NUMERIC); array_shift($filesread); array_shift($filesread); No need to waste memory there, or with the counter for that matter. $countera = count($filesread); srand ((double) microtime() * 1000000); $randomstencil = rand(0,countera-1); Becomes srand ((double) microtime() * 1000000); $randomstencil = rand( 0, count($filesread) - 1 ); So you're final code would look something like: <? function RandomStencil() { //-----open the directory $dir = opendir( $_SERVER['DOCUMENT_ROOT'] . "/stencil/ thumb/" ); #open directory //-----read all files into an array while removing the 'th.gif' from the end of each filename while ($f = readdir($dir)) $filesread[] = $f; //-----sort and shift off the two . and .. files sort($filesread,SORT_NUMERIC); array_shift($filesread); array_shift($filesread); //-----choose a random stencil and print it with link to big stencil page srand ((double) microtime() * 1000000); $randomstencil = rand(0, count($filesread) - 1); $randstenc = $filesread[$randomstencil]; print ("<a href='stencil/stencil.php?image=" . $randstencil . "' title=''><img class='navlastimg' border='0' src='stencil/thumb/" .. $randstenc . "th.gif' alt='' /></a>"); } ?> ---------------------------------------- And I probably missed more here (or made something unreadable for someone as well) so I wouldn't be surprised if you got other people saying different things. |
|
![]() |
| Outils de la discussion | |
|
|