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

Since {exp:simple_math} is used as a a single tag, not as a tag pair, there is no sense to use in it

parse="inward"

parameter.

When you have

{exp:simple_math calculate="{my_unparsed_variable}"}

you should add that parameter to the tag {my_unparsed_variable} belongs to.

       
kpow's avatar
kpow
22 posts
about 16 years ago
kpow's avatar kpow

Dear Laisvunas;

I am trying to do something very similar to what Mark wants (on post #2 and your response and his on #3 and #4). But I am not getting any love.

It should not required any php but still I have even change in the templates preferences to allow php and php parsing stage, but none have worked.

After installing your plugin

I am trying to do the following:

  • The formula would be something like this: Total Price = (Quantity x Unit Price)
  • Unit Price and Quantity are custom fields in EE
  • Basically, my template shows a list of entries in that blog. there are three collums total, one for price, one for quantity and one for total price.
  • I am having a hard time making the script automatically fill the last collum based on the first two collumns that are {unit_price} and {quantity}

Makes sense? can you help or point where to go, what to do? please, thanks.

my code is:

{exp:weblog:entries weblog="orders" orderby="entry_id" sort="desc" parse="inward"}
{exp:simple_math calculate="{quantity} + {unit_price}"}
{/exp:weblog:entries}
       
Laisvunas's avatar
Laisvunas
879 posts
15 years ago
Laisvunas's avatar Laisvunas

Hi,

It seems that you have an error in your code - there should be * symbol instead of + symbol:

{exp:weblog:entries weblog="orders" orderby="entry_id" sort="desc" parse="inward"}
{exp:simple_math calculate="{quantity} * {unit_price}"}
{/exp:weblog:entries}
       
kpow's avatar
kpow
22 posts
15 years ago
kpow's avatar kpow

Thanks, but now, I get:

Parse error: syntax error, unexpected ‘<’ in /home/.aaaaa/aaaaaaa/aaaaaa.com/system/plugins/pi.simple_math.php(65) : eval()’d code on line 1

I read some suggestions here on the post, about this error, but I am still not sure.

Do I need to keep the parse=”inward” or remove it? from the above code?

Do I need to leave on the template preferences: Allow PHP? yes or no and PHP Parsing Stage - in or out?

Also, on your previous replies to others, what do you mean by:

You get this error because {exp:simple_math} is evaluated before the variable which is the value of the “calculate” parameter.

AND

Fix the parse order and there will be no error message. You should add that parameter to the tag {my_unparsed_variable} belongs to

….How/where do I do this little lost, sorry…

thanks again for your prompt reply. crossing my fingers here…

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

Hi,

Check what the variables {quantity} and {unit_price} output outside {exp:simple_math} tag:

{exp:weblog:entries weblog="orders" orderby="entry_id" sort="desc" parse="inward"}
{quantity}, {unit_price}

{/exp:weblog:entries}

If you get error message

syntax error, unexpected ‘<’ in /home/.aaaaa/aaaaaaa/aaaaaa.com/system/plugins/pi.simple_math.php(65) : eval()‘d code on line 1

it means that some variable you use inside “calculate” parameter outputs “<” symbol.

       
kpow's avatar
kpow
22 posts
15 years ago
kpow's avatar kpow

I GOT IT !

I had the : Field Groups › Custom Fields

Formatted for XHTML.

When I changed them to NONE, this did the trick !

THANKS FOR ALL YOUR HELP !

       
kpow's avatar
kpow
22 posts
15 years ago
kpow's avatar kpow

Ok, two more silly questions:

1) can I change something, and where, in order to make numbers have a fixed decimal case for 2, and thousands separator. IE: 1,000,00 rather than 1000 ?? how/where to make this changes?

2) is there a way to calculate a date? for instance: I have a field (days) that has 10 (or 20 or ..) and I have a field for date such as 12/12/2009. I wanted to have the {date}+(days) output the result in a date format: 12/22/2009. is there a easy way to do such (I havent tried yet, figured I would ask first..)

Thanks again Laisvunas, A+++ plugin!!

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

Hi,

1) I do not know how to prettify the numbers. It would be best if someone would write a plugin doing this.

2) in some cases you can use Simple Math plugin together with WI Friendlee Date plugin, but as far as I know there is no general solution. Again, it would be best if someone would write a plugin doing this.

       
kpow's avatar
kpow
22 posts
15 years ago
kpow's avatar kpow

thanks again… will wait for some kind soul to develop such solutions, in the mean time, your help has been amazing! I am up and running and at the end of the day, that was the most important thing. -have a great week

       
kpow's avatar
kpow
22 posts
15 years ago
kpow's avatar kpow

Back:

I have found a way to get my numbers working:

http://expressionengine.com/downloads/details/number_format/

So, the numbers work fine, if I just use their info.. for example:

{exp:number_format number="{order_price}" decimals="2" dec_point="," thousands_sep="."}

will output me something like 1.000,00

However, I have tried to use that in conjunction with yours, and it’s not working. I was wondering where was I wrong. I tried two ways, neither did any good:

(your example that works:

{exp:simple_math calculate="{order_quantity}*{order_price}"}

comes with a result 2000)

now:

{exp:number_format number="{exp:simple_math calculate="{order_quantity}*{order_price}"}" decimals="2" dec_point="," thousands_sep="."}

or

{exp:simple_math calculate="{exp:number_format number="{order_quantity}*{order_price}" decimals="2" dec_point="," thousands_sep="."}"}

none of them output the expected 2.000,00

Any ideas why? way around it? better coding on my end? thanks thanks thanks

       
Robert Wallis's avatar
Robert Wallis
36 posts
15 years ago
Robert Wallis's avatar Robert Wallis

Hey, just got your email. You can’t have one EE tag’s result supply the parameter for another EE tag.

However, you can turn on PHP in the template for “output” (I think) and then do this:

<?php
echo number_format( floatval("{order_quantity}") * floatval("{order_price}"), 2);
?>

Sorry, I didn’t test this out. Please let me know if it works.

       
kpow's avatar
kpow
22 posts
15 years ago
kpow's avatar kpow

Yo’all Rock!

Robert, It did work !!!! Thanks

So now the last thing I need to figure it out somehow is to get a specific date , like today, and add 10, or 20… (for net 10, net 20) and have it tell me what the date will be based on the 10 or 20, or…

Also, a little out of the simple math realm, but in the topic,.. ever since I combined both scrips using Robert’s suggestion above, now, if I have a field that is blank, its showing “0” is there a way to leave blank if its bkank rather than output “0”

And if I have a whole number, like 5.000,00 is there a way to show only 5.000 (without the decimals if there are no decimals in that specific number?)

Thanks again for any pointers relevant to the three questions above..

Thanks

       
kpow's avatar
kpow
22 posts
15 years ago
kpow's avatar kpow

jsut fyi, I have been able to come to terms. this is what I ended up doing.

http://ellislab.com/forums/viewthread/82801/#634835

thanks all

       
reconsider's avatar
reconsider
18 posts
15 years ago
reconsider's avatar reconsider

Hey I’m having problems with this plugin.

When I preview the page the following message appears in the front end -

Parse error: syntax error, unexpected T_DNUMBER in /www/eh10375/public_html/ee_glassworks/vpsys/plugins/pi.simple_math.php(65) : eval()'d code on line 1

This is the template code.

<div class="{switch="resultsBk|resultsBk2|resultsBk3|resultsBk4"}">
<a href="http://{permalink=">{image1_sml}</a>
<h3><a href="http://{permalink=">{designer_search_field}</a></h3>
{products_title}
{if foxee_is_on_sale}<span class="priceCross">{foxee_price}</span> - {exp:simple_math calculate="({foxee_price}/100)*{foxee_price}"}
<span class="sale2">On Sale - {foxee_saleprice}</span>{if:else}{foxee_price}{/if}
<h4>{tag}</h4>                        
</div>

Any help would be splendid.

Thanks

S

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

Hi,

Which tag the variable {foxee_price} belongs to?

       
1 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.