|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
artik Smarty Rookie
Joined: 04 May 2019 Posts: 8
|
Posted: Sun May 05, 2019 5:41 pm Post subject: [Solved] Getting sum of values in multi dimensional array |
|
|
Hi, I'm quite stuck since this afternnon. I tried many ways to get a result (using {count} and many other things I saw on documenttaion and internet) but none of them worked. Here is my infos :
$session
Code: | Value
Array (4)
msg => "You must log in first"
email => "jeff@gmail.com"
success => "You are now logged in"
shopping_cart => Array (2)
0 => Array (7)
id => "179"
title => "ruyrytur"
image => "Luxury-Sofa-for-Main-Room-at-Home-Design-Luxury-with-Stunning-Outdoor.jpg"
variants => Array (0)
quantity => 3
unit_price => "123.00"
price => 123
1 => Array (7)
id => "2"
title => "Vewlix 1080p (red and white)"
image => "amazing-modern-villa-Freshome-02.jpg"
variants => Array (1)
0 => Array (3)
option_id => "1"
option_name => "Panel 1 player"
option_price => "0.00"
quantity => 1
unit_price => "1950.00"
price => 1950 |
I'm trying with a foreach to get the sum of the value 'quantity' (that should be 4).
Code: | {foreach $session.shopping_cart as $cart}
{$cart.quantity|@count}
{/foreach} |
with no luck.
I also tried
Code: | {foreach $session.shopping_cart as $cart}
{$cart.quantity}
{/foreach} |
that works with one product, but if I add another one it doesn't count, simple displays me 3 and 1.
Any help?
Last edited by artik on Mon May 06, 2019 2:12 pm; edited 2 times in total |
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Sun May 05, 2019 11:14 pm Post subject: |
|
|
I would try:
Code: | {$cart_item_count = 0}
{foreach $session.shopping_cart as $cartItem}
{$cart_item_count += $cartItem.quantity}
{/foreach}
Total item count: {$cart_item_count}
|
|
|
Back to top |
|
artik Smarty Rookie
Joined: 04 May 2019 Posts: 8
|
Posted: Sun May 05, 2019 11:46 pm Post subject: |
|
|
thank bsmither, I tried, and here is the error:
Quote: | Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:/var/www/html/theme/header.tpl" on line 19 "{$cart_item_count += $cartItem.quantity}" - Unexpected "= " <-- thrown in /var/www/html/vendor/smarty/smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php on line 19 |
|
|
Back to top |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Mon May 06, 2019 4:59 am Post subject: |
|
|
Code: | {$cart_item_count += $cartItem.quantity}" - Unexpected "= " | Interesting. It then appears that Smarty (at least the version you are using) does not know about the operator shortcuts.
So, try:
Code: | {$cart_item_count = $cart_item_count + $cartItem.quantity} |
|
|
Back to top |
|
artik Smarty Rookie
Joined: 04 May 2019 Posts: 8
|
Posted: Mon May 06, 2019 1:05 pm Post subject: |
|
|
Works perfectly, thanks bsmither! |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|