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

Smarty Math Foreach

 
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
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Fri May 04, 2012 12:08 pm    Post subject: Smarty Math Foreach Reply with quote

Hi,

Not sure if this is possible but what I am trying to do is add some amounts within a foreach loop and create some subtotals. The problem is that I need to create a number of subtotals from within the foreach loop.

The foreach loop looks like this,

{foreach from=$lines item=entry}
{$entry->id}
{$entry->amount}
{/foreach}

And an example of the output would be this,

Job ID Amount
482 €30.00
482 €60.00
482 €90.00
489 €30.00
489 €60.00
490 €30.00

What I need is it to look like this,

Job ID Amount
482 €180.00
489 €90.00
490 €30.00

So you can see where the Job ID's are the same I want to add the amounts to create a sub-total.

I can get a running total using this math,

{math assign="amount" equation="x + y" x=$amount|default:0 y=$entry->amount|default:0}

But that doesnt provide the sub total I am after.

Can anyone advise if they think this is even possible within a foreach loop.

Thanks

Chris
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 674
Location: Germany, border to Switzerland

PostPosted: Fri May 04, 2012 1:16 pm    Post subject: Reply with quote

In Smarty3 you can achieve your goal quite easily:

Code:
{$values = [
  [482, 30.00],
  [482, 60.00],
  [482, 90.00],
  [489, 30.00],
  [489, 60.00],
  [490, 30.00]
]}
{$sums = []}
{foreach $values as $value}
  {if $value@first}<ul>{/if}

  <li>{$value.0} €{"%0.2f"|sprintf:$value.1}</li>

  {if !isset($sums.{$value.0})}
    {$sums.{$value.0} = {$value.1}}
  {else}
    {$sums.{$value.0} = $sums.{$value.0} + {$value.1}}
  {/if}

  {if $value@last}</ul>{/if}
{/foreach}

{foreach $sums as $id => $sum}
  {if $sum@first}<ul>{/if}
  <li>{$id} €{"%0.2f"|sprintf:$sum}</li>
  {if $sum@last}</ul>{/if}
{/foreach}


In general computations should be done in PHP not in Smarty, though.
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Fri May 04, 2012 2:07 pm    Post subject: Thanks Reply with quote

Hi,

Thanks for the reply. Unfortunately I am using CMSMS which currently only uses Smarty2.

I found this plugin, http://smarty.incutio.com/?page=foreachgroup, called foreachgroup which I thought could be useful. However, I cannot get it to work.

Is there anyway to achieve the same result in Smarty2?

Thanks

Chris
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