Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Adding rows

New Here ,
Feb 01, 2011 Feb 01, 2011

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!

1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Feb 01, 2011 Feb 01, 2011

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.

Translate
Contributor ,
Feb 01, 2011 Feb 01, 2011

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 01, 2011 Feb 01, 2011

&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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 01, 2011 Feb 01, 2011

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 01, 2011 Feb 01, 2011

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 01, 2011 Feb 01, 2011

Also a valid option.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 01, 2011 Feb 01, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 01, 2011 Feb 01, 2011

Thanks for your help guys, and your patience. Sorry for coming off ignorant to basic Programming 101 skills, but that's probably because I am. I'm a graphic artist and only work with ColdFusion once a year to do lists and update forms. I've never used CFLOOP or ARRAYS, so unfortunately you'll need to elaborate more completely in order for me to follow your logic

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 01, 2011 Feb 01, 2011

I'm sorry - but this forum isn't really the place for you then.  We've given you good suggestions - hell we've even written the code for you.  I'd suggest reading the documenation or taking a class if you're still stuck.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Feb 01, 2011 Feb 01, 2011
LATEST

ilssac wrote:


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

<cfset totalProjectCost = totalProjectCost + Project_Cost>

And if I could tell my left hand from my right hand, I would have properly said "...use the variable 'TotalProjectCost' on the right side...".  It was the undefined varaible in the expression 'totalProjectCost + Project_Cost' that was throwing the exception.  Because if totalProjectCost is undefined what the heck can ColdFusion add to the value of Project_Cost.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources