Article
Build a Rotating Gallery in ColdFusion
<div align="center">
<table width="70%" border="0" cellpadding="0" cellspacing="0">
<tr>
We start with a table, and open a row.
<cfif start_item LT end_item>
If our end_item value is higher than the start car, we can perform a very straightforward output. If not, we must deal with the overlap from the last record to the first record.
I'm using CFLOOP below for the clarity that the startrow and endrow attributes provide. However this could also be accomplished with CFOUTPUT.
<cfloop query="qGallery" startrow="#start_item#"
endrow="#end_item#">
We set the startrow and endrow attributes to our start_item and end_item values.
<cfset col_tick = IncrementValue(col_tick)>
<cfset count = IncrementValue(count)>
We’ll keep track of the total number of items output with the count variable, when it matches total_items, we are finished. The col_tick variable is used to count the table columns, when col_tick equals columns we need to start a new table row and reset col_tick to zero.
<cfoutput>
We open a cfoutput tag, as we know need to print our variable values to the browser.
<td width="40"><img src="#pic_thumb#" height="#pic_y#"
width="#pic_x#" alt="#pic_title#"></td>
At last, we write our img tag to the browser.
<cfif count EQ total_items>
</tr></table>
<cfelseif col_tick EQ columns>
</tr><tr><cfset col_tick = 0>
</cfif>
Here we compare our count variable to the total_items variable. When they match, we are through and need to close our table. If not true, we check to see if our col_tick variable matches our columns variable. If they match, we need to close our current table row and start a new one.
</cfoutput>
Close our cfoutput tag.
</cfloop>
Close the loop.
<cfelse><!--- if start_item LT end_item --->
If our end_item value is less than the start_item value, we'll need to deal with the overlap from the last record to the first. We can do this by outputting the query results twice. Once to the end of the recordset, and then from the beginning of the recordset to the end_item value.
The code below is identical to the code above, except for the startrow and endrow values in the cfloop tags.
For the first output we set startrow to the start_item value as above, but this time we set the endrow value to match the recordcount from our query. Also, we don’t need the <cfif count EQ total_items> statement, as we know this loop will not complete the gallery.
<cfloop query="qGallery" startrow="#start_item#"
endrow="#qGallery.recordcount#">
<cfset col_tick = IncrementValue(col_tick)>
<cfset count = IncrementValue(count)>
<cfoutput>
<td width="40"><img src="#pic_thumb#" height="#pic_y#"
width="#pic_x#" alt="#pic_title#"></td>
<cfif col_tick EQ columns>
</tr><tr><cfset col_tick = 0>
</cfif>
</cfoutput>
</cfloop>