looping over column names

Occasionally I just need a dump of results from a query columns and values. I have two methods for this.

The SQL (cfquery name=”myquery”)
Select TOP 1 * from contacts
 
The simple method allows me to output the column names and the values in alphabetical order. The evaluate(column) is what displays the values.
<cfoutput query="myquery">
< cfloop list="#myquery.ColumnName#" index="column">
#column#: #Evaluate(column)# <br>
</cfloop>
 
Column name list Yields: address, city, contact_id, email, fname, lname, phone, state, zipcode. Which while useful is a bit confusing for output.
 
If I want my columns output in a particular order I merely replace the "list=#myquery.ColumnName#" with a list of the columns I want to display in the order I want to display them. The rest of the code is the same.
 
< cfloop list="fname, lname, address, city, state, zipcode, phone" index="column">
 
I can even suppress data that is blank or meets other criteria within the loop.
<cfif #len(EVALUATE(column))# gt 1>#column#:
   <cfif isdate(EVALUATE(column))>
   #dateformat(EVALUATE(column), "mm/dd/yyyy")#
   <cfelse>
   #EVALUATE(column)#
   </cfif>
</cfif>

Cup size   
Select size then click on coffee cup.
This entry was posted in Code. Bookmark the permalink.