Evaluate function
Asked Answered
J

3

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.

Jotun answered 6/1, 2010 at 19:2 Comment(0)
C
11
<cfloop list="#qry.Columnlist#" index="FieldName">
    <cfset form[FieldName] = qry[FieldName][1]>
</cfloop>

?

Coalesce answered 6/1, 2010 at 19:35 Comment(4)
Good answer! I was looking for a solution to this today too.Palsgrave
What about evaluate("qry.#myArr[i].foo#") I tried qry[myArr[i][foo]] but no luckAna
@Ana not sure what u're asking there, but for array object, it's [fieldName][rowIndex]. Is foo the rowIndex? if so, set myArr[1] to some var first, then use that. CF might not like nested []Coalesce
myArr is an array of structs, so foo is an attribute in the struct.Ana
J
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>
Joashus answered 6/1, 2010 at 19:25 Comment(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 ;)Casement
L
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>
Lith answered 14/1, 2010 at 14:52 Comment(2)
Very interesting. I'm getting confused mixing struct notation and array notation, but I'll look into this. Thanks!Jotun
Yah sorry, {} is shorthand for StructNew() and [] is shorthand for ArrayNew(1)Lith

© 2022 - 2024 — McMap. All rights reserved.