Search code examples
arrayscoldfusionstructcoldfusion-9

ColdFusion Array And Struct


I have a shopping cart array stored in a session:Screen Shot of Shopping Cart Array

I give my user the ability to select all items to delete or select individual items via a checkbox.

I am sending the array index via form post to arrayDeleteAt.

Now if I select the bottom 3 items, it does not delete it.

Here is my delete code:

<cfif isDefined("form.leadId") AND  listLen(form.leadId)>
  <cfloop from="#listLen(form.leadId)#" to="1" step="-1" index="i">
    <cfset temp = arrayDeleteAt(session.shoppingcart, #i#)>
  </cfloop> 
</cfif>

Solution

  • You are going to have more issues with this method of managing your cart. After using ArrayDeleteAt the array's indexes will be recalculated, so when you will most likely delete the wrong item from the array, or you can get an error when you try and delete an item that is out of bounds.

    Is see that you are trying to get around this by working backwards through your list, and Dan is right for what your issue is with the code above, but if the list is passed in in an incorrect order then you are in a world of hurt.

    I would suggest instead of using an array, use a struct with a surrogate key, such as a UUID, then delete items by that key.