SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    generating a subtotal

    I'm new to ColdFusion, and I'm having problems trying to create a basic invoice system. I save each expense item as a separate row in the database, and this includes an item quantity and unit price. When I output the expense items on my page, I generate an item total by multiplying the quantity times unit cost. This total is only for that line. What I can't figure out is how to add up each line total and create a final total. The number of expense items varies based on invoice. Any help would be appreciated.

  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2007
    Posts
    584
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are a few ways. One is to run a QoQ at the end to get a grand total.

    <cfoutput query="yourQuery">
    ... display the items and totals ...
    </cfoutput>

    <!--- use QoQ to get the total --->
    <cfquery name="getFinalTotal" dbtype="query">
    SELECT SUM((Quantity * Cost)) AS FinalTotal
    FROM yourQuery
    </cfquery>

    <cfoutput>
    Final total = #getFinalTotal.finalTotal#
    </cfoutput>

    Another is to store a running total in a variable, and increment it inside your output loop

    <cfset finalTotal = 0>
    <cfoutput query="yourQuery">
    ... other code ...

    <cfset finalTotal = finalTotal + ( Quantity * Cost )>
    </cfoutput>
    <cfoutput>
    Final total = #finalTotal#
    </cfoutput>

    There are probably other options as well. If you are calculating the "ItemCost" in your sql query, just replace ( Quantity * Cost ) with "ItemCost".

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. I'll give this a try.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •