2012-02-22 31 views
1

我有一个购物车数组,它包含购物车中物品的产品信息。如果与“主要项目”一起购买,某些添加的产品将享有特别折扣。从数组中删除关联的对象ColdFusion

如果有人将其具有关联的特殊优惠项目,我设置与yes的值称为mainitem项目结构的关键,相关的主要项目所有后续的优惠项目有关键mainitem设定为no并有另一个名为mainitemid的键(这是mainitem uid)。

如果有人删除主要项目,我需要确保删除任何相关的特别优惠项目。这是我遇到的麻烦,不能完全解决如何找到他们。

我使用form.itemID提供要删除的产品的项目标识。我需要确保被删除的项目是主要项目;如果是这样,请通过购物车的其余部分循环查找与mainitemid等于form.itemid的任何物品并将其删除,然后删除mainitem

mainitem被定义为session.mycart[i].mainitem maintitenid被定义为session.mycart[i].mainitemid

<cfloop index="i" from="1" to="#arrayLen(session.mycart)#"> 

</cfloop> 

我需要建立两个循环或者我可以用一个做到这一点?我不确定我的条件性陈述。当然

回答

1

解决方案OP的具体问题

修订,以提供更完整的解决方案

<!--- First of all, will need to go 1 to ArrayLen() to find the main item id. Doesn't matter what order you go here, but main items should be inserted before children so may as well start at the bottom ---> 
<cfloop index="i" from="1" to="#ArrayLen(session.mycart)#"> 
    <!--- Is this the main item? ---> 
    <cfif session.mycart[i].id EQ form.itemID> 
     <!--- It's found the item, is it a main item? We've found the item so entering here to break the loop ---> 
     <cfif session.mycart[i].mainitem> 
      <!--- Go from ArrayLen() to 1, as if you go 1 to ArrayLen() when deleting you confuse Coldfusion ---> 
      <cfloop index="j" from="#ArrayLen(session.mycart)#" to="1" step="-1"> 
       <cfif session.mycart[j].id EQ form.itemID OR session.mycart[j].mainitemid EQ form.itemID> 
        <cfset ArrayDeleteAt(session.mycart,j)> 
       </cfif> 
      </cfloop> 
     </cfif> 
     <!--- This loop is done, so break out ---> 
     <cfbreak> 
    </cfif> 
</cfloop> 

在你的帖子,你的国家,你是从索引1索引ArrayLen循环(.. )。如果你正在从数组中删除项目,Coldfusion有点直接,并没有真正注意,所以当你删除5元素数组的元素2时,数组变成4个元素(因为你删除了一个)和索引3的元素现在是索引2,因此它是错过的。

解决方法是从最后开始并向后工作。只要你一次删除最多1条记录,那么这是完全有效的,因为它会继续减少它正在检查的索引,直到你下降到1,这将是第一个元素。

这样你可以通过元素5,4,3,2删除元素2,然后它将检查索引1,它现在仍然是与启动循环时相同的项目,因此不会跳过经验丰富。

如何处理这个

我误解了问题,或者在写这一点,得到了编辑的一些Blurb的,但下面适用所以在这里留下它仍然

你有没有考虑将主要商品的特价商品作为子商品,然后它将整个商品封装在一起,并且删除父商品将删除该商品。我有一个类似的问题,其中项目具有与它们相关的选项,并允许观察层次结构,我决定创建一个与参考数组结合的树。正如一个粗略的例子来说明的原则,来看看这个

<cfscript> 
    // Create a main item structure 
    stcMainItem = { 
     id = CreateUUID(), 
     somekey = somevalue, 
     someotherkey = someothervalue, 
     cost = 123 
    }; 

    // Create some child item structures, special offers for ex 
    stcSpecialOfferItem1 = { 
     id = CreateUUID(), 
     parent = stcMainItem.id, 
     cost = 45 
    }; 

    stcSpecialOfferItem2 = { 
     id = CreateUUID(), 
     parent = stcMainItem.id, 
     cost = 45 
    }; 



    // Each is added to a reference array 
    arrItemReference = []; 
    arrItemRefernce.add(stcMainItem); 
    arrItemRefernce.add(stcSpecialOfferItem1); 
    arrItemRefernce.add(stcSpecialOfferItem2); 

    // Now you decide to delete the main item and direct parents 
    strIDToDelete = stcMainItem.id; 
    for (i=ArrayLen(arrItemReference);i>=1;i--) { 
     if (
      arrItemReference[i].id == strIDToDelete 
      || 
      arrItemReference[i].parent == strIDToDelete 
     ) { 
      ArrayDeleteAt(arrItemReference,i); 
     } 
    } 
</cfscript> 

在我实际的代码中,我用的方法创建一个Item.cfc的方式做到了这一点,以应对上述和级联起来删除孙辈等,但原则是健全的。基本上你有一些方法可以让项目作为平面数组和父母,孩子和兄弟姐妹的层次结构进行公开。

信息化点

顺便说一句,你不停地交换“阵列”和“结构”当两者不同,如PHP语言,其中阵列用于参考,这两个方面略有不同。数组包含数值索引从1到n的值,而结构是一个保存与任意键有关的值的对象。不同的原因是它们不构建相同的基本Java对象,因此一些在一个方法上工作的方法不能在另一个上工作。

+0

啊!你刚刚救了我的理智!谢谢你1你的评论说,你从上到下,但实际上从下到上是正确的,再次感谢你! – 2012-02-22 23:40:42

+0

在那里,澄清了一点,底部= 1和顶部= ArrayLen(),没有歧义 – 2012-02-22 23:44:17

+0

真是一个很好的解释!谢谢!你的代码很棒! – 2012-02-22 23:59:01

0
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#"> 
    <cfif session.mycart[i].mainitem eq "no" AND session.mycart[i].mainitemid EQ form.itemid> 
     <cfset arrayDeleteAt(session.myCart,i) /> 
     <cfset i = i - 1 /><!--- this is necessary to keep you from running out of the end of the loop ---> 
    </cfif> 
</cfloop> 

这个心不是测试,你可以与它合作,把您的具体变量名中,但我认为它应该让你那里。如果我误解了这个问题,请告诉我。

+0

我试过上面的方式,但它并没有删除每一个关联的产品,我似乎得到2剩下四个?我现在正在玩它,并会发布我的问题 - 谢谢 – 2012-02-22 21:16:07

0

我没有看到在一个循环中做到这一点的方法,因为我认为它是一个两步过程。首先,您必须查看您删除的项目标识是否为主项目,然后返回数组并删除其余相关项目。

根据需要添加StructKeyExists()。

<!--- Find Main Item ---> 
<cfset isMainItem = false /> 
<cfloop from='1' to='#ArrayLen(Session.MyCart)#' index='item'> 
    <cfif Session.MyCart[ item ].ItemID EQ Form.ItemID AND Session.MyCart[ item ].MainItem EQ 'Yes'> 
     <cfset isMainItem = true /> 
     <cfbreak /> 
    </cfif> 
</cfloop> 

<cfif isMainItem> 
    <!--- Clean up special offer items ---> 
    <cfloop from='1' to='#ArrayLen(Session.MyCart)#' index='item'> 
     <cfif Session.MyCart[ item ].MainItemID EQ Form.ItemID AND Session.MyCart[ item ].MainItem EQ 'No'> 
      <cfset ArrayDeleteAt(Sesion.MyCart, Item) /> 
      <cfset item-- /> 
     </cfif> 
    </cfloop> 
</cfif> 
+0

我也试过这种方法,但它仍然在车中留下两个产品?不确定为什么循环停止? – 2012-02-22 21:18:21

+0

只是不会循环通过唯一的错误,我得到的是位置4的元素无法找到。我添加了'struckkeyexists'来确保mainitemid的存在,它从三个特价商品中删除了两个商品,只是停止了? – 2012-02-22 22:46:07