Get a subtotal and total out of a cfloop index

TSellers
TSellers used Ask the Experts™
on
I have a loop that builds a table for as many records as there are in the recordcount. Now I would like to:
1) Take the ExtPrice and make a subtotal
2) Multiply the subtotal by .07 to get tax.
3) Add the tax to subtotal to get total.

I assume I need to assign values to a variable during each pass of the loop? Would this be hard to do?

This is what the results presently look like:

SKU ProductName Brand Unit Price Qty ExtPrice
 
11072 100% Trophic $15.19 2 $30.38  
11723 ALFALFA Trophic $10.07 2 $20.13  
11912 APPLE Trophic $9.11 2 $18.23  

and here is the code that does it:

<cfloop index="x" from="1" to="#SearchResult.recordCount#">
     <cfif SearchResult.ResultField5[x] is not 0>
          <tr><td>#SearchResult.ResultField1[x]#</td>
          <td>#SearchResult.ResultField2[x]#</td>
          <td>#SearchResult.ResultField3[x]#</td>
          <td>#DollarFormat(SearchResult.ResultField4[x])#</td>
          <td>#SearchResult.ResultField5[x]#</td>
          <td>#DollarFormat(SearchResult.ResultField5[x]*SearchResult.ResultField4[x])#</td><TD></TD>
          </TR><br>
         
     </cfif>
</cfloop>
 
Regards, Tom
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
<cfset subtotal = "">
<cfset tax  = "">
<cfset total = "0">

<cfset grandtotal = "0">
 
<cfloop index="x" from="1" to="#SearchResult.recordCount#">
    <cfif SearchResult.ResultField5[x] is not 0>
         <tr><td>#SearchResult.ResultField1[x]#</td>
         <td>#SearchResult.ResultField2[x]#</td>
         <td>#SearchResult.ResultField3[x]#</td>
         <td>#DollarFormat(SearchResult.ResultField4[x])#</td>
         <td>#SearchResult.ResultField5[x]#</td>
         <td>#DollarFormat(SearchResult.ResultField5[x]*SearchResult.ResultField4[x])#</td><TD></TD>

       <cfse subtotal = SearchResult.ResultField3[x]*SearchResult.ResultField4[x])#>
       <!--- i assume it will SearchResult.ResultField3 instead of SearchResult.ResultField5 --->
       <cfset tax  = .07 * subtotal>
       <cfset total = subtotal+ tax>
       <td>#total#</td>
       
       <cfset grandtotal = grandtotal + total>
       


         </TR><br>
   
    </cfif>
</cfloop>

show grand total at below

Author

Commented:
Thanks Dash:

I've tried it a few different ways, I still get the following error:
----------------------
Just in time compilation error

Invalid parser construct found on line 91 at position 50. ColdFusion was looking at the following text:

*
-----------------------

It seems to relate to:
<cfset subtotal = SearchResult.ResultField4[x]*SearchResult.ResultField5[x]>

-I changed the ResultField3 to ResultField5 because 3 was not a numeric value, it was 'brand'.
Commented:
just use evaluate function.

 
<cfset subtotal = #evaluate( (SearchResult.ResultField4[x]) * (SearchResult.ResultField5[x]) )#>

Author

Commented:
Thanks, I just had to place some "" around the .07 and that did it. Now I'll just have to go off and study up on why it works!

Commented:
Thanks TSeller.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial