I have a query and a list of field/column names. I want to do a sort of double loop - loop through each record in the query, and then loop through the list of field/column names and output each corresponding field. The loops should be something like this:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>?</td>
</cfloop>
</tr>
</cfoutput>
</table>
The problem is what to put where the question mark is... I've tried #myquery[col]#
, but this didn't work. I need to get the variable indicated by the string name in the variable col
... And obviously, #col#
will just return the column name. I need to figure out some way to double-evaluate the string... something like ##col##
, which of course won't work either. How can I accomplish this?
When referencing column names as a structure, you need to also tell the query which row you want to get. You should also make sure that you check that the column name exists if you didn't get the cols variable via myQuery.ColumnList.
Use the following code to dynamically reference each column in your loop:
<table>
<cfoutput query="myQuery">
<tr>
<cfloop list="#cols#" index="col">
<td>#myQuery[col][CurrentRow]#</td>
</cfloop>
</tr>
</cfoutput>
</table>