I have a weblog with multiple entries. I need to add up all the entries and divide by the total results
my weblog is “tracking” and my custom field is {points}
{exp:weblog:entries orderby="date" sort="desc" limit="999" weblog="tracking" parse="inward"}
{exp:simple_math calculate="{points} / {total_results}"}
{/exp:weblog:entries}
since I have multiple entries (limit =99) how do I add up each entry to give em a total and then divide my the total results?
Hi, What’s the problem? Your code does not work?
The code will work for a single entry.
Say I have 3 entries in the weblog “Tracking” and each entry is 10, 20 and 25 in the custom field {points}
What I need to display is the sum of all entries divided by the number of entries. So in the above example I need the template to display 15.
(which is 3 entries, Add up the sum of all entries = 45 divided by the total entries which is 3 that will output the total which is 15)
my code
{exp:weblog:entries orderby="date" sort="desc" limit="3" weblog="tracking" parse="inward"}
{exp:simple_math calculate="{points} / {total_results}"}
{/exp:weblog:entries}
just gives me .3.33 6.66 8.33 (1 result for each entry, I need one result for all entries) sum of all entries being displayed divided by the total entries being displayed
Thanks for the quick reply and for kindly sharing Simple Math and other addons with the EE community. Although I’m sorry to hear that a tag pair isn’t in your plans, I’m sure Simple Math will continue to be helpful to many folks. I may try contacting you directly in case you are available for paid work to extend Simple Math.
I’m going to post my problem below in hopes that you or fans of Simple Math have an alternate solution. I’d appreciate any advice on how to approach my problem.
HOW TO CALCULATE A VALUE FOR USE AS AN INPUT PARAMETER IN ANOTHER PLUGIN? I need to calculate a new height for images that were uploaded via an NGen File field in a FF Matrix. The trick is that the calculated height needs to be available high in the parse order so it can be used as an input parameter for resizing with imgsizer. The height calculation uses two additional fields within the same FF Matrix row as well as another field in the same weblog.
I’d hoped to nest Simple Math with two other plugins: Imgsizer and ZM_image_dimensions. If Simple Math had tag pairs I might be able to do something like the following:
{ff_matrix_field}
{exp:zm_image_dimensions url="http://mydomain.com{ngen_image}" parse="inward"}
{exp:simple_math calculate="round({result_height_from_zm_image_dimensions}*{scaling_factor}/10*{length_in_one_unit}/{length_in_another_unit})" parse="inward"}
{exp:imgsizer:size src="{ngen_image}" height="{result_from_simple_math}" quality="90"}
{sized}
{/exp:imgsizer:size}
{/exp:simple_math}
{/exp:zm_image_dimensions}
{/ff_matrix_field}
I’m not sure how else to calculate a new height and use it as a parameter in the opening imgsizer tag.
I thought about getting the original image size with PHP’s getimagesize and making the calculation in PHP too… but I’d seemingly have to set the PHP to output so that I could access the needed image path and other fields in the same FF Matrix row. With PHP on output, however, the calculated height would be too low in the parse order to be used in the opening imgsizer tag. I could just resize the images with PHP, but then I’d lose the caching and other advantages of imgsizer.
I feel like I’m missing a key element to how EE works. Is there some standard way to take the result of a Simple Math calculation and hold it as a temporary variable for use lower in the template code as an parameter in another plugin tag? Or, alternatively, is there an add-on that helps with controlling variables and parse order for my problem?
I’d appreciate any advice on how to pursue this problem. My PHP coding skills are minimal at this point, but I might be able to ask a programmer friend to help out. Thanks.
Laisvunas,
Thanks for creating the Math Plus plugin. It solved the problem I outlined above.
Is there any way to specify the number of decimal digits to round to like you can in PHP? For instance:
{exp:simple_math calculate="round(1.95583, 2)"}
would output 1.96
Would your Math Plus plugin work for this? Or do I have to do this in PHP?
This would be really helpful for calculating prices. Like this:
{exp:simple_math calculate="round( {price} * {qty}, 2 )"}
Whops!
I just realized it does work like this… I had some syntax wrong but now it’s working exactly as I outlined above. The reason I was confused is because rounding a whole number to 2 decimals doesn’t add ‘.00’ to the number like I was hoping it would.
For instance:
{exp:simple_math calculate="round(36, 2)"}
outputs “36” not “36.00”
Is there any way I can do this through this plugin?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.