遇到一个问题,我想我可以看看是否有人对如何解决它有任何想法。
基本上,我在单个变量下传递多个值,并且我想使用循环来提取每个单独的值并同时插入它。
例如,ischecked 是我用来传入设备值的变量。如果我要选择两个设备,按提交并将变量#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.
我用它来找出插入循环应该持续多长时间。
我本来也可以只检查数组的长度,但我也打算将该列表用于其他目的。
<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>
所以基本上这就是我陷入困境的地方,循环遍历这些值,但它查找 devicetest1 而不是 device test (因为索引),但我一生都无法弄清楚如何传递这些值这样它就能单独挑选出每一个。
我见过一些例子,人们将索引(i)附加到值,然后用它来插入,但并没有真正理解它是如何工作的。
谢谢,
约旦