[thelist] Cold Fusion -- looping records in 3 columns

Morgan Kelsey morgan at morgankelsey.com
Fri Feb 28 21:11:01 CST 2003


gravyface,


> without having to write 3 separate queries, is there anyway to continue
> looping the output of a query to three <td>'s (columns)?
> Lets says there are 9 records returned by the query but I want to output
3
> records in the first <td>, 3 in the second, and the last 3 in the third.
>
> Is there anyway to set a record limit condition in the loop?
>

from my .jeff snippet collection....

   <cfset currentrow = 1>
   <cfset records = YourQuery.recordcount>
   <cfset columns = 2>
   <cfset rows = Ceiling(records / columns)>
   <cfif Ceiling(records / (columns - 1)) EQ rows>
     <cfset columns = columns - 1>
   </cfif>
   <cfif columns GT records>
     <cfset columns = records>
   </cfif>

    <cfoutput>

   <table border="0" width="100%">
   <cfloop from="1" to="#rows#" index="row">
   <cfset currentrow = 1>
   <tr>
     <cfloop from="1" to="#columns#" index="column">
       <cfset currentrow = 1>
       <cfset currentrow = (row - 1) + column>
       <cfif column GT 1>
         <cfset currentrow = (rows * (column - 1)) + row>
       </cfif>
     <td valign="top" width="#Val(100 / columns)#%">
       <cfif column EQ columns AND currentrow GT records>
       &nbsp;
       <cfelse>
       #YourQuery.YourField[currentrow]#
       </cfif>
     </td>
     </cfloop>
   </tr>
   </cfloop>
   </table>
   </cfoutput>


hth,

nagrom
http://morgankelsey.com




More information about the thelist mailing list