Display Random images in 3 lines of code!

Ever wanted to run random images on your site, but didn't want tons of code?
Here you go, every time the user refreshes the image is changed.

Put the contents below in a randomimg.cfm file and change the variables in red.

Then Call the image as in Include: <CFINCLUDE Template="randomimg.cfm">

<!--- Code Begins --->
<CFSET serverdir =
"C:\INETPUB\WWWROOT\IMAGES\RANDOM\">
<CFSET httpdir =
"http://www.yourserver.com/images/random">
<CFSET filter =
"*.jpg OR *.gif"> (Note: Only one single extension allowed)

<CFDIRECTORY
     ACTION=
"list"
     DIRECTORY=
"#serverdir#"
     NAME=
"images"
     FILTER=
"#filter#">
<CFSET index = RandRange(1, images.RecordCount)>
<cfoutput>
   
<img src="#httpdir#/#images.name[index]#">
</cfoutput>
<!--- Code Ends --->
 
About This Tutorial
Author: DesertTRD
Skill Level: Beginner 
 
 
 
Platforms Tested: CF4,CF5
Total Views: 28,679
Submission Date: August 15, 2003
Last Update Date: June 05, 2009
All Tutorials By This Autor: 1
Discuss This Tutorial
  • I am wanting to put together a quiz like application with a lot of questions included. There would be 6 sections with each having around 150 questions each. I was going to put the questions in a database so others can add to it if desired by a form I made to insert the question and answers into a databse. But with each quiz, I was only wanting around 35-40 random questions from the specific section they are working on from the database. What would I set instead of the httpdir and serverdir to extract from the database?? The other coding part I know I just need to know the settings part of it so that it can pull random questions out. Thanks!!

  • There are no variables in red.

  • Very clean and easy to setup and use! Is there a way to do a random swf (flash format) with this?

Advertisement

Sponsored By...
Powered By...