Reputation: 593
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
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
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
Reputation: 12306
Better to use MySQL query with GROUP BY and SUM() if individual amounts store in DB
Upvotes: 0