8

Have a nice day everyone, I have something to ask your hel, to better understand here is my code:

{math equation=((($order_total-$commission)+$discount+$delivery_charge)*$rate)}

I want that to be pass to another variable,in php I want to be like this

<?php 
  $var=0
  foreach($result as $resultA){
   $var = $var+((($order_total-$commission)+$discount+$delivery_charge)*$rate);
}
?>

Hope you can help me guys!

Noel Balaba
  • 183
  • 2
  • 11

2 Answers2

15

If you're using Smarty 3 I highly recommend ditching the {math}:

{$order_total = 123}
{$commission = 13}
{$discount = 10}
{$delivery_charge = 20}
{$rate = 1.1}

{$result = 0}
{$result = $result + ($order_total - $commission + $discount + $delivery_charge) * $rate}
{$result}

It is both better readable and faster (as the expression is actually compiled, rather than eval()ed over and over again).


Smarty 2 equivalent:

{assign var="order_total" value=123}
{assign var="commission" value=13}
{assign var="discount" value=10}
{assign var="delivery_charge" value=20}
{assign var="rate" value=1.1}

{assign var="result" value=0}
{math 
  assign="result"
  equation="result + (order_total - commission + discount + delivery_charge) * rate"
  result=$result
  order_total=$order_total
  commission=$commission
  discount=$discount
  delivery_charge=$delivery_charge
  rate=$rate
}
{$result}

If there is any chance to upgrade to Smarty 3 - do it!

rodneyrehm
  • 13,442
  • 1
  • 40
  • 56
  • Hi Rodney, Im using smarty version 2.6.18 , i tried your suggestion but nothing display, how can i translate that into version 2.6.18 – Noel Balaba Jun 25 '12 at 01:54
  • I've updated my answer to show a Smarty2 solution as well. please close the question when you're satisfied – rodneyrehm Jun 26 '12 at 07:48
1

Try to use assign parameter:

From documentation http://www.smarty.net/docsv2/en/language.function.math.tpl :

If you supply the assign attribute, the output of the {math} function will be assigned to this template variable instead of being output to the template.

But it would be better, if you had made ​​similar calculations using PHP (at business-logic layer, not at view)

See http://www.smarty.net/best_practices (section "Keep business logic separate!")

Vladimir Posvistelik
  • 3,843
  • 24
  • 28