> -----Original Message-----
> From: Pete Ruckelshaus [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, September 28, 2005 11:11 PM
> To: CF-Talk
> Subject: Every once in a while, I feel smart. A story of displaying a
> single random image.
> 
> I thought I'd share this simple solution that I came up with.

Great idea - nothing like some brain fuel.  ;^)

> 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.
> 
> <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>

Just to be a jerk however...

You don't actually need the loop at all.  Remember that you can access a
query using indexed notation - so the above could be written as:

<cfset randImgRow = randRange(1, getRandImage.recordcount)>
<cfoutput><a
href="/album/default.cfm?albumid=#getrandimage.album_id[randImgRow]#"><img
src="/images/album/#getRandImage.thumbnail[randImgRow]#"
alt="#getRandImage.caption[randImgRow]#"
class="thumbnail"></a></cfoutput>

Of course both solutions do the exact same thing and both access the
query... I wouldn't be surprised to find them both about the same
performance-wise as well.  Mine is really just a little less code.

I cover this in more details in my "Guide to CFML Variables" here:

http://www.depressedpress.com/Content/Development/ColdFusion/Guides/Variable
s/NotationIndexed.cfm

Don't get me wrong tho' - I agree completely about "informational" posts
like this.  I wish more people would throw up snippets when they get
something to work.  ;^)

Jim Davis




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
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:219554
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=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to