Monday, January 31, 2011

ColdFusion - Breaking output into columns using mod

Say you have a long list which you want to output, but you want to break it up into several columns. The way to do this is to use mod, which gives you the remainder from a division operation. The formula is very simple:
<ul><cfloop query="q_myquery">
<li>[output goes here]</li><cfif not q_myquery.currentrow mod ceiling(q_myquery.recordcount / [number of columns] )>
</ul>
<ul></cfif></cfloop>
</ul>
In this example, ul (unordered list) is used to create columns. You'll also need to set width and float in css, and can get rid of the bullets if they're not needed.

Make sure to put the statement after the main output, not before.

2 comments: