gpt4 book ai didi

data-structures - ColdFusion 循环遍历具有键评估的结构失败!我错过了什么?

转载 作者:行者123 更新时间:2023-12-04 07:11:42 24 4
gpt4 key购买 nike

我的 cfm 中有这段代码,它可以工作

<cfif not StructIsEmpty(form)>
<cfset larray = user.getArray() />
<cfloop collection="#form#" item="key">
<cfif left(key,4) eq "UPD_">
<cfset x = listLast(key,"_") />
<cfset y = evaluate(0,key) />
<cfloop index="j" from="1" to="#arrayLen(larray)#">
<cfif (larray[j][1] eq x) and (larray[j][3] neq y)>
<cfset larray[j][3] = y />
<cfif not LSIsNumeric(larray[j][3])>
<cfset larray[j][3] = "0" />
</cfif>
<cfset larray[j][4] = "update" />
</cfif>
</cfloop>
</cfif>
</cfloop>

<cfloop collection="#form#" item="key">
<cfif left(key,4) eq "DEL_">
<cfset x = listLast(key,"_") />
<cfloop index="k" from="1" to="#arrayLen(larray)#">
<cfif larray[k][1] eq x>
<cfset larray[k][4] = "delete" />
</cfif>
</cfloop>
</cfif>
</cfloop>

<cfset user = createObject("component", "cfc.User").init(
identifier = FormatBaseN(form.id,10),
array = larray
) />
</cfif>

<form name="usform" method="POST">
<cfset array = user.getArray() />
<cfoutput>
<cfloop index="i" from="1" to="#arrayLen(array)#">
<table>
<tr>
<td><input type="text" name="upd_#array[i][1]#" maxlength="6" size="6" value="#array[i][3]#" /></td>
<td><input type="checkbox" name="del_#array[i][1]#" /></td>
</tr>
</table>
<input type="hidden" name="id" value="#user.getIdentifier()#" />
</cfoutput>
</form>

我已经把它放到一个 cfc 中来分离我的逻辑和我的观点,我正在努力让它更通用

<cfcomponent name="ArrayManager" output="false">
<cffunction name="init" hint="constructor" output="false" returntype="ArrayManager">
<cfargument name="user" type="User" required="true" hint="User bean" />
<cfargument name="form" type="Struct" required="true" />
<cfset variables.instance.array = arguments.user.getArray() />
<cfset variables.instance.form = arguments.form />
<cfreturn this />
</cffunction>

<cffunction name="update" access="public" output="true" returntype="boolean">
<cfargument name="structstring" type="String" required="true" />
<cfargument name="seperator" type="String" required="true" />
<cfset var x = "0" />
<cfset var y = "0" />
<cfloop collection="#variables.instance.form#" item="key">
<cfif key eq "#arguments.structstring#">
<cfset x = listLast(key,"#arguments.seperator#") />
<cfset y = evaluate(0,key) />
<cfloop index="j" from="1" to="#arrayLen(variables.instance.array)#">
<cfif (variables.instance.array[j][1] eq x) and (variables.instance.array[j][3] neq y)>
<cfset variables.instance.array[j][3] = y />
<cfif not LSIsNumeric(variables.instance.array[j][3])>
<cfset variables.instance.array[j][3] = "0" />
</cfif>
<cfset variables.instance.array[j][4] = "update" />
</cfif>
</cfloop>
</cfif>
</cfloop>
<cfset arguments.user.init(array = variables.instance.array) />
<cfreturn true />
</cffunction>

<cffunction name="delete" access="public" output="false" returntype="boolean">
<cfargument name="structstring" type="String" required="true" />
<cfargument name="seperator" type="String" required="true" />
<cfset var x = "0" />
<cfloop collection="#variables.instance.form#" item="key">
<cfif key eq "#arguments.structstring#">
<cfset x = listLast(key,"#arguments.seperator#") />
<cfloop index="k" from="1" to="#arrayLen(variables.instance.array)#">
<cfif variables.instance.array[k][1] eq x>
<cfset variables.instance.array[k][4] = "delete" />
</cfif>
</cfloop>
</cfif>
</cfloop>
<cfset arguments.user.init(array = variables.instance.array) />
<cfreturn true />
</cffunction>
</cfcomponent>

还有我的新cfm

<cfif not StructIsEmpty(form)>
<cfset arraymanager = createObject("component","cfc.ArrayManager").init(user,form) />
<cfset seperator = "_" />
<cfset structstring = "UPD" />
<cfset arraymanager.update(structstring,seperator) />
</cfif>
...

它失败了,我得到这个错误信息

The CFML compiler encountered an unexpected coldfusion.compiler.CompilerInternalException exception. The reason for this was: Unable to complete CFML to Java translation. Occurred at:

. . .

The error occurred in C:\path\to\document\root\cfc\ArrayManager.cfc: line 21
Called from C:\path\to\document\root\cfc\update-emp.cfm: line 66
Called from C:\C:\path\to\document\root\cfc\update-emp.cfm: line 66

19 : <cfif key eq "#arguments.structstring#"><br/>
20
:
21 : <cfset y = evaluate(0,key) /><br/>
22
:
23 `:

我做错了什么或者有更好的方法来完成我想做的事情(在表中显示数据库内容并通过同一个表更新(更新和删除)数据库内容)

最佳答案

您发布的错误消息表明您滥用了 Evaluate功能。根据文档,它是这样工作的:

Evaluates one or more string expressions, dynamically, from left to right. (The results of an evaluation on the left can have meaning in an expression to the right.) Returns the result of evaluating the rightmost expression.

但是您还有其他问题。对于初学者来说,当您将代码移入 CFC 时,您并没有正确地复制逻辑。

在您的工作代码中,您使用条件:

<cfif left(key,4) eq "UPD_">

但是在你的 CFC 中你有:

<cfif key eq arguments.structString>

这应该是:

<cfif left(key,4) eq arguments.structString>

接下来,您没有使用最好的求值语法,您可能根本不想使用它。声明:

y = evaluate(0,key)

可以重写为:

y = evaluate(key)

因为key的值为“UPD_something”,所以可以重写为:

y = [variables|arguments|etc].UPD_Something

(由于您没有明确指定变量作用域,CF 将尝试按特定顺序在一组作用域中查找变量;这就是我使用语法 [a|b|...] 的原因)

您可能不是这个意思,您可能想要表单中的值。由于键名是动态的,您应该以这种方式访问​​它(而不是使用 evaluate ):

y = variables.instance.form[key]

我认为这可能会解决问题。所以,总结一下:

  1. 替换您的声明 <cfif key eq arguments.structString><cfif left(key,4) eq arguments.structString> (并确保您作为“structString”传递的值包含下划线!)

  2. 替换您对 evaluate 的使用与:y = variables.instance.form[key]

我希望能解决您的问题...

让它工作后,开始考虑您的变量名。 “数组”是一个糟糕的变量名,因为它实际上是 CFML 中的保留字。并且使用“x”和“y”根本不是描述性的。这些类型的问题使这个问题难以回答。

关于data-structures - ColdFusion 循环遍历具有键评估的结构失败!我错过了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1062893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com