Google Community
Latest Forums Rules Resources
Custom Search

Go Back   Google Community > Webmaster Forums > Web Design, Coding & Programming Forum

GoogleCommunity Sponsor

Reply
 
LinkBack Thread Tools Display Modes
Old 05-05-2005, 11:44 PM   #1 (permalink)
Noogle
 
Join Date: Apr 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
puccavn
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.
puccavn is offline   Reply With Quote
 
Sponsored Links
Reply

Thread Tools
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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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


All times are GMT -8. The time now is 03:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© 2004–2007 Google Community