I am in the process of putting together a shopping cart, I am holding the cart data in an array, inside the array is a struct which holds the individual product information. I need get the sum of the totalprice column within the struct, please see my dump below, I have tried
<cfset carTotal = ArraySum(session.mycart[ "totalPrice" ])>
but this creates an error
the value totalprice cannot be converted to a number
Is this because I am using arraysum
in a struct?
Any help would be appreciated, thank you.
If mycart
was a Query object, it'd be a simple ArraySum(mycart.totalPrice)
Since it's an array of struct, you've got to loop it yourself, which is still rather easy:
<cfset sum = 0>
<cfloop array="#session.mycart#" index="item">
<cfset sum += item.totalPrice>
</cfloop>
<cfdump var="#sum#">
Don't forget to use PrecisionEvaluate()
when you need full precision.