6

Is there a better way to write the following?

<cfloop list="#qry.Columnlist#" index="FieldName">
   <cfset "form.#FieldName#" = Evaluate("qry.#FieldName#")>
</cfloop>

This loop is assigning every field in the query to a corresponding form field. I understand the evaluate function is shunned.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373

3 Answers3

11
<cfloop list="#qry.Columnlist#" index="FieldName">
    <cfset form[FieldName] = qry[FieldName][1]>
</cfloop>

?

Henry
  • 32,689
  • 19
  • 120
  • 221
4

Assuming you are returning a single recordset the following will work.

<cfloop list="#qry.Columnlist#" index="FieldName">
<cfset "form.#FieldName#" = qry[FieldName][1]>
</cfloop>
jarofclay
  • 680
  • 4
  • 12
  • 1
    Technically, there is nothing wrong with that. But you may as well go the whole way and use array notation for both sides of the cfset ;) – Leigh Jan 06 '10 at 20:30
1

Tangential, but if you were looping over multiple rows of a query, you could use the currentRow variable in the query object to do the same thing as the accepted answer.

<cfset var someStruct = {} />
<cfset var colummnList = queryObj.columnList />

<cfloop query="queryObj">
    <cfset someStruct[currentRow] = {} />        

    <cfloop list="#columnList#" index="fieldName">
        <cfset someStruct[currentRow][fieldName] = queryObj[fieldName][currentRow] />
    </cfloop>
</cfloop>
Bialecki
  • 30,061
  • 36
  • 87
  • 109