Not yet rated
Tags:

Problem

You want to display an image on a web page which is selected randomly from a collection of images in a directory.

Solution

Use the <cfdirectory> tag to retrieve a list of all the images in a specified directory, then use the RandRange function to select one randomly.

Detailed explanation

<cfset imagepath = "images/gallery/">

<cfdirectory directory="#ExpandPath(imagepath)#"
        filter="*.jpg|*.gif"
        name="qryImages"
        action="list">

<cfoutput>
<img src="#imagepath##qryImages.name[RandRange(1,
qryImages.RecordCount)]#" />
</cfoutput>

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes