|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Sat Aug 19, 2017 1:01 pm Post subject: Assign array within Smarty template |
|
|
Hello. I have a foreach loop in my Smarty template, & everything is going OK.
however now, I want to total / count the values for each category. But, when i do my foreach for my totals, i get a blank result.
Top / first part of the code
Code: | {foreach from=$v.data key=ka item=va}
{$ka} - {$va}<br />
{assign var=total.$ka value=$total.$ka+$va}
{/foreach}
|
Below that, I want to do a foreach on $total - however right now i'm getting a blank result. |
|
Back to top |
|
sarahfoxnz Smarty Regular
Joined: 11 Apr 2010 Posts: 76
|
Posted: Sat Aug 19, 2017 2:14 pm Post subject: |
|
|
couldnt figure it out - to have a running total as an array. so did it in my PHP script & created a second (unnecessary) array.
Ps a single $total is ok, but not $total.item1 $total.item2 etc...
EDIT: There are hundreds of different items... i want individual totals on...
(but only a few dozen items will be shown in the totals area at a time - depending on what my script needs. i dont know the names of the totals in advance) |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Sat Aug 19, 2017 3:47 pm Post subject: |
|
|
You are using Smarty2? |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sat Aug 19, 2017 4:05 pm Post subject: |
|
|
The following suggestion is taken from:
https://www.smarty.net/docs/en/language.function.assign.tpl
in the Comments section.
Try:
Code: | {assign "total" []}{* initializes to an array *}
{foreach from=$v.data key=ka item=va}
{$ka} - {$va}<br />
{$total.$ka += $va}
{/foreach} |
|
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|