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

Hi Luc,

Your code

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

will not work because exp:sl_currencyconverter:convert tag is single tag. In order for SL Currency Converter to work with Simple Math plugin it should support variables within tag pairs pairs, e.g. {converted_currency_value} within {exp:sl_currencyconverter:convert}{/exp:sl_currencyconverter:convert} tag pair:

{exp:sl_currencyconverter:convert amount="{price_rand}" base="ZAR" target="GBP" parse="inward"}
{exp:simple_math calculate="ceil({converted_currency_value}) + {related_entries id="price_level"}{markup}{/related_entries}"}
{/exp:sl_currencyconverter:convert}
       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

I’m receiving the same error as dragonfly444 was above. Can you please let us know what the solution to this issue was?

Thanks, -Brett

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

Hi bdewoody,

In order to debug it should be possible for me to reproduce the issue; so, provide the portion of your code which causes that error message.

       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

Basically hacking together a simple shopping cart, I know its sloppy. The template this code is used on is set to allow PHP on input. It loops through the products, creating a segment of code which then gets parsed by EE and the simple_math plugin. It spits out what looks like a simple equation, something like (150*2) + (10 *4). But when that gets parsed by the exp:simple_math tag it throws the error at the bottom.

The code:

<?php
        //print_r($_SESSION['shoppingcart']);
                $shoppingtotal = "";
                foreach ($_SESSION['shoppingcart'] as $item => $info) {
                   $item_id = substr($item,0,strpos($item,"-"));
                   $size = substr($item,strpos($item,"-")+1,strlen($item)); 
                   $shoppingtotal .= "{exp:weblog:entries weblog=\"merchandise\" entry_id=\"" . $item_id . "\"}({merchandise-price} * " . $info['quantity'] . "){/exp:weblog:entries} + ";
                   echo "{exp:weblog:entries weblog=\"merchandise\" entry_id=\"" . $item_id . "\"}";
                   echo "<div class=\"row\">
                        <form action=\"\" method=\"post\">
                        <input type=\"hidden\" name=\"item\" value=\"" . $item_id . "\"  />
                        <input type=\"hidden\" name=\"size\" value=\"" . $size . "\"  />
                        <div class=\"left col-img\"><img  ></div>
                        <div class=\"left col1\">{title}</div>
                        <div class=\"left col2\">" . $size . "</div>
                        <div class=\"left col3\"><input type=\"text\" name=\"quantity\" value=\"" . $info['quantity'] . "\" /></div>
                        <div class=\"left col4\">\${exp:simple_math calculate=\"{merchandise-price} * " . $info['quantity'] . "\"}</div>
                        <div class=\"left col5\"><input type=\"submit\" value=\"Update\" /></div>
                        </form></div>
                        <div class=\"clear\"></div>";
                   echo "{/exp:weblog:entries}";
                }
                $shoppingtotal = substr($shoppingtotal,0,-3);
                echo $shoppingtotal;
                echo "{exp:simple_math parse=\"inward\" calculate=\"$shoppingtotal\"}";
          ?>

The error:

Notice: Use of undefined constant M20o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr - assumed 'M20o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr' in /htdocs/www/system/plugins/pi.simple_math.php(65) : eval()'d code on line 1

Notice: Use of undefined constant M30o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr - assumed 'M30o93H7pQ09L8X1t49cHY01Z5j4TT91fGfr' in /htdocs/www/system/plugins/pi.simple_math.php(65) : eval()'d code on line 1

Hope this helps, and thanks for the help in advance!

-Brett

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

Hi,

Check if after PHP code was evaluated you really have left with

{exp:simple_math calculate="(150*2) + (10 *4)"}

I checked and found that it works perfectly.

       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

With the items I currently have in my shopping cart here is what is spit out for the $shoppingtotal variable after the loop is finished:

(150.00 * 2) + (10.00 * 5)

If I copy this into the {exp:simple_math} tag it works fine. But it doesn’t work when I insert it via PHP. Like this:

echo "{exp:simple_math parse=\"inward\" calculate=\"$shoppingtotal\"}";

I’ve tried it with and without the parse=”inward” parameter with the same result.

Any other ideas? Love the plugin btw, this is functionality is definitely needed in EE.

       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

Here is the code being spit out in the end:

{exp:simple_math calculate="(150.00 * 2) + (10.00 * 5)"}

It returns 0, along with the errors I mentioned before.

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

Hi bdewoody,

On my installation both the code

{exp:simple_math calculate="(150.00 * 2) + (10.00 * 5)"}

and the code

<?php
echo "{exp:simple_math calculate=\"(150.00 * 2) + (10.00 * 5)\"}";
?>

works perfectly.

If on your installation it does not, the I do not know what to suggest.

       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

Try to put

(150.00 * 2) + (10.00 * 5)

into a php variable and insert into string as a variable. That’s what’s causing the problem for me. When I do what you’re doing it returns the correct answer. Its only when the formula above is inserted via a PHP variable into the echo statement.

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

Hi bdewoody,

Tried to put numbers which should be calculated into a variable and inserted it as the value of “calculate” parameter and found that it works correctly. Try the code below:

<?php
$numbers = "(150.00 * 2) + (10.00 * 5)";
echo "{exp:simple_math calculate=\"".$numbers."\"}";
?>
       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

Thanks for the help Laisvunas, but still no luck.

If I copy what you did it works out fine, but pulling my dynamic equation in isn’t working. I’ve made it spit out the exp line, and it returns this:

{exp2:simple_math calculate="(150.00 * 2) + (10.00 * 5) + (19.00 * 1)"}

I added the “2” in there to prevent it from being evaluated by EE. But when I remove the “2” I still get the error I was before.

I’ve tried a few more tactics and managed to get a different error, so maybe I’m a bit closer. Here’s the new error:

Parse error: syntax error, unexpected '{' in /htdocs/www/system/plugins/pi.simple_math.php(65) : eval()'d code on line 1

Has this come up before?

Thanks!

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

Hi,

You have

{exp2:simple_math calculate="(150.00 * 2) + (10.00 * 5) + (19.00 * 1)"}

instead of

{exp2:simple_math calculate="(150.00 * 2) + (10.00 * 5) + (19.00 * 1)"}

Notice “exp2”.

       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

I added the “2” so the exp code would be visible without being processed by ExpressionEngine. When I remove the 2, the code is processed and that’s when I receive the error in my previous message.

Meaning I get the error above when I use

{exp:simple_math calculate="(150.00 * 2) + (10.00 * 5) + (19.00 * 1)"}
       
Laisvunas's avatar
Laisvunas
879 posts
about 16 years ago
Laisvunas's avatar Laisvunas

The code

{exp:simple_math calculate="(150.00 * 2) + (10.00 * 5) + (19.00 * 1)"}

produces no errors.

But the code

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

produces the following error message:

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

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

Fix the parse order and there will be no error message.

       
Brett DeWoody's avatar
Brett DeWoody
140 posts
about 16 years ago
Brett DeWoody's avatar Brett DeWoody

Thanks for all this help Laisvunas. It is greatly appreciated!

I added a parse=”inward” to the exp:simple_math tag. The error I’m getting now is:

Parse error: syntax error, unexpected ';' in /htdocs/www/system/plugins/pi.simple_math.php(65) : eval()'d code on line 1
       
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.