Fortran
Fortran

Reputation: 593

Smarty total amount sum

how can I count the final price to pay if I have the following code

$Total

:

 {foreach from=$items key=k item=item}
            <tr>
              <td class="align_left"><strong>{$item.Title}</strong></td>
              <td><a href="products/view/{$k}"><img src="products_images/{$item.ImagePath}" width="60" height="60" alt="{$item.Title}" /></a></td>
              <td>{$item.quantity}</td>
              <td>{$item.Price}<span class="s_currency s_after"> лв.</span></td>
              <td>{math equation="x * y" x=$item.Price y=$item.quantity}<span class="s_currency s_after"> лв.</span></td>
            </tr>              
         {/foreach}
         <tr class="last">
             <td class="align_right" colspan="4"><strong>Всичко:</strong></td>
             <td class="s_secondary_color"><span class="s_currency s_before">$</span>{$Total}</td>
        </tr>

Upvotes: 3

Views: 6112

Answers (3)

voodoo417
voodoo417

Reputation: 12101

I did not understand why you need it so, but do it:

NOTICE: for Smarty 3.

{$Total = 0 }  

{foreach from=$items key=k item=item}
        <tr>
          <td class="align_left"><strong>{$item.Title}</strong></td>
          <td><a href="products/view/{$k}"><img src="products_images/{$item.ImagePath}" width="60" height="60" alt="{$item.Title}" /></a></td>
          <td>{$item.quantity}</td>
          <td>{$item.Price}<span class="s_currency s_after"> лв.</span></td>
          <td>{math equation="x * y" x=$item.Price y=$item.quantity}<span class="s_currency s_after"> лв.</span></td>
          {$Total = $Total+ ($item.Price*$item.quantity) }
        </tr>              
     {/foreach}
     <tr class="last">
         <td class="align_right" colspan="4"><strong>Всичко:</strong></td>
         <td class="s_secondary_color"><span class="s_currency s_before">$</span>{$Total}</td>
    </tr>

Upvotes: 3

user1625871
user1625871

Reputation:

use assign in ur loop and add the values to variable {$total}

{assign var=total value=0}

better loop through ur array in php and calculate total, if data coming from database, then query sum

Upvotes: 0

Victor Bocharsky
Victor Bocharsky

Reputation: 12306

Better to use MySQL query with GROUP BY and SUM() if individual amounts store in DB

Upvotes: 0

Related Questions