cfloop will only evaluate the listLen value once (example: cfloop from 1 to 7).
...
<cfset CLIENT.CartMerchList = "1,2,3,4,5,6,7">
<cfloop from="1"
to="#ListLen(CLIENT.CartMerchList)#" index="i">
....
When you delete elements, the list length changes. Say you delete two elements, the new list length = 5. So at a certain point, an error is thrown because the code is trying to delete elements that no longer exist. Example
<cfset myList = "1,2,a,4,a,6,a">
<cfoutput>
list length = #listLen(myList)#<br>
<cfloop from="1" to="#listLen(myList)#" index="x">
<cfif x mod 3 eq 0>
<cftry>
<cfset myList = listDeleteAt(myList, x)>
<cfcatch><!--- ignore ---></cfcatch>
</cftry>
</cfif>
pos=[#x#]
<cfif x lte listLen(myList)>
#listGetAt(myList, x)#
<cfelse>
error, this element no longer exists
</cfif>
<br>
</cfloop>
</cfoutput>
|
The solution is to traverse the list
in reverse. So the list positions will always exist.
<cfloop from="#ListLen(CLIENT.CartMerchList)#" to="1" step="-1" index="i">
....
</cfloop>
|
That said, IMO lists are not the best choice for this scenario. Most list functions ignore empty elements, which could lead to an incorrect shopping cart total or result in wrong items being ordered. A stray comma in the values could do the same thing. IMO arrays are a better choice.