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
16 years ago
Laisvunas's avatar Laisvunas

The attached add-on files may be out of date. Please visit Devot-ee for the latest versions.

Hi,

This plugin provides support for doing simple mathematical operations in templates.

There is already the plugin Mathematical Operations by Yuriy Salimovskiy which has the same purpose. My plugin is more powerful since it

1) allows more mathematical operations;

2) allows to input more than two numbers;

3) allows to control order of mathematical operations by using round brackets.

PARAMETERS

1) calculate - Required. Allows you to input a string consisting of numbers, symbols of mathematical operations, symbols of mathematical functions and round brackets, e.g calculate = “(10 + 5) / 3”. You can use such symbols of mathematical operations as: + (addition), - (subtraction), * (multiplication), / (division), % (division remainder), ++ (increment), – (decrement); also you can use symbols of mathematical functions, e.g. round(), floor(), ceil().

2) invalid_input - Optional. Accepts two values: “alert” and “silence”. Default value is “silence”. If the value is “alert”, then in cases when the plugin has some problem with parameters, PHP alert is being shown; if the value is “silence”, then in cases when the plugin has some problem with parameters, it finishes its work without any alert being shown. Set this parameter to “alert” for development, and to “silence” - for deployment.

EXAMPLE OF USAGE

The code

{exp:simple_math calculate="(11 + 4) / 3"}

will output “5”.

The code

{exp:simple_math calculate="ceil((10 + 6) / 3)"}

will output “6”.

There is also Math Plus plugin which differs from Simple Math only in one feature: it is required to use a tag pair instead of single tag. Tag pair allows for better control of the parse order.

       
Adrienne L. Travis's avatar
Adrienne L. Travis
213 posts
16 years ago
Adrienne L. Travis's avatar Adrienne L. Travis

Laisvunas, you are a plugin-writing MACHINE! Thanks so much!

       
Mark Terpstra's avatar
Mark Terpstra
205 posts
16 years ago
Mark Terpstra's avatar Mark Terpstra

Would it be possible to use this plugin for a price calculator…

  • The formula would be something like this: Total Price = (Quantity x Unit Price) + Setup Fee
  • Unit Price and Setup Fee would be custom fields in EE
  • User would need to enter the Quantity to generate the Total Price

Otherwise I assume PHP could be used.

Any insights or recommendations?

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

Hi Mark,

It seems that Simple Math plugin can be useful for your purpose.

You only need to find a way how to input Quantity - maybe using PHP Session Variables? (There is Session Variables plugin, but it would need to be rewritten in order to use together with Simple Math).

The code would be as follows:

{exp:weblog:entries weblog="my_weblog" url_title="my_url_title" parse="inward"}
{exp:simple_math calculate="(<?php echo $quantity ?> * {unit_price}) + {setup_fee}"}
{/exp:weblog:entries}
       
Mark Terpstra's avatar
Mark Terpstra
205 posts
16 years ago
Mark Terpstra's avatar Mark Terpstra

Thanks Laisvunas. That looks like what I need. Very useful.

       
aircrash's avatar
aircrash
293 posts
16 years ago
aircrash's avatar aircrash

Is there a way to round the results? I’m trying to use the plugin in a way that requires a whole number.

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

Hi aircrash,

to round a result you can use such PHP functions as round(), floor(), ceil(), e.g.:

{exp:simple_math calculate="round((10 + 6) / 3)"}
{exp:simple_math calculate="floor((10 + 6) / 3)"}
{exp:simple_math calculate="ceil((10 + 6) / 3)"}
       
aircrash's avatar
aircrash
293 posts
16 years ago
aircrash's avatar aircrash

perfect, thanks!

       
dragonfly444's avatar
dragonfly444
2 posts
16 years ago
dragonfly444's avatar dragonfly444

I am trying to use the Simple Math plugin with a variable using the following code.

Tax = {exp:simple_math calculate="{total} * .07”}

if {total} is outside of the simple_math expression it works fine and produces a value but when added to simple math expression, I get this error…

Parse error: syntax error, unexpected ‘{’ in /home/sweettee/public_html/EE/files/plugins/pi.simple_math.php(65) : eval()’d code on line 1

Can anyone help me on this as I really need to get this to work.

Thanks, Candi

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

Hi Candi,

Can you provide bigger portion of your code; I mean the tag which the variable {total} belongs to.

       
dragonfly444's avatar
dragonfly444
2 posts
16 years ago
dragonfly444's avatar dragonfly444

Thanks for getting back to me. I’m all set.

Thanks, Candi

       
Mark Bowen's avatar
Mark Bowen
12,637 posts
16 years ago
Mark Bowen's avatar Mark Bowen

Hi Candi,

If you don’t mind my popping in on this one I’m not too sure what it is that you’re trying to do here but I would ask that you don’t share the Shopping Cart Plugin code with anyone as it is a paid product and as such I don’t really want it being sent out to anyone that hasn’t purchased it or the code being shown on the forums here. As the plugin is a paid plugin a lot of people won’t even know what it is or what it does so you may or may not get the answers you are after here.

I don’t know if you’ve already contacted me about this or not but if not then perhaps you could explain to me what you are trying to do and I can see if I can help maybe. The Shopping Cart Plugin does already contain totals and all those variables so I’m not too sure from looking at the code above exactly what it is that you are trying to do but if you can explain to me then perhaps I can help. Might also be best to either PM me or email me as I don’t want to take away from this thread if we can help it.

Thanks for your understanding on this.

Best wishes,

Mark

       
LucPestille's avatar
LucPestille
146 posts
16 years ago
LucPestille's avatar LucPestille

I’m trying to use this plugin in conjunction with SL Currency Converter, but get an error when trying to do this:

{exp:simple_math calculate="ceil( {exp:sl_currencyconverter:convert amount="{price_rand}" base="ZAR" target="GBP"} ) + {related_entries id="price_level"}{markup}{/related_entries}"}

It errors on the second {exp:} tag (substituting a static value works OK) - is this an issue with the plugin, or can I not embed {exp:} tags?

Thanks,

       
Ingmar Greil's avatar
Ingmar Greil
29,243 posts
16 years ago
Ingmar Greil's avatar Ingmar Greil

Have you tried parse=”inward”?

       
LucPestille's avatar
LucPestille
146 posts
16 years ago
LucPestille's avatar LucPestille
Have you tried parse=”inward”?

Yeah, doesn’t want to work (on either {exp:}, just to be certain). =/

       
1 2 3 Last

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.