Google Community Chat
 
Random image for your gallery

You're visiting Google Community as a guest.
In order to post, you'll need to register and log in.



(If you were registered and logged in, these advertisements wouldn't be here)
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum
   
Author Message
puccavn
Noogle
Noogle


Joined: 12 Apr 2005

1021.15 GC$

Items

PostPosted: Thu May 05, 2005 11:44 pm    Post subject: Random image for your gallery Reply with quote
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.
Back to top
View user's profile Send private message
Sponsored Links
Posted: 5 Dec 2008 10:02 am    Post subject: Advertisements
Back to top
Post new topic   Reply to topic    Google Community Forum Index // Web Design, Coding & Programming Forum All times are GMT - 8 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Host your free forums with Invision Plus.net forum web hosting with your own subdomain.

alexisBlue v1.2 // Theme Created By: Andrew Charron // Icons in Part By: Travis Carden

© 2005-2006 Google Community

Powered by phpBB

Privacy Policy | Contact Us

Powered by Google Search blog

This website is not affiliated in any way with Google, Inc.
Google™ is a registered trademark of Google, Inc.