Ask Ben: Displaying A Query Vertically Instead Of Horizontally

Posted March 4, 2007 at 10:41 AM by Ben Nadel

Tags: ColdFusion, Ask Ben

I am used to outputting a ColdFusion query horizontally (going across then breaking into another row). However, I need to display a query vertically through columns but I can't figure out how to loop over it or where to break the rows. Please help.

Looping over a query and outputting it across rows of table is nice and easy because there is no calculations that need to be done. Well not true, you do have to figure out when to break in to a new row, but that's a simple MOD calculation. If you want to display a query vertically, things get much more sticky because you are outputting in a horizontal methodology, but the cell value has to be based on the vertical position and column offset.

In order to figure out what query value to output you need to figure out where you are in terms of output row and column:

  • <!---
  • To demonstrate, first we have to build a test query.
  • For the purposes of demonstration, we are just going
  • to build a simple, number-incrementing query so you
  • can easily see where the values are output.
  • --->
  • <cfset qNumbers = QueryNew( "" ) />
  •  
  • <!--- Populate the query. --->
  • <cfset QueryAddColumn(
  • qNumbers,
  • "number",
  • "CF_SQL_VARCHAR",
  • ListToArray( "1,2,3,4,5,6,7,8,9,0" )
  • ) />
  •  
  •  
  • <!---
  • In order for this to work, you have to explicitly
  • define how many columns you want to output.
  • --->
  • <cfset intColCount = 3 />
  •  
  • <!---
  • Based on the columns and the record count of the query,
  • we can determine how many rows we are going to output.
  • In this case, we are using the ceiling function as we
  • will need to round up.
  • --->
  • <cfset intRowCount = Ceiling(
  • qNumbers.RecordCount / intColCount
  • ) />
  •  
  •  
  • <!---
  • Output the query. Unlike a traditional horizontally
  • outputted query, we cannot loop over the query itself
  • as it does not have "proper" ordering. Instead, we
  • have to loop over the rows and the columns and then
  • grab the appropriate value out of the query.
  • --->
  • <table>
  • <tr>
  •  
  • <!--- Loop over the rows, which we calculated above. --->
  • <cfloop
  • index="intRow"
  • from="1"
  • to="#intRowCount#">
  •  
  • <!--- Loop over the columns (set above). --->
  • <cfloop
  • index="intColumn"
  • from="1"
  • to="#intColCount#">
  •  
  • <!---
  • Here's where the magic happens. We need to
  • figure out which query cell value we are
  • getting based on the current row and column
  • indexes. If you think about it, the beginning
  • of each column begins with all the values
  • from the previous columns. That means that
  • each column begins with (COLUMN - 1) * the
  • number of rows in a column.
  • --->
  • <cfset intCellIndex = ((intRowCount * (intColumn - 1)) + intRow) />
  •  
  • <!---
  • Now, because we are outputting over a RxC
  • output, its possible that we will have a cell
  • that is outside of the record set. Check to
  • see if the cell index is beyond the record
  • count.
  • --->
  • <td>
  • <cfif (intCellIndex LTE qNumbers.RecordCount)>
  • #qNumbers[ "number" ][ intCellIndex ]#
  • <cfelse>
  • .<br />
  • </cfif>
  • </td>
  •  
  • </cfloop>
  •  
  • <!---
  • Check to see if we have any more rows to output.
  • Once we hit the final row, we will let the </tr>
  • post CFLoop handle the final close.
  • --->
  • <cfif (intRow LT intRowCount)>
  • </tr>
  • <tr>
  • </cfif>
  •  
  • </cfloop>
  •  
  • </tr>
  • </table>

Running that gives us the following output:

[ 1 ][ 5 ][ 9 ]
[ 2 ][ 6 ][ 0 ]
[ 3 ][ 7 ][ . ]
[ 4 ][ 8 ][ . ]

It's a bit more work, but it gets the job done.



Reader Comments

Mar 4, 2007 at 11:23 AM // reply »
2 Comments

It is producing an error:

Variable QNUMBERS is undefined.


The error occurred in D:\Sites\CF Test\index.cfm: line 14

12 : <body>
13 : <!--- To demonstrate, first we have to build a test query. For the purposes of demonstration, we are just going to build a simple, number-incrementing query so you can easily see where the values are output. --->
14 : <cfset QueryAddColumn(qNumbers,"number","CF_SQL_VARCHAR",ListToArray( "1,2,3,4,5,6,7,8,9,0" )) />


Mar 4, 2007 at 1:16 PM // reply »
10,640 Comments

@Ken,

Opps, I missed the very first line of code:

<cfset qNumbers = QueryNew( "" ) />

I have updated the code above. Sorry about that.


Tom
Mar 14, 2007 at 8:54 AM // reply »
15 Comments

I'm having trouble substituting a query in place of the number array you setup. I'm getting the right number of rows/columsn but as soon as I put the cfquery tag in, the output is wrong. I'm assuming I need to replace one of the cfloops with my cfoutput statements.


Mar 14, 2007 at 9:05 AM // reply »
10,640 Comments

DL, shoot me your code and I will take a look... ben [ at ] bennadel [ dot ] com.


Tom
Mar 15, 2007 at 9:50 AM // reply »
15 Comments

Thanks for the help, err. the code. I wasn't able to solve the problem but you sure did!

I look forward to seeing your posts in the other TAGS.


May 30, 2007 at 2:52 PM // reply »
2 Comments

Ben,
Would you mind posting the code that helped DL? I want to replace your array with a standard query.

--
Thanks for such a great blog!
Frank


May 30, 2007 at 4:36 PM // reply »
10,640 Comments

@Frank,

Glad you like the blog. The code above is actually working on a query. What you have to realize is that the query is being acted on just like it were an array. When I use the code:

#qNumbers[ "number" ][ intCellIndex ]#

That is access the query, "qNumbers", and getting the column value for the column "number" at row "intCellIndex".


May 30, 2007 at 4:53 PM // reply »
2 Comments

Ben,
Your the man, got it!

Keep up the quality posts!

--
Frank


May 30, 2007 at 4:55 PM // reply »
10,640 Comments

@Frank,

No problem. And if you ever get stuck on something or would just like to see a demo of something written up - I like to write demos :)

http://www.bennadel.com/ask-ben/


Jan 27, 2012 at 7:23 AM // reply »
1 Comments

Awesome! Saved me from wasting time on research and trial and error. Continue to keep posting these solutions.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »