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.
When we have a function in formula form, it is usually a simple matter to evaluate the function. For example, the function f(x)=5−3x2 f ( x ) = 5 − 3 x 2 can be evaluated by squaring the input value, multiplying by 3, and then subtracting the product from 5.
A function will only have one or zero outputs for any input. If there is more than one output for a particular input, the equation does not define a function. In order to be a function, each value of x should have, at most, one output value of y .
<cfloop list="#qry.Columnlist#" index="FieldName">
<cfset form[FieldName] = qry[FieldName][1]>
</cfloop>
?
Assuming you are returning a single recordset the following will work.
<cfloop list="#qry.Columnlist#" index="FieldName">
<cfset "form.#FieldName#" = qry[FieldName][1]>
</cfloop>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With