By: Kevin
<cfquery datasource="dsn" name="q_your_query">
    SELECT id,name FROM your_table
</cfquery>

<table>
<cfoutput query="q_your_query">
    <cfif currentrow mod cols EQ 1>
        <!--- NEW ROW AND CELL--->
        <tr><td>
    <cfelse>
        <!--- NEW CELL --->
        <td>
    </cfif>
    DATA FOR THE CELL (#id#,#name#,etc..)
    </td>
    <cfif currentrow mod cols EQ 0>
        <!--- CLOSE ROW IF CURRENT ROW DIVISABLE EVENLY BY COLUMNS NEEDED--->
        </tr>
    <cfelseif currentrow EQ recordcount>
        <!--- FILL IN EXTRA BLANK CELLS FOR PROPER HTML COMPATABILITY ---->
        <!--- FIGURE OUT LOOP COUNT BASED ON RECORDCOUNT AND COLUMNS NEEDED --->
        <cfloop from="1" to="#cols - (recordcount mod cols)#" index="i">
            <td> </td>
        </cfloop>
        <!--- CLOSE ROW --->
        </tr>
    </cfif>
</cfoutput>
<!--- DONE! --->
</table>
About This Tutorial
Author: Kevin
Skill Level: Beginner 
 
 
 
Platforms Tested: CF5,CFMX,CFMX7,BlueDragon
Total Views: 38,085
Submission Date: April 21, 2005
Last Update Date: June 05, 2009
All Tutorials By This Autor: 3
Discuss This Tutorial
  • I left out that part accidently... at the beginning of this code add Where 5 is how many columns wide you want your data to display in.

  • I'm new to CF and used ASP several years ago which output the query into nicely formated columns. So Ineed to learn this code. It would be nice if the code were updated to show corrections such as stating the cols. Again I'm so new I dont know what I could delare for the Col variable??

  • Thanks for catching that. As for the #cols - (recordcount mod cols)#" part, what version CF are you using? This works fine for me in 5 and 6 (MX) Anyways, I will have a table-less version soon. I just haven't had a situation come up where I really needed it, so I haven't done it yet.

  • Just tried out this tutorial and had 2 minor problems. First, your instructions omit that you need to set a variable named "cols" and that is variable should be a number equal to the number of columns you desire. I know, its pretty a pretty obvious thing, but when I was starting out, I needed those obvious things stated. ; ) Second, where you have: this line gave me errors. To make it work I had to make a variable equal to "cols - (recordcount mod cols)" and put the variable name (within pound signs) in the to attribute. Other than those two minor changes it went together very easily. Thanks for posting it!

Advertisement

Sponsored By...
Powered By...