1 Reply Latest reply on May 20, 2006 10:04 PM by noah_britton

    Simple Shopping Cart

      I am trying to develop a simple shopping cart.
      I have created an array of structures for this as follows.
      <cfif not Isdefined("Session.ShoppingCart")>
      <cfset SESSION.shoppingcart = ArrayNEW(1)>

      <cfset additem = StructNew()>
      <cfset additem.id = FORM.id>
      <cfset additem.name = FORM.name>
      <cfset additem.color = FORM.color>

      <cfset arrayappend(session.shoppingcart, additem)>

      I would like to delete a given entry in the array given a particular FORM.id, say id = 220
      Here is my code to try and delete the item
      <cfloop from="1" to="#ArrayLen(SESSION.shoppingcart)#" index="i">
      <cfif SESSION.shoppingcart .id eq FORM.id>
      <cfset foo = structdelete(SESSION.shoppingcart
      , id)>

      This is not working. I guess perhaps I should delete the array entry, but I can't figure out how to do this. Thanks in advance. -Noah
        • 1. Re: Simple Shopping Cart
          noah_britton Level 1
          Duh, I figured it out.
          <cfset bar = arraydeleteat(SESSION.shoppingcart, i)>
          I am having a problem when looping not sure why. I think because I'm deleting an item in the array it tries to loop to that index in the array. It will say "The element at position 3 cannot be found". I guess I normally wouldn't have to delete multiple positions in the array with a normal shopping cart unless they selected two different id's. I'll work on a more advanced shopping cart and post again.