Skip to main content
Known Participant
Feb 1, 2011 5:49 PM UTC
Answered

Adding rows

I've never done calculations in ColdFusion before so I don't even know where to start. I would like to put a total at the bottom of each row of returned values. How do I go about doing that? I know none of this right, but here's my code:

<cfoutput query="GetList">
     <tr>
      <td valign="top" id="text">#ProjectName#</td>
      <td valign="top" id="text">#Phase#</td>
      <td valign="top" id="text">#Target_Quarter#</td>
      <td valign="top" id="text">#DollarFormat(Project_Cost)#</td>
      <td valign="top" id="text">#DollarFormat(Q1_Costs)#</td>
      <td valign="top" id="text">#DollarFormat(Actual_q1_Costs)#</td>
     </tr>
</cfoutput>

Heres where I want to show the totals from the above values:

<cfoutput query="GetTotal">
     <tr>
      <td valign="top" id="text" align="right" colspan="3"><b>TOTALS:</b></td>
      <td valign="top" id="text2">#DollarFormat(Total.Project_Cost)#</td>
      <td valign="top" id="text2">#DollarFormat(Total.Q1_Costs)#</td>
      <td valign="top" id="text2">#DollarFormat(Total.Actual_q1_Costs)#</td>
     </tr>

</cfoutput>

Thanks!

    This topic is closed to new replies. Start a new post to keep the conversation going.
    ilssac
    ilssacCorrect answer
    Inspiring
    Feb 1, 2011 6:39 PM UTC

    Just like any variable, you have to define it before you can use it.

    Inorder to be able to use the variable 'TotalProjectCost' on the left side of this assignement.

    <cfset totalProjectCost = totalProjectCost + Project_Cost>

    You must define it.

    I.E. at the very beginning of your loop.

    <cfset totalProjectCost = 0>

    ...

    <cfloop query="GetList">

    Basic programmer 101 stuff.

    9 replies

    ilssac
    Inspiring
    Feb 1, 2011 5:59 PM UTC

    &lt;Bryan&gt; wrote:

    I've never done calculations in ColdFusion before so I don't even know where to start. I would like to put a total at the bottom of each row of returned values. How do I go about doing that? I know none of this right, but here's my code:

    <cfoutput query="GetList">
         <tr>
          <td valign="top" id="text">#ProjectName#</td>
          <td valign="top" id="text">#Phase#</td>
          <td valign="top" id="text">#Target_Quarter#</td>
          <td valign="top" id="text">#DollarFormat(Project_Cost)#</td>
          <td valign="top" id="text">#DollarFormat(Q1_Costs)#</td>
          <td valign="top" id="text">#DollarFormat(Actual_q1_Costs)#</td>

         </tr>

    </cfoutput>

    As you are looping over the record set, record the total of each itme.

         </tr>

         <cfset totalProjectCost = totalProjectCost + Project_Cost>

         etc.

    </cfoutput>

    Then in the last row, just output these totals.

    <cfoutput>

      <tr>

      ....

       <td>#DollarFormat(totalProjectCost)#</td>

       ...

       </tr>

    </cfoutput>

    Translate
    _Bryan_Author
    Known Participant
    Feb 1, 2011 6:27 PM UTC

    ilsaac - Tried your suggest but got this error: Variable TOTALPROJECTCOST is undefined.

    Is this how the code should look:

    <cfoutput query="GetList">
         <tr>
          <td valign="top" id="text">#ProjectName#</td>
          <td valign="top" id="text">#Phase#</td>
          <td valign="top" id="text">#Target_Quarter#</td>
          <td valign="top" id="text">#DollarFormat(Project_Cost)#</td>
          <td valign="top" id="text">#DollarFormat(Q1_Costs)#</td>
          <td valign="top" id="text">#DollarFormat(Actual_q1_Costs)#</td>
         </tr>
         <cfset totalProjectCost = totalProjectCost + Project_Cost>
         <cfset totalQ1_Costs = totalQ1_Costs + Q1_Costs>
         <cfset totalActual_q1_Costs = totalActual_q1_Costs + Actual_q1_Costs>
         </cfoutput>
         <cfoutput>
         <tr>
          <td valign="top" id="text" align="right" colspan="3"><b>TOTALS:</b></td>
          <td valign="top" id="text2">#DollarFormat(totalProjectCost)#</td>
          <td valign="top" id="text2">#DollarFormat(totalQ1_Costs)#</td>
          <td valign="top" id="text2">#DollarFormat(totalActual_q1_Costs)#</td>
         </tr>
         </cfoutput>

    Translate
    Participating Frequently
    Feb 1, 2011 6:37 PM UTC

    That's because they don't exist the first time thru the loop.  Declare the vars first outside the loop.  Or use arraySum() like I suggested.

    Translate
    Participating Frequently
    Feb 1, 2011 5:58 PM UTC

    Rather then a separate query you can use arraySum() on a query column to total up the values in your query.  Try this:

    <cfoutput query="GetList">
          <tr>
           <td valign="top" id="text">#ProjectName#</td>
           <td valign="top" id="text">#Phase#</td>
           <td valign="top" id="text">#Target_Quarter#</

    td>
           <td valign="top" id="text">#DollarFormat(Project_Cost)#</td>
           <td valign="top" id="text">#DollarFormat(Q1_Costs)#</td>
           <td valign="top" id="text">#DollarFormat(Actual_q1_Costs)#</td>
          </tr>
    </cfoutput>  

    <cfoutput>

    <tr>
    <td> </td>
    <td> </td>
    <td> </td>
    <td>#arraySum(GetList.Project_Cost)#</td>
    <td>#arraySum(GetList.Q1_Costs)#</td>
    <td>#arraySum(GetList.
    Actual_q1_Costs
    )#</td>
    </tr>

    </cfoutput>

    Translate