Evaluate function

我的未来我决定 提交于 2019-12-08 20:38:58

问题


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.


回答1:


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

?




回答2:


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

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



回答3:


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>


来源:https://stackoverflow.com/questions/2015429/evaluate-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!