Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Total value of a collection?

Status
Not open for further replies.

DeZiner

Programmer
Joined
May 17, 2001
Messages
815
Location
US

I am running a loop to determine the quantity of items in the cart. Later I want to out put the total items in the cart. As your cart contains 10 items totalling $10.00. When I outout the results, obviously I am getting the your cart contains line for each set of items. how can I determinr the total number of items added together and out put them?

Code:
<cfloop collection=&quot;#session.cart#&quot; item=&quot;i&quot;>  

<cfset subtotal = subtotal + (session.cart[i][4] * session.cart[i][3])>
<form method=&quot;post&quot;>
	
Your Cart Contains 

#session.cart[i][4]# <cfif #session.cart[i][4]# EQ 1> item,<cfelse>items,</cfif>
</cfloop>

#session.cart[4]# is the quantity of each item choosen DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
ArraySum(array) - Returns the sum of values in an array. Sylvano
dsylvano@hotmail.com
 
if you do a loop over the structure like you have at the moment to get the actual value of the cart (Quantity*price) then when that has finished you can use structCount(Session.Cart) to get the number of enteries that are in the cart at that time so your code will look like:

<cfloop collection=&quot;#session.cart#&quot; item=&quot;i&quot;>

<cfset subtotal = subtotal + (session.cart[4] * session.cart[3])>
</cfloop>

<form method=&quot;post&quot;>

Your Cart Contains #StructCount(Session.Cart) <cfif #StructCount(Session.Cart)# EQ 1> item,<cfelse>items,</cfif> totalling #subtotal#
</form>

hope this helps !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top