遇到了一个问题,我想我可能会看到是否有人对如何修复它有任何想法.
基本上,我在一个奇异变量下传递多个值,我想使用一个循环来提取每个单独的值并同时插入它.
例如,缺血是我用来传递设备值的变量.如果我要选择两个设备,按下提交并在我的处理页面中转储变量#form.ischecked#,我会得到一个值为41,42的值.我需要一种方法来分割这些值,并想出一个cfloop和insert将是完美的.
如果重要的话,这都是在cfc中完成的.
<cfset devicearray = ArrayNew(1)>
<cfset temp = ArrayAppend(devicearray, #ischecked#)>
<cfset test = ArrayToList(devicearray, ",")>
<cfset length= ListLen(test)>\
\\this loop takes the amount of devices selected, and outputs the length of the list.
Run Code Online (Sandbox Code Playgroud)
我用它来找出插入循环应该有多长.我原本也可以检查数组的长度,但我也会将该列表用于其他目的.
<cfset devicetest = #form.ischecked#>
<cfset usertest = #form.userid#>
\\form.ischecked is the variable that contains the device IDs
\\form.userid is the variable that contains the User IDs
<cfquery name="loopquery" datasource="Test">
<cfloop from="1" to="#length#" index="i">
\\loop from 1 to "length", the number of Devices selected as specified earlier
INSERT INTO Loan (DeviceID, UserID)
VALUES ("#Evaluate("devicetest#i#")#","#Evaluate("userID#i#")#" )
</cfloop>
</cfquery>
Run Code Online (Sandbox Code Playgroud)
所以基本上就是我坚持的地方,循环遍历值,但它寻找devicetest1而不是设备测试(因为索引),但我不能为我的生活弄清楚如何传递值这样它就可以单独挑出每一个.
我已经看到一些例子,其中人们已经使用值附加了索引(i),然后使用它来插入,但是并不真正理解它将如何工作.
谢谢,乔丹
我觉得你这很复杂了.下面将循环遍历表单变量中的列表.
<!--- dummy data --->
<cfset form.userid = 75>
<cfset form.ischecked = '46,47'>
<cfloop list="#form.ischecked#" index="i">
<cfquery name="loopquery" datasource="Test">
INSERT INTO Loan (DeviceID, UserID)
VALUES (
<cfqueryparam cfsqltype="cf_sql_integer" value="#i#">,
<cfqueryparam cfsqltype="cf_sql_integer" value="#form.userid#">
)
</cfquery>
</cfloop>
Run Code Online (Sandbox Code Playgroud)