Coldfusion 更新数量不添加新结构
我目前正在构建一个购物车,它使用数组中的结构来保存购物车信息。如果产品已添加到购物车,并且再次按下“添加到购物车”按钮,则我需要更新结构数量,而不是将其他项目添加到购物车。当按下“添加到购物车”按钮时,我首先检查数组是否为空,如果不是,我在数组中循环查找表单提交的产品id,如果找到,我只需更新与产品id关联的数量字段,然后设置变量addNew=no。如果找不到产品,我使用了cfelse设置变量addNew=yes。我意识到我的问题是,如果购物车有多个产品,循环会继续,显然在某个点上它找不到产品id并设置变量addNew=new,然后它会更新数量,也会将产品添加到一个新结构中,结果是产品a数量2和产品a数量1 这是第一次以这种方式使用数组和结构,我只是找到了解决方法,所以如果我的代码效率不高,我道歉。非常感谢任何指点Coldfusion 更新数量不添加新结构,coldfusion,Coldfusion,我目前正在构建一个购物车,它使用数组中的结构来保存购物车信息。如果产品已添加到购物车,并且再次按下“添加到购物车”按钮,则我需要更新结构数量,而不是将其他项目添加到购物车。当按下“添加到购物车”按钮时,我首先检查数组是否为空,如果不是,我在数组中循环查找表单提交的产品id,如果找到,我只需更新与产品id关联的数量字段,然后设置变量addNew=no。如果找不到产品,我使用了cfelse设置变量addNew=yes。我意识到我的问题是,如果购物车有多个产品,循环会继续,显然在某个点上它找不到产品i
<cfif arrayLen(session.mycart) GT 0>
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
<!---check for existance of the id submitted--->
<cfif session.mycart[i].itemID eq form.itemID>
<!---if the id is matched update the quantity--->
<cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity>
<cfset myTotal = form.itemCost*session.mycart[i].quantity>
<cfset session.mycart[i].totalPrice = myTotal>
<!---this will tell the add to cart function not add a new item--->
<cfset addNew = "no">
<cfelse>
<!---as this is a new item tell the add to cart function to add it--->
<cfset addNew ="yes">
</cfif>
</cfloop>
<cfelse>
<!---as the array is empty tel the add to cart function insert the product--->
<cfset addNew ="yes">
</cfif>
听起来您需要的是购物车中每个商品的布尔值,而不仅仅是一个。因此,您甚至可以将其添加到现有结构中。此外,尽管ColdFusion允许您使用字符串“yes”和“no”作为布尔值,但我认为最好使用true和false
<cfif session.mycart[i].itemID eq form.itemID>
<!---if the id is matched update the quantity--->
<cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity>
<cfset myTotal = form.itemCost*session.mycart[i].quantity>
<cfset session.mycart[i].totalPrice = myTotal>
<!---this will tell the add to cart function not add a new item--->
<cfset session.mycart[i].addNew = FALSE>
<cfelse>
<!---as this is a new item tell the add to cart function to add it--->
<cfset session.mycart[i].addNew =TRUE>
</cfif>
听起来您需要的是购物车中每个商品的布尔值,而不仅仅是一个。因此,您甚至可以将其添加到现有结构中。此外,尽管ColdFusion允许您使用字符串“yes”和“no”作为布尔值,但我认为最好使用true和false
<cfif session.mycart[i].itemID eq form.itemID>
<!---if the id is matched update the quantity--->
<cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity>
<cfset myTotal = form.itemCost*session.mycart[i].quantity>
<cfset session.mycart[i].totalPrice = myTotal>
<!---this will tell the add to cart function not add a new item--->
<cfset session.mycart[i].addNew = FALSE>
<cfelse>
<!---as this is a new item tell the add to cart function to add it--->
<cfset session.mycart[i].addNew =TRUE>
</cfif>
我能想到两件事 首先,您可以通过取消对数组的外部长度检查来简化代码:循环将为您覆盖它 第二,当你发现并更新你的数量(循环中IF的真实部分)时,你可以停止寻找这个项目。在阵列的其余部分继续寻找您已经找到的东西是没有意义的。因此,在该点使用CFBREAK退出循环
要记住的另一件事是,只有在尚未设置变量时,才可以使用CFPARAM来设置变量。因此,如果将addNew设置为true,并且随后调用CFPARAM,则该变量将保持原样。但是在这种情况下,CFBREAK方法更好。我可以想到两件事 首先,您可以通过取消对数组的外部长度检查来简化代码:循环将为您覆盖它 第二,当你发现并更新你的数量(循环中IF的真实部分)时,你可以停止寻找这个项目。在阵列的其余部分继续寻找您已经找到的东西是没有意义的。因此,在该点使用CFBREAK退出循环
要记住的另一件事是,只有在尚未设置变量时,才可以使用CFPARAM来设置变量。因此,如果将addNew设置为true,并且随后调用CFPARAM,则该变量将保持原样。但是在这种情况下,CFBREAK方法更好。您应该在开始时将标志设置为true,然后在循环时,如果找到产品,只需将标志设置为false即可
<!--- first set the flag to add item to cart --->
<cfset addNew = true>
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
<!---check for existance of the id submitted--->
<cfif session.mycart[i].itemID eq form.itemID>
<!---if the id is matched update the quantity--->
<cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity>
<cfset myTotal = form.itemCost*session.mycart[i].quantity>
<cfset session.mycart[i].totalPrice = myTotal>
<!---if the item is already in the cart, tell the add to cart function not add a new item--->
<cfset addNew = false>
</cfif>
</cfloop>
您应该在开始时将标志设置为true,然后在循环时,如果找到产品,只需将标志设置为false即可
<!--- first set the flag to add item to cart --->
<cfset addNew = true>
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
<!---check for existance of the id submitted--->
<cfif session.mycart[i].itemID eq form.itemID>
<!---if the id is matched update the quantity--->
<cfset session.mycart[i].quantity = form.quantity+session.mycart[i].quantity>
<cfset myTotal = form.itemCost*session.mycart[i].quantity>
<cfset session.mycart[i].totalPrice = myTotal>
<!---if the item is already in the cart, tell the add to cart function not add a new item--->
<cfset addNew = false>
</cfif>
</cfloop>
谢谢你的回复,我不得不跳出来,一回来就会试试你的补救办法!非常感谢。谢谢你的回复,我不得不跳出来,一回来就会试试你的补救办法!非常感谢,谢谢大家!我使用了break方法,并在顶部将变量设置为true。非常好用,谢谢大家!我使用了break方法,并在顶部将变量设置为true。而且它工作得很好。