我有一个多维关联数组,我将其传递给 smarty 模板。我在 smarty 模板中使用以下代码在浏览器中生成显示的两个表。
聪明的代码:
<!--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}
Run Code Online (Sandbox Code Playgroud)
表格:
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 | +++ |
______________________|_______|
Run Code Online (Sandbox Code Playgroud)
我想要的是将“+++”替换为每个“金额”列中值的实际总和。似乎什么都不起作用。有人可以帮忙吗?提前致谢。
想出了一个办法。:) 完整的代码如下。
{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}
Run Code Online (Sandbox Code Playgroud)