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

Here's a function to sum the contents of an array column

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
leason
Smarty n00b


Joined: 09 Sep 2003
Posts: 2

PostPosted: Tue Sep 09, 2003 2:22 pm    Post subject: Here's a function to sum the contents of an array column Reply with quote

I wrote this little function to show me totals for items and their costs. Here is the usage:
Code:
{sum_col array=$myArray col="myCol"}


I am having a problem using modifiers with this function, however. I would like to be able to do this:
Code:
{sum_col array=$myArray col="myCol"|string_format:"%.2f"}

But when I do it gives me a value of zero regardless of what it should be. Anyone know why this is happening?

Anyways, the function will return the sum of all the values of myCol in $myArray. Here is the function:

Code:
<?PHP

/*********************
This is a smarty plugin that will sum the contents of a column in an array
*********************/

function smarty_function_sum_col($params, &$smarty){
    if(!isset($params['array']) || !isset($params['col'])){
        $smarty->trigger_error("sum_col: missing array or col value");
        return;
    }

    $result = 0;
   
    foreach($params['array'] as $row){
        $result += $row[$params['col']];
    }
   
    return $result;
}

?>


Hope you like it, this is my first function. I have only been using Smarty for about three hours now.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Sep 12, 2003 3:36 pm    Post subject: Reply with quote

i think zoran is right.

nevertheless:
your example {sum_col array=$myArray col="myCol"|string_format:"%.2f"}
applies the modifier to "myCol" (!).

if you want the modifier be applied to the plugins result do:
{sum_col|string_format:"%.2f" array=$myArray col="myCol"}

HTH
messju
Back to top
View user's profile Send private message Send e-mail Visit poster's website
leason
Smarty n00b


Joined: 09 Sep 2003
Posts: 2

PostPosted: Tue Sep 16, 2003 11:32 am    Post subject: Reply with quote

I suppose that depends on what you consider programming logic and what you consider presentation logic. If the sum of a column of numbers is for display, and has no value to the program itself, then why not make it possible to do that in the template?

However, I see your point and it is valid. I have since writing that function switched to computing the sum in PHP and passing the value to the template. My problem at the time was that I did not understand some of the finer points of the smarty engine (and still don't Very Happy ).

Thanks for your replies.
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 -> Plugins 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