Coldfusion - 遍历一行中的列

Coldfusion - Loop over columns on a row

我有一个 CFQuery,其中 return 很重要。 例如:

count1 | count2 | count3
   1   |   23   |  27

结果始终是一行,但列并不总是相同。我的意思是有时可能 return 3 列,有时 10 多列,我不知道它们的名字。 我的目标是遍历列的名称并获取它们的值并将它们呈现在 table.

我试过这个:

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

但我得到这个错误:

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.

有人知道如何遍历列及其值吗?

您将圆点表示法与括号表示法混用了。

这应该可以满足您的要求:

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

columnList 属性始终包含在查询对象中,并且是以逗号分隔的列名列表。只需在循环中使用它并使用括号表示法输出查询值。

编辑:如@Tomalak 所述,您还可以使用 currentRow:

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