Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Assign array within Smarty template

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sat Aug 19, 2017 1:01 pm    Post subject: Assign array within Smarty template Reply with quote

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
View user's profile Send private message
sarahfoxnz
Smarty Regular


Joined: 11 Apr 2010
Posts: 76

PostPosted: Sat Aug 19, 2017 2:14 pm    Post subject: Reply with quote

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
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Aug 19, 2017 3:47 pm    Post subject: Reply with quote

You are using Smarty2?
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Sat Aug 19, 2017 4:05 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP