Coldfusion Calculate Sum Total (Loop? )
ok. so i have this table:
|item| quantity| price|
|apple | 2 | 2.00 |
|orange | 3 | 1.50 |
|grape | 5 | 2.50 |
i want to display the Grand Total that a customer has to pay. how to do that? enter code here
i don't really know how to use array. can anyone show me how?
my code (sort of)
the price is shown in each of the row using this query:
<cfquery name="getPrice" datasource="fruits">
select *
from fruits
</cfquery>
<cfloop query="getPrice">
#quantity# | #price# | #totalPrice#
</cfloop><br>
the Grand Total should be displayed in the last row (Grand Total =$ 21.00 ).
Thanks for your help.
<cfset grandTotal = 0 />
<cfloop query="getPrice">
#quantity# | #price# | #totalPrice#<br />
<cfset grandTotal = grandTotal + ( price * quantity ) />
</cfloop>
<br /><br />
<cfoutput>#grandTotal#</cfoutput>
如果你想要的全部是总计,那么你可以在SQL中做到这一点,而不会循环记录为:
<cfquery name="getPrice" datasource="fruits">
select sum(price*quantity) as grandTotal
from fruits
</cfquery>
Total: <cfoutput>#getPrice.grandTotal#</cfoutput>
链接地址: http://www.djcxy.com/p/62062.html