Jef*_*iny 1 coldfusion coldfusion-9
我不确定这是否可行.我可以连接两个表示变量名的字符串,然后从中读取变量值吗?例如:
<cfloop index="person" from="0" to="#numberAuthorized - 1#">
//USING 'thePerson' DIDN'T WORK EITHER
<cfset thePerson = 'authorized_name' & person>
<cfquery name="qAddAuthorizedPeople" datasource="#thedb#" blockfactor="100">
INSERT INTO people (requestid, fullname)
VALUES ('#requestid#', '#authorized_name & 1#')
</cfquery>
</cfloop>
Run Code Online (Sandbox Code Playgroud)
我需要阅读的变量是authorized_name0, authrozied_name1, etc..所以我循环,所以我可以增加一个索引,将它追加到变量名的末尾.然后插入我的数据库.显然,这不起作用.我之前尝试连接并将其设置为变量(thePerson)并放入#thePerson#查询,但这也无效.我能做到这一点吗?
编辑我不知道你是如何传递这些变量的.但是说它们在FORM范围内...... FORM也是一种结构.使用任何结构,您都可以使用关联数组表示法动态访问键.(另外,请务必正确调整变量的范围.)
<cfloop from="0" to="#numberAuthorized - 1#" index="counter">
<!--- extract value of authorized_name0, authorized_name1, ... --->
<cfset variables.fullName = FORM["authorized_name"& counter]>
<cfquery name="qAddAuthorizedPeople" datasource="#thedb#" blockfactor="100">
INSERT INTO people (requestid, fullname)
VALUES (
<cfqueryparam value="#requestid#" cfsqltype="cf_sql_varchar">
, <cfqueryparam value="#variables.fullName#" cfsqltype="cf_sql_varchar">
)
</cfquery>
</cfloop>
Run Code Online (Sandbox Code Playgroud)