|
||||||||||
|
|||||||
| GoogleCommunity Sponsor |
|
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Noogle
![]() Join Date: Apr 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
Random image for your gallery
If you want have a gallery with random images !!!
Well, here is the way you can do it: <?php $imgpath = "path/to/image/dir"; $handle = opendir( "$imgpath" ); $imgArray = array(); while($file = readdir($handle)) { if( $file != "." && $file != ".." ) { array_push( $imgArray, $file ); } } closedir( $handle ); mt_srand( (double)microtime( ) * 1000000 ); $randval = mt_rand( 0, sizeof( $imgArray ) - 1 ); print( "<IMG SRC=\"$imgpath/" . $imgArray[ $randval ] . "\">" ); ?> Is it complicated ? Then let's comment the code. $handle = opendir( "path/to/image/dir" ); $imgArray = array(); Opens the folder where the images are stored and then empties the array of images. while($file = readdir($handle )) { if($file != "." && $file != "..") { array_push( $imgArray, $file ); } } closedir( $handle ); The code above starts a loop, reads folder contents and checks if the file is not a folder. Then a name of the file is stored in the array and folder is closed when the cycle is over. $randval = mt_rand( 0, sizeof( $imgArray ) - 1 ); This line generates a random value not greater than number of files in the directory. print( "<IMG SRC=\"img/" . $imgArray[ $randval ] . "\">" ); Last step is to print image tag code to the page. |
|
|
|
|
Sponsored Links
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| viewing gallery of images in gmail | paridoth | Gmail Forum | 6 | 10-22-2007 05:28 PM |
| ::: ShahRukh Khan Photo Gallery by Naveedzonline ::: | naveedzonline | Chit Chat | 0 | 01-20-2006 07:10 PM |
| Getsome's Gallery | Pixel | Graphics Design Forum | 2 | 10-02-2005 12:01 PM |
| ME!, dont u just love random? | mikeskuse999 | Introduce Yourself | 1 | 07-20-2005 08:47 AM |
| Random php script | kaz | Web Design, Coding & Programming Forum | 6 | 10-13-2004 12:42 AM |