SumTotal在每组经典asp结尾

时间:2013-01-14 12:10:49

标签: asp-classic

我要做的是在最后一行获得总和。这里有一组带有总和的两组itens的例子:

我有:

1000 (group number)

TitleUm   TitleTres   TitleQuatro
Apple     1           20
Pear      5           12

1001 (group number)

TV        1           20
Mobile    1           12
Car       1           15
Bicycle   1            5

 TOTAL   10           84

我正在寻找:

1000 (group number)

TitleUm   TitleTres   TitleQuatro
Apple     1           20
Pear      5           12

 TOTAL    6           32

1001 (group number)

TV        1           20
Mobile    1           12
Car       1           15
Bicycle   1            5

 TOTAL    4           52

下面的代码执行几乎所有操作,列出项目和每行的每个值,但它现在显示总和:

<table class="Itens">
<%
currentGroupName = ""
previousGroupName = ""
Do Until ItensList.EOF
currentGroupName = ItensList("oito")
Um= ItensList("um")
Tres= ItensList("tres")
Quatro= CCur(ItensList("quatro"))

If currentGroupName <> previousGroupName Then
%>

<tr>
<td><% Response.Write currentGroupName %></td>
</tr>
<tr>            
<th><% Response.Write TituloUm %></th>
<th><% Response.Write TituloTres %></th>
<th><% Response.Write TituloQuatro %></th>
</tr>

<%
End If
%>

<%
Um= CCur(ItensList("um"))
Tres= CCur(ItensList("tres"))
Quatro= CCur(ItensList("quatro"))
%>

<tr>
<td><% Response.Write Um %></td>
<td><% Response.Write Tres %></td>
<td><% Response.Write Quatro %></td>
</tr>

<%
previousGroupName = currentGroupName
ItensList.MoveNext
Loop
ItensList.Close
%>

<tr class="Total">
<td>TOTAL</td>
<td><% Response.Write ShowSumTotalTresHere %></td>
<td><% Response.Write ShowSumTotalQuatroHere %></td>
</tr>
</table>

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

将每个变量添加到循环内的变量中,如下所示:

<%
SumSomething = 0

Do Until objRs.EoF
     SumSomething = SumSomething + objRs("SomeValue")
objRs.MoveNext
Loop
%>
...
<tr>
  <td><%= SumSomething%></td>
...

答案 1 :(得分:0)

你需要在循环时保持一个运行总计,按下面的示例代码,我在循环之前声明两个新的变量:

Total_Tres          = 0
Total_Quatro        = 0

然后我在你的循环中增加这些:

'Maintain Running Total
Total_Tres          = Total_Tres + Tres
Total_Quatro        = Total_Quatro + Quatro

最后,输出总变量:

<td>TOTAL</td>
<td><% = Total_Tres %></td>
<td><% = Total_Quatro %></td>

完整示例(未经测试)

<table class="Itens">
<%
currentGroupName    = ""
previousGroupName   = ""
Total_Tres          = 0
Total_Quatro        = 0
Do Until ItensList.EOF
    currentGroupName    = ItensList("oito")
    Um                  = ItensList("um")
    Tres                = ItensList("tres")
    Quatro              = CCur(ItensList("quatro"))

    If currentGroupName <> previousGroupName Then
        %>
        <tr>
            <td><% = currentGroupName %></td>
        </tr>
        <tr>            
            <th><% = TituloUm %></th>
            <th><% = TituloTres %></th>
            <th><% = TituloQuatro %></th>
        </tr>
        <%
    End If

    Um      = CCur(ItensList("um"))
    Tres    = CCur(ItensList("tres"))
    Quatro  = CCur(ItensList("quatro"))
    %>
    <tr>
        <td><% = Um %></td>
        <td><% = Tres %></td>
        <td><% = Quatro %></td>
    </tr>
    <%
    '#### Maintain Running Total
    Total_Tres          = Total_Tres + Tres
    Total_Quatro        = Total_Quatro + Quatro

    '### Flag current group
    previousGroupName   = currentGroupName
    ItensList.MoveNext
Loop
ItensList.Close
%>
<tr class="Total">
<td>TOTAL</td>
<td><% = Total_Tres %></td>
<td><% = Total_Quatro %></td>
</tr>
</table>