从数组 ColdFusion 中删除关联对象
我有一个购物车数组,其中保存购物车中商品的产品信息。部分新增商品与“主商品”一起购买可享受特别折扣。
如果有人添加了一个与特价优惠相关的商品,我会在商品结构中设置一个名为 mainitem
的键,其值为 yes
,所有后续特价商品都会关联主项目的键 mainitem
设置为 no
并具有另一个名为 mainitemid
的键(这是 mainitem
uid )。
如果有人删除了主要项目,我需要确保所有相关的特价商品也被删除。这就是我遇到的麻烦,无法完全弄清楚如何找到它们。
我使用 form.itemID
来提供要删除的产品的项目 ID。我需要确保被删除的项目是主要项目;如果是这样,请循环浏览购物车的其余部分,找到任何 mainitemid
等于 form.itemid
的商品并将其删除,然后删除 mainitem
。
mainitem
定义为 session.mycart[i].mainitem
maintitenid 定义为 session.mycart[i].mainitemid
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
</cfloop>
我需要创建两个循环还是可以用一个循环来完成?我不确定我的条件陈述。
I have a shopping cart array which holds the product information of items in the cart. Some products which are added will be at a special discount if purchased with the "main item".
If someone adds an item which has special offers associated to it, I set a key in the item structure called mainitem
with a value of yes
, all the subsequent special offer items associated to the main item have key mainitem
set as no
and have another key named mainitemid
(this is the mainitem
uid).
If some one deletes the mainitem I need to ensure any related special offer items are deleted as well. This is what I am having trouble with, can't quite work out how to find them all.
I am using form.itemID
to supply the item id of the product being deleted. I need to make sure the item being deleted is a main item; if so, loop through the rest of the cart and find any item with mainitemid
equal to form.itemid
and remove them, then delete the mainitem
.
mainitem
is defined as session.mycart[i].mainitem
maintitenid is defined as session.mycart[i].mainitemid
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
</cfloop>
Do I need to create two loops or could I do it with one? I'm not sure of my conditional statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
针对 OP 特定问题的解决方案
进行修改以提供更完整的解决方案
在您的帖子中,您声明您正在从索引 1 循环到索引 ArrayLen(...)。如果您要从数组中删除项目,Coldfusion 有点简单,并没有真正引起注意,因此当您删除 5 元素数组的元素 2 时,数组将变为 4 个元素长(因为您删除了一个元素),并且原来索引 3 的元素现在是索引 2,因此它被遗漏了。
解决这个问题的方法是从最后开始并向后进行。只要您一次最多删除 1 条记录,那么这就是完全有效的,因为它将继续减少当前正在检查的索引,直到减少到 1,这将是第一个元素。
这样,您可以遍历元素 5、4、3、2,删除元素 2,然后它会检查索引 1,索引 1 现在仍然与开始循环时相同,因此不会发生跳过。
关于如何处理这个问题的一些简介
我误读了这个问题,或者在写这篇文章时对其进行了编辑,但下面的内容是适用的,所以仍然将其留在这里
您是否考虑过有特殊的提供项目作为主项目的子项,因为它将整个报价封装在一起,删除父项也会删除子项。我有一个类似的问题,其中项目具有与其关联的选项,并且为了允许观察层次结构,我决定创建与引用数组相结合的树。只是作为一个粗略的例子来展示原理,看看这个
在我的实际代码中,我通过创建一个 Item.cfc 来完成此操作,其中包含处理上述内容的方法并向上级联树删除孙子等,但是这个原则是合理的。本质上,您拥有允许将项目公开为平面数组以及父项、子项和兄弟项的层次结构的方法。
信息点
顺便说一句,当“数组”和“结构”略有不同时,您可以不断地互换它们,这与 PHP 等语言不同,在 PHP 中,数组用于引用这两个术语。数组包含数字索引从 1 到 n 的值,而结构体是保存与任意键相关的值的对象。但存在差异的原因是它们不是基于相同的基础 java 对象构建的,因此某些适用于其中一个对象的方法不适用于另一个对象。
Solution for OPs specific issue
Revised to offer a more complete solution
In your post, you state you are looping from index 1 to index ArrayLen(...). If you are deleting items from the array though, Coldfusion is a bit straightforward and doesn't really pay attention and so when you delete element 2 of a 5-element array, the array becomes 4 elements long (because you deleted one) and the element which was index 3 is now index 2, thus it is missed.
The way around this is to start at the end and work backwards. As long as you are deleting at most 1 record at a time, then this is perfectly valid as it will continue reducing the index it is currently checking until you get down to 1, which will be the first element.
This way you can go through element 5, 4, 3, 2, delete element 2, and it will then check index 1 which now will still be the same item as it was when you started the loop and thus no skipping is experienced.
Some blurb on how to deal with this
I misread the question, or it got edited while writing this, but the below is applicable so leaving it here still
Have you considered having special offer items as children to the main item, as then it encapsulates the entire offer together and deleting the parent deletes the child. I've a similar issue where items have options associated with them, and to allow hierarchy to be observed I decided on creating a tree combined with a reference array. Just as a rough example to show the principle, take a look at this
In my actual code I've done this by way of creating an Item.cfc with methods to deal with the above and to cascade up the tree deleting grandchildren etc, but the principle is sound. Essentially you have methods that allow the items to be exposed as both a flat array and as a hierarchy of parents, children, and siblings.
Informational point
As an aside, you keep interchanging "array" and "structure" when the two are slightly different unlike languages like PHP where array is used to refere to both terms. An array contains values with a numeric index from 1 up to n, while a structure is an object that holds values related to arbitary keys. The reason that there is a difference though is that they don't build off the same base java objects so some methods that work on one don't work on the other.
当然,这还没有经过测试,您可能必须使用它来输入确切的变量名称,但我认为它应该可以帮助您实现这一点。如果我误解了这个问题,请告诉我。
This isnt tested of course and you may have to work with it to put your exact variable names in, but I think it should get you there. Let me know if I have misunderstood the question.
我没有找到一种方法可以在一个循环中完成此操作,因为我将其视为一个两步过程。首先,您必须查看删除的项目 ID 是否是主项目,然后返回数组并删除其余关联项目。
根据需要添加 StructKeyExists()。
I don't see a way to do this in one loop, as I view it as a two step process. First you have to see if the item id you deleted is a main item, then go back through the array and delete the rest of the associated items.
Add StructKeyExists() as necessary.