We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin: Simple Math

Development and Programming

Laisvunas's avatar
Laisvunas
879 posts
15 years ago
Laisvunas's avatar Laisvunas

Hi,

I released version 1.0.1 of Simple Math plugin.

In this version I suppressed PHP error messages.

       
Eastwood Design's avatar
Eastwood Design
605 posts
15 years ago
Eastwood Design's avatar Eastwood Design

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?

       
Laisvunas's avatar
Laisvunas
879 posts
15 years ago
Laisvunas's avatar Laisvunas

Hi,

What’s the problem? Your code does not work?

       
Eastwood Design's avatar
Eastwood Design
605 posts
15 years ago
Eastwood Design's avatar Eastwood Design
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

       
Laisvunas's avatar
Laisvunas
879 posts
15 years ago
Laisvunas's avatar Laisvunas

Hi,

I don’t know a plugin or combination of plugins which would allow to achieve the functionality you need. It seems that you need to write a dedicated plugin.

       
fjldude's avatar
fjldude
50 posts
15 years ago
fjldude's avatar fjldude

[UPDATE] I’ve changed my mind again, a tag pair might help in my case after all… so I’m still curious about tag pairs.

Any chance that Simple Math will add a tag pair option, instead of just the single tag? This would provide more flexibility to nest plugins as needed.

       
Laisvunas's avatar
Laisvunas
879 posts
15 years ago
Laisvunas's avatar Laisvunas

Hi,

I know that in certain cases tag pair would be useful, but currently I do not have the plans to rewrite this plugin.

       
fjldude's avatar
fjldude
50 posts
15 years ago
fjldude's avatar fjldude

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.

       
fjldude's avatar
fjldude
50 posts
15 years ago
fjldude's avatar fjldude

Laisvunas,

Thanks for creating the Math Plus plugin. It solved the problem I outlined above.

       
Alex Roper's avatar
Alex Roper
6 posts
15 years ago
Alex Roper's avatar Alex Roper

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 )"}
       
Alex Roper's avatar
Alex Roper
6 posts
15 years ago
Alex Roper's avatar Alex Roper

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?

       
Laisvunas's avatar
Laisvunas
879 posts
15 years ago
Laisvunas's avatar Laisvunas

It seems that you can achieve what you need by using together Math Plus and Add VAT plugins.

       
Alex Roper's avatar
Alex Roper
6 posts
15 years ago
Alex Roper's avatar Alex Roper

Thanks Laisvunas! That seems to work perfectly.

       
First 2 3 4

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.