Dynamic Column Output (Part One)

I spend a good amount of time in various help forums (be it here or on DALnet?s #coldfusion)?and there?s one question that seems to come up time after time:

?CJ?how to you get your hair so shiny and bouncy??

Another question I hear a lot is:

?How can I have my query output display n columns per row??

I?d like to take a few moments to reflect on the second question. If there?s any time left afterwards, feel free to stick around and we?ll discuss my strict hair regimen.

To see exactly what I mean by outputting n columns per row, see this page:

http://charlie.griefer.com/4cols.cfm ?

which will demonstrate the concept by allowing you to determine the number of columns to display, and rendering the content dynamically.

The key to being able to display a specific number of columns per row within an output is the MOD operator. If you?re new to programming (and many CF?ers are? CF was my first programming language when I began with it), you may not be familiar with this operator.

MOD is short for MODULUS, and is simply the value of the remainder after one number is divided by another.

Recall from 3rd grade math that in division, there is a dividend, a divisor, a quotient, and a remainder:

    Dividend / Divisor = Quotient rRemainder 
    Example: 20 / 6 = 3 r2

In the example above, the dividend is 20, the divisor is 6, the quotient is 3 (6 goes into 20 three times), and the remainder is 2. This is our MOD value.

A few more examples:

Value A (dividend)

 

Value B (divisor)

 

Quotient

Mod

50

/

4

=

12

2

16

/

3

=

5

1

100

/

10

=

10

0

The MOD operator is commonly used to test whether or not a value is odd or even. Since any even number is divisible by 2 with no remainder, the following holds true:

<cfif A MOD 2 EQ 0>
    The number is even!
<cfelse>
    The number is odd!
</cfif>

Hopefully you feel that you now have a good grasp of the MOD operator. Time to move on to the next concept.
The next thing you need to be aware of has to do with the
<cfquery> object. We?re all aware of the fact that <cfquery> returns database values from a database table (we?re assuming a SELECT query). It also returns 4 additional variables:

recordCount ? the number of rows returned in the query
columnList ? a comma delimited list of the columns returned in the query
executionTime ? the amount of time it took to execute the query
currentRow ? during output, indicates the row # that is being displayed.

It?s the last one (currentRow) that we?re interested in. Here?s a short example of how currentRow works:

<cfquery name=?getFoo? datasource=?foo?>
    SELECT foo
    FROM bar
</cfquery>

<table border=?1?>
   
<cfoutput query=?getFoo?>
    <tr>
        <td>
#getFoo.currentRow#</td>
    </tr>

   
</cfoutput>
</table>

Assuming the getFoo query returned 20 rows, our output would show the numbers 1 through 20, each in it?s own table cell in its own row. Each iteration of the query driven
<cfoutput> starts a new table row (note that the <tr></tr> tags are within the <cfoutput></cfoutput> block).

Putting the currentRow variable together with the MOD operator, we can now get to work on displaying our output neatly in a specific number of columns.

Using our same query as above, we?re going to make 2 modifications. The first is:

<cfquery name=?getFoo? datasource=?foo?>
    SELECT foo
    FROM bar
</cfquery>

<table border=?1?>
    <tr>

    <cfoutput query=?getFoo?
        <td>#getFoo.currentRow#</td>
    </cfoutput>
    </tr>
</table>


What?s different? We placed our query-driven
<cfoutput> tags inside of <tr></tr> blocks, as opposed to outside.

Now, instead of 20 table rows, we?re going to get 20 table cells (
<td>s) all in one row.

The final step is going to be how to tell ColdFusion to end the current row and start a new row after 4 <td>s.

This is where we get some mileage out of the MOD operator.

<cfquery name=?getFoo? datasource=?foo?>
    SELECT foo
    FROM bar
</cfquery>

<cfset variables.newrow = false>

<table border=?1?>
    <tr>

   
<cfoutput query=?getFoo?>
    <cfif variables.newrow = true>
        <tr> 
    </cfif>

       
<td>#getFoo.currentRow#</td>
       
<cfif getFoo.currentRow MOD 4 EQ 0>
               
</tr>
                <cfset variables.newrow = true>
        <cfelse>
                <cfset variables.newrow = false>
       
</cfif>
   
</cfoutput>
   
</tr>
</table>

<cfif getFoo.currentRow MOD 4 EQ 0>. That?s the key.

We?re telling ColdFusion that as it loops over the query output, it needs to check to see if the current record being output is evenly divisible by 4.  If it is, we insert a </tr> (since we only want 4 columns per row).  We also ?flip a switch??by setting the local variable newrow to be true.  The next iteration of the output needs to be aware of the fact that a <tr> was just closed, so it knows to open a new <tr>.  The newrow variable handles that.  Originally, we set it to false (before our table).  It will resolve to false for every iteration of the output that is *not* divisible by 4.

Check out the page at http://charlie.griefer.com/4cols.cfm, and as always, contact me with any questions or comments :)

All ColdFusion Tutorials By Author: Charlie Griefer (CJ)
  • CFSCRIPT Intro
    An introductory look at CFSCRIPT. Rules, some basic syntax, and a couple of examples of loops and conditional processing.
    Author: Charlie Griefer (CJ)
    Views: 46,873
    Posted Date: Saturday, January 18, 2003
  • ColdFusion Mad Libs - Part I
    A silly but fun time-waster that you can easily include on your Web site. You might be surprised at how addicting it can become :)
    Author: Charlie Griefer (CJ)
    Views: 27,666
    Posted Date: Thursday, May 29, 2003
  • ColdFusion Mad Libs - Part II
    You've finished the first Mad Libs tutorial, but you feel like there's something missing. Of course there is! You want to be able to save the final output to a database to let your visitors browse through other user's stories. Includes a bad-words filter for the more conservative among us :)
    Author: Charlie Griefer (CJ)
    Views: 22,838
    Posted Date: Thursday, May 29, 2003
  • to cfqueryparam or not to cfqueryparam
    It's been out there since ColdFusion 4.5...most of us have heard of it...few of us use it. Here are some compelling reasons why you should get into the habit of using the tag.
    Author: Charlie Griefer (CJ)
    Views: 34,135
    Posted Date: Thursday, May 29, 2003
  • Dynamic Column Output (Part One)
    Have you ever wanted to display your content in rows of 3 columns? If you ever wanted to specify the number of columns per row within your content, here's the tutorial for you.
    Author: Charlie Griefer (CJ)
    Views: 34,987
    Posted Date: Thursday, May 29, 2003
  • Dynamic Column Output (Part Two)
    This tutorial picks up where the Dynamic Columns tutorial left off, showing you how to not only output your data in a specified number of columns, but how to do it while still publishing well formed HTML.
    Author: Charlie Griefer (CJ)
    Views: 27,534
    Posted Date: Saturday, May 31, 2003
  • Remote File Management
    Manage text-based files on your server from any Web browser. Create a new file, edit a file, or delete a file. Can be a life saver if you're on the road, and find an error in some of your code that needs a quick fix.
    Author: Charlie Griefer (CJ)
    Views: 27,815
    Posted Date: Tuesday, June 3, 2003
  • Save your visitor's clickstreams
    A nifty little custom tag that will allow you to save a visitor's clickstream through your site, as well as display it back to them (with links). Did I really just say 'nifty'?
    Author: Charlie Griefer (CJ)
    Views: 25,608
    Posted Date: Monday, June 16, 2003
  • Grouping Output in CF
    How to group cfquery output in order to effectively display relational database data. Includes an overview of how to output nested groups as well.
    Author: Charlie Griefer (CJ)
    Views: 32,508
    Posted Date: Tuesday, June 17, 2003
  • arrays and structures - part 1
    part one of a three-part tutorial designed to gently introduce you to the world of complex variables.
    Author: Charlie Griefer (CJ)
    Views: 38,749
    Posted Date: Monday, August 11, 2003
  • arrays and structures - part 2
    part two of a three-part tutorial designed to gently introduce you to the world of complex variables.
    Author: Charlie Griefer (CJ)
    Views: 28,290
    Posted Date: Monday, August 11, 2003
  • arrays and structures - part 3
    part three of a three-part tutorial designed to gently introduce you to the world of complex variables.
    Author: Charlie Griefer (CJ)
    Views: 33,332
    Posted Date: Monday, August 11, 2003
  • JavaScript Form Validation
    Yes, I know we're a ColdFusion site...but ColdFusion does not live in a vacuum. We have to know SQL, HTML, CSS...and sometimes...JavaScript! This tutorial focuses on using JavaScript (in lieu of cfform) to create client side form validation (and explains why writing your own is better than using ).
    Author: Charlie Griefer (CJ)
    Views: 58,283
    Posted Date: Thursday, August 14, 2003
  • CF 'Best Practices'
    Some tips and techniques that I've picked up over the years. I don't maintain that these are 'official' or 'absolute'...they are simply my preference and things that have worked for me. I would like to share them here, and leave you to make the decision as to whether or not they fit in your 'code arsenal' :)
    Author: Charlie Griefer (CJ)
    Views: 35,637
    Posted Date: Friday, August 15, 2003
  • Helping users obtain their passwords
    Your site requires your visitors to log in. of course, some of your visitors are going to forget their passwords (ok, most will forget their passwords). You don't want them to have to send you an e-mail, and then wait for a response. They need immediate access.

    This tutorial shows two methods by which you can accomodate them.
    Author: Charlie Griefer (CJ)
    Views: 26,198
    Posted Date: Thursday, August 28, 2003
Download the EasyCFM.COM Browser Toolbar!