tags:

views:

104

answers:

3
+5  Q: 

Evaluate function

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.

+4  A: 

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
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
Good point. :) .
jarofclay
+10  A: 
<cfloop list="#qry.Columnlist#" index="FieldName">
    <cfset form[FieldName] = qry[FieldName][1]>
</cfloop>

?

Henry
Good answer! I was looking for a solution to this today too.
Dan Sorensen
A: 

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
Very interesting. I'm getting confused mixing struct notation and array notation, but I'll look into this. Thanks!
cf_PhillipSenn
Yah sorry, {} is shorthand for StructNew() and [] is shorthand for ArrayNew(1)
Bialecki