Re: [SMARTY] section sum of numbers...
boots <[email protected]>
| Newsgroups | gmane.comp.php.smarty.general |
|---|---|
| Message-ID | <[email protected]> |
Thanks! I posted it because I wanted to highlight the fact that sometimes simple problems need simple solutions. As for the "more general reporting solution", the current version is mostly workable but it does have a bad grouping bug and I would say that its overall design is not the most elegant. I am working on-and-off on an improved version. If you have comments on the current one or ideas for a future one, please post them in the related thread at the forums (in the plugins forum, titled Banded Report Generator). Greetings. --- Jochem Maas <[email protected]> wrote: > just saw your reply, just wanted to complement you on the rather > elegant example/snippet... it definitely has me thinking about new > ways > of doing things / looking at problems (especially regarding Smarty) > > :-) > > now to look up your 'more generalized reporting tool' > > rgds, > Jochem > > boots wrote: > > 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 > > > .... > __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- Smarty General Mailing List (http://smarty.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php