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}
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
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.
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.
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.
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."\"}";
?>
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!
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)"}
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.