Reply to Re: [SMARTY] section sum of numbers...

Your name:

Reply:


Posted by boots on 07/21/05 04:24

If you don't feel like building a bunch of objects to get simple sums
from normal recordsets you may enjoy this cheeky solution:

Define the following function (you can delcare it as a modifier if you
wish, but it is not necessary to do so):

function sum_records($v=0, $k=0, $key=null)
{
static $a=0;
if ($key == null) {
$b = $a;
$a = 0;
return $b;
} else {
$a += $v[$key];
}
}

and assume that $data is an array of records. Then to sum for a given
field (say 'year' and 'sales') in the template:

{$data|@array_walk:'sum_records':'year'|sum_records}
{$data|@array_walk:'sum_records':'sales'|sum_records}

You can hide some of the details by creating a proper modifier, perhaps
ending with: {$data|@sum_on:'year'} but I will leave that as a trivial
excercise :)

I think I would like the Iterator approach more if ArrayAccess was also
implemented; I'm not big on using property notation for fields in
templates but I guess that's just me :)

FWIW, awhile ago I posted a more generalized reporting tool at the
forum though it is presently being upgraded so as to be more OOP like
in the backend while hopefully remaining template like in the
front-end. In the meantime, it works well enough to solve simpler cases
like this and also provide in-template sorting, etc.

xo boots

--- Jochem Maas <jochem@iamjochem.com> wrote:

> messju mohr wrote:
> > On Wed, Jul 20, 2005 at 01:54:15PM -0500, Andras Kende wrote:
> >
> >>Hello,
> >>
> >>Is there a way to find out the SUM of the numbers from the template
> ?
> >
> >
> > it's possible to do this with {assign} and {math}, but it's
> > recommended to do this kind of logic from php and just assign the
> > result.
> >
>
> now I can't remember whether the lastest version of Smarty
> allows foreach Iterator overloading on objects yet
> (still running a small hack locally) but this is a nice example of
> why I would like to see more OO capability. assume it was a forach
> loop
> (I never use section, always seems awkward somehow):
>
> ------------->8-------------
> <table>
> {foreach name=i from=$items item=item}
> <tr>
> <td>{$item->name}</td>
> <td>{$item->price}</td>
> </tr>
> {/foreach}
> </table>
> There items: {$items->basketCount()} {* takes quantities into account
> *}
> Total price: {$items->getTotal()} {* sums everything, discount
> aware *}
> ------------->8-------------
>
> note $items is an object. :-)
>
> rgds,
> Jochem
>
> >>-------------
> >>
> >>{section name=i loop=$items}
> >> <tr>
> >> <td>{$items[i].name}</td>
> >> <td>{$items[i].price}</td>
> >> </tr>
> >>{/section}
> >>
> >>There items: {$smarty.section.i.total}
> >>Total price: {$smarty ??? }
> >>
> >>-------------
> >>
> >> <tr>
> >> <td>Joe</td>
> >> <td>2.00</td>
> >> </tr>
> >> <tr>
> >> <td>Steve</td>
> >> <td>5.00</td>
> >> </tr>
> >>
> >>There items: 2
> >>Total price: 7.00
> >>
> >>-------------
> >>
> >>Thanks,
> >>
> >>Andras Kende
> >>http://www.kende.com/
> >>
> >>--
> >>Smarty General Mailing List (http://smarty.php.net/)
> >>To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация