0

I have a CFQuery which returns some counts. For example:

count1 | count2 | count3
   1   |   23   |  27

The result is always one row BUT the columns are not always the same. I mean sometimes might return 3 columns and sometimes 10+ columns which I don't know their names. My goal here is to loop over the names of the columns and get their values and present them in a table.

I have tried this:

<cfloop list="#qGetCommentsDetails#" index="col">
    <cfloop query="qGetCommentsDetails">
        #qGetCommentsDetails.[col][currentRow]#
    </cfloop>
</cfloop>

but I get this error:

A CFML variable name cannot end with a "." character.

The variable qGetCommentsDetails. ends with a "." character. You must either provide an additional structure key or delete the "." character.

Anyone knows how to loop over columns and their values?

BlackM
  • 3,927
  • 8
  • 39
  • 69

1 Answers1

4

You are mixing dot notation with bracket notation.

This should do what you want:

<cfif qGetCommentsDetails.recordCount>
    <cfloop list="#qGetCommentsDetails.columnList#" index="col">
        <cfoutput>
            #col# : #qGetCommentsDetails[col][1]# <br/>
        </cfoutput>
    </cfloop>
</cfif>

The columnList attribute is always included in a query object, and is a comma delimited list of the column names. Just use this in your loop and use bracket notation to output the query values.

EDIT: As @Tomalak noted, you can also use currentRow:

<cfoutput query="qGetCommentsDetails">
    <cfloop list="#qGetCommentsDetails.columnList#" index="col">
        #qGetCommentsDetails[col][qGetCommentsDetails.currentRow]#
    </cfloop>
</cfoutput>
Tomalak
  • 332,285
  • 67
  • 532
  • 628
beloitdavisja
  • 1,509
  • 10
  • 13