The for-in loop construct in ColdFusion is generally used to loop over a structure variable. In ColdFusion 10, one can use the same for-in construct to loop over Query and List variables. Here is an example in which the for-in construct is used to loop over Query and List variables.
The variable row used in the for-in construct is a structure variable. It contains query columns as keys.
<!--- Fetch some records from a table --->
<cfquery name="arts" datasource="artgallery" >
select * from ART
</cfquery>
<table border="1">
<cfscript>
//copy the list containing the column names to a variable 'columns'
//columns variable contains list - ARTID, ARTISTID, ARTNAME, DESCRIPTION, ISSOLD, LARGEIMAGE, MEDIAID, PRICE
columns = arts.ColumnList;
//use for in construct to loop over a list
for (col in columns) {
writeOutput("<th>" &col& "</th>");
}
writeOutput("<tr>");
/*
loop over the resultset arts. The variable 'row' used in for-in
construct is a struct that contains query columns as keys
*/
for (row in arts) {
for(col in columns)
writeOutput("<td>" &row[col]& "</td>");
writeOutput("<tr>");
}
</cfscript>
</table>
The variable row used in the for-in construct is a structure variable. It contains query columns as keys.
Comments
Post a Comment