I thought I'd share this simple solution that I came up with (though
I'm sure someone else has done it before....just as I'm sure there are
better ways of doing this).

I'm building a site that has a photo album as part of it.  The image
data (caption, description, filename, etc.) is stored in a SQL Server
database and the image is on the server file system.

On the front page I wanted to display a single random image.  The
problem is, how to grab a truly random image?

What I did was first run a query that grabbed all of the active images
in the database:

<cfquery name="getRandImage" datasource="#request.app.dsname#">
        SELECT          P.caption, P.album_id, P.thumbnail
        FROM            tblPhotos P
        WHERE           P.active = 1
</cfquery>

Next, by using randRange, the number 1, and the recordcount of the
above query, I generate a random number:

<cfset randImgRow = randRange(1, getRandImage.recordcount)>

Finally, and here's where the dead-simple beauty of all this comes in,
display the random image using cfloop's startrow and endrow using the
randImgRow value:

<cfoutput><cfloop query="getRandImage" startrow="#randImgRow#"
endrow="#randImgRow#"><a
href="/album/default.cfm?albumid=#getrandimage.album_id#"><img
src="/images/album/#getRandImage.thumbnail#"
alt="#getRandImage.caption#"
class="thumbnail"></a></cfloop></cfoutput>

That's it.  There's a tendancy for people to bring problems seeking
answers to this list, I thought just once I'd share a solution waiting
for a problem.

Pete

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support 
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:219552
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to