After my post yesterday on selecting or rotating images, a reader asked if it was possible to show a random image once and not again until the other images are shown. That is certainly possible, and here is one way to do it...
<cfapplication name="img" sessionManagement="true">
<!--- Get full path to images. ---> <cfset imageDirectory = expandPath(".")>
<!--- Get directory ---> <cfdirectory action="list" directory="#imageDirectory#" name="images" filter="*.jpg">
<!--- Do we have any images? ---> <cfif images.recordCount gt 0>
<!--- store ID values ---> <cfif not structKeyExists(session, "totalList") or session.totalList is ""> <cfset session.totalList = valueList(images.name)> </cfif>
<!--- pick a random number ---> <cfset pickedIndex = randRange(1, listLen(session.totalList))>
<!--- pick from list ---> <cfset image = listGetAt(session.totalList, pickedIndex)>
<!--- remove from total list ---> <cfset session.totalList = listDeleteAt(session.totalList, pickedIndex)>
<!--- display it ---> <cfoutput><img src="#image#"></cfoutput>
The way I handled it was to simply store the list of filenames in a session variable. This then becomes my list of data to randomly select from. Because the list gets smaller on every hit, I have to check for either the session variable not existing, or if it is empty, and if so, I fill in the values.
Running this version will give you a random order of images, with no image repeated until they have all been shown. There is one exception to this. It is possible that if dharma.jpg, for example, was picked last, that it could then be picked first on reload. As homework, modify the code above to handle that edge case.
p.s. As a reminder, do not forget that you can subscribe to the blog by using the Subscribe form on the right hand side. This will let you get entries via email.