Chad & Qing Xia:

Thanks for your responses. You are both correct. 
But I still can't figure out how to fix it.

Chad:
I used cfdump in the loop and what is happening is that the
session.shopping array  index is  decreasing  by 1 every time one
structure is deleted.
Which makes sense. When the index is off it can't find the others. 

Qing Xia:
This statement is correct.
"I bet the error happens only if item 3 is selected. If you do 1 and 2 
then
you'll get the original number 2 remaining in the array."
However, I don't understand how this would work:
"So maybe you can figure out which array index values are NOT selected for
deletion, clear out the original array and insert the un-deleted array 
elements back in there."

I have tried to compensate for that in the loop but nothing works so far.
It seems I  need to decrease the session.shopping [i]  by 1 every time it 
loops. 
Or maybe there is another way to accomplish this?

I tried the array resize function and did not work.
<cfset private.arraycount = #arrayLen(session.shoppingcart)#>
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">               
                                
  <cfif StructKeyExists(rc,"itemid" & i)> 
  <cfset private.temp = arrayDeleteAt(session.shoppingcart,i)>
  <cfset private.arraycount = private.arraycount - 1> 
  <cfset private.resize = 
ArrayResize(session.shoppingcart,private.arraycount)>
  </cfif>
</cfloop>       

Thanks for any insights.

Jaime 


Chad Gray wrote:

My best guess is if you remove one item it changes the structure of the 
array and when it goes to remove the next one it cannot be found?

Use CFDump to display the array as you are looping over it to help debug.  
This way you can see the way the array is changing as you are removing 
items.



-----Original Message-----
From: jrmuniz [mailto:[email protected]]
Sent: Tuesday, January 12, 2010 4:16 PM
To: cf-talk
Subject: Problem deleting structure in an array

Hello:

Sorry, I forgot the subject in my last post.

I'm having an issue deleting a form structure that is in a shopping cart
array.
I'm sure it's something simple I'm doing wrong, but it's driving me crazy.

I have a checkbox to select which items to delete.
If only one item is selected there is no error and it works fine.
If more than one item is selected then the error is triggered.
Strangely, if I resubmit the form after the error it works OK.

If I select two items to delete the error is:
The error message is "          Element at position [3] doesn't exist in
array"

Here is the form code:
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">
<tr>
   <td>
   <cfinput type="checkbox" value="#i#" name="itemid#i#">
   </td>
   <td>
   <cfinput type="text" name="quantity#i#"
value="#session.shoppingcart[i].quantity#" size="3">
   </td>
   <td>
   <cfinput type="text" name="catalogno#i#"
value="#HTMLEditFormat(session.shoppingcart[i].catalog)#" size="10">
   </td>
   <td>
   <cfinput type="text" name="description#i#"
value="#HTMLEditFormat(session.shoppingcart[i].description)#"
size="40"></td>
   <td>
   <cfinput type="text" name="price#i#"
value="#session.shoppingcart[i].price#" size="3">
   </td>
</tr>
</cfloop>

Here is the cfc code:
<cfloop from="1" to="#arrayLen(session.shoppingcart)#" index="i">

 <cfif StructKeyExists(rc,"itemid" &  i)>
        <cfset private.temp = arrayDeleteAt(session.shoppingcart,i)>
   </cfif>
</cfloop>

Any help is greatly appreciated.

Regards,
Jaime



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:329611
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4

Reply via email to