【问题标题】:Coldfusion Calculate Sum Total (Loop? )Coldfusion计算总和(循环?)
【发布时间】:2011-10-20 03:25:13
【问题描述】:

好的。所以我有这张桌子:

|项目|数量|价格|

|苹果| 2 | 2.00 |
|橙色 | 3 | 1.50 |
|葡萄 | 5 | 2.50 |

我想显示客户必须支付的总计。 怎么做? enter code here 我真的不知道如何使用数组。谁能告诉我怎么做?

我的代码(有点)

价格显示在使用此查询的每一行中:

<cfquery name="getPrice" datasource="fruits">
select *
from fruits
</cfquery>

<cfloop query="getPrice">
  #quantity# | #price# | #totalPrice#
</cfloop><br>

总计应显示在最后一行(总计 =$ 21.00)。

感谢您的帮助。

【问题讨论】:

    标签: sql arrays coldfusion


    【解决方案1】:
    <cfset grandTotal = 0 />
    
    <cfloop query="getPrice">
        #quantity# | #price# | #totalPrice#<br />
        <cfset grandTotal = grandTotal + ( price * quantity ) />
    </cfloop>
    
    <br /><br />
    
    <cfoutput>#grandTotal#</cfoutput>
    

    【讨论】:

    【解决方案2】:

    如果您想要的只是总计,您可以在 SQL 中执行此操作,而无需将记录循环为:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 2023-04-03
      • 2021-08-05
      相关资源
      最近更新 更多