1

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.

dygta
  • 81
  • 3
  • 7

2 Answers2

7
<cfset grandTotal = 0 />

<cfloop query="getPrice">
    #quantity# | #price# | #totalPrice#<br />
    <cfset grandTotal = grandTotal + ( price * quantity ) />
</cfloop>

<br /><br />

<cfoutput>#grandTotal#</cfoutput>
charliegriefer
  • 3,342
  • 1
  • 18
  • 20
  • Thank you. u've been a great help! – dygta Oct 20 '11 at 04:25
  • 3
    @dygta: Don't forget you can "accept" the answer that helped you the most. See also [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – ale Oct 20 '11 at 12:42
4

If ALL you want is the grand total, you can do that in SQL without looping over the records as:

<cfquery name="getPrice" datasource="fruits">
  select sum(price*quantity) as grandTotal
  from fruits
</cfquery>

Total: <cfoutput>#getPrice.grandTotal#</cfoutput>
Justin Scott
  • 865
  • 4
  • 10