Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum of a table column - SMARTY

Tags:

smarty

I have a multidimensional associative array, which I pass to a smarty template. I use below code in my smarty template to generate the shown two tables in my browser.
The smarty code:

<!--language:smarty-->
{foreach from=$allarr key=header item=table}
            <h5>{$header}</h5>
            <table> 
                <tr>
                    <td>{t}Emp #{/t}</td>
                    <td>{t}Employee Name{/t}</td>
                    <td>{t}Amount{/t}</td>
                </tr>
                    {foreach from=$table item=sub}   
                            <tr>
                                    <td>{$sub.emp_num}</td>
                                    <td>{$sub.user_name}</td>
                                    <td>{$sub.amount}</td>
                            </tr>
                    {/foreach}
                <tr>
                    <td colspan="2">{t}Variation Total{/t}</td>
                    <td colspan="1">{t}+++{/t}</td>
                </tr>
            </table>
{/foreach} 

The tables:

Table One
________________________________
Emp #   |Employee Name  |Amount |
________|_______________|_______|
1000001 | Test User     | 775.00|
26      | user1         | 555.00|
________|_______________|_______|
Variation Total         | +++   |
________________________|_______|

Table Two
______________________________
Emp #   |Employee Name| Amount|
________|_____________|_______|
1000001 | Test User   | 110.00|
________|_____________|_______|
Variation Total       | +++   |
______________________|_______|

What I want, is to replace the "+++" with the actual sum of the values in each Amount column. Nothing seems to work. Can anyone help? Thanks in advance.

like image 512
Les_Salantes Avatar asked Jan 29 '26 15:01

Les_Salantes


1 Answers

Figured out a way. :) The complete code is below.

{foreach from=$allarr key=header item=table}
       <h5>{$header}</h5>
       <table> 
          <tr>
               <td>{t}Emp #{/t}</td>
               <td>{t}Employee Name{/t}</td>
               <td>{t}Amount{/t}</td>
         </tr>
         {foreach from=$table item=sub}   
         <tr>
               <td>{$sub.emp_num}</td>
               <td>{$sub.user_name}</td>
               <td>{$sub.amount}</td>
         </tr>
        {assign var="sum" value="`$sum+$sub.amount`"}
        {/foreach}
        <tr>
              <td colspan="2">{t}Variation Total{/t}</td>
              <td colspan="1">{t}{$sum}{/t}</td>
              {assign var="sum" value=0}
       </tr>
    </table>
{/foreach}
like image 120
Les_Salantes Avatar answered Feb 01 '26 05:02

Les_Salantes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!