Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

[Solved] Display in same div products with same value

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
artik
Smarty Rookie


Joined: 04 May 2019
Posts: 5

PostPosted: Mon May 06, 2019 8:30 pm    Post subject: [Solved] Display in same div products with same value Reply with quote

I have a question regarding an array and how to display my infos.
I have 4 products, and 2 of them with the same "order_id".

My $orders:

Code:
Array (8)
0 => Array (11)
  id => "85"
  order_id => "1"
  user_email => "jeff@gmail.com"
  total_price => "2000"
  status => "Pending payment"
  order_option_id => "1"
  image => "Luxury-Sofa-for-Main-Room-at-Home-Design-Luxury-with-Stunning-Outdoor.jpg"
  title => "ruyrytur"
  variants => ""
  quantity => "1"
  unit_price => "123.00"
1 => Array (11)
  id => "98"
  order_id => "2"
  user_email => "jeff@gmail.com"
  total_price => "20000"
  status => "Pending payment"
  order_option_id => "2"
  image => "Modern-Green-Home-Architecture-house-plans.jpg"
  title => "Vewlix 1080p (black and white)"
  variants => "Panel 2 players (+50 euros) Red backlight (+100 euros) Screen 1440p (+400 eur..."
  quantity => "3"
  unit_price => "2680.00"
2 => Array (11)
  id => "99"
  order_id => "2"
  user_email => "jeff@gmail.com"
  total_price => "20000"
  status => "Pending payment"
  order_option_id => "2"
  image => "amazing-modern-villa-Freshome-02.jpg"
  title => "Vewlix 1080p (red and white)"
  variants => "Panel 2 players (+50 euros)"
  quantity => "1"
  unit_price => "2000.00"
3 => Array (11)
  id => "102"
  order_id => "4"
  user_email => "jeff@gmail.com"
  total_price => "2000"
  status => "1"
  order_option_id => "4"
  image => "amazing-modern-villa-Freshome-02.jpg"
  title => "Vewlix 1080p (red and white)"
  variants => "Panel 2 players (+50 euros)"
  quantity => "1"
  unit_price => "2000.00"


I tried many foreach to "merge" the products with the same order_id in the same div without success
This is the output I'd like:

Code:
<div>
  product id 85
</div>
<div>
  product id 98
  product id 99
</div>
<div>
  product id 102
</div>


thanks for help


Last edited by artik on Tue May 07, 2019 2:53 pm; edited 1 time in total
Back to top
View user's profile Send private message
bsmither
Smarty Pro


Joined: 20 Dec 2011
Posts: 242
Location: West Coast

PostPosted: Tue May 07, 2019 2:34 am    Post subject: Reply with quote

You would have to presort the given array into a new multi-level array.
Code:
{foreach $orders as $n}
{$OIDs[{$n.order_id}][] = $n}
{/foreach}

Then:
{foreach $OID as $o_id => $orders4OrderID}
<div>
{foreach $orders4OrderID as $order}
product id {$order.product_id}
{/foreach}
</div>
{/foreach}
(I haven't tried this.)
Back to top
View user's profile Send private message
artik
Smarty Rookie


Joined: 04 May 2019
Posts: 5

PostPosted: Tue May 07, 2019 1:50 pm    Post subject: Reply with quote

It works perfectly, I had to remove the unexpected "s" from your second line ($OIDs), and it does the job :

Code:
{foreach $orders as $n}
   {$OID[{$n.order_id}][] = $n}
{/foreach}

{foreach $OID as $o_id => $orders4OrderID}
   <div style="background: grey; margin-bottom: 10px;">
      {foreach $orders4OrderID as $order}
         product id {$order.id}
      {/foreach}
   </div>
{/foreach}


Now, I calculate the price of each order:

Code:
{$total_count = 0}
{foreach $orders4OrderID as $order_count}
   {$total_count = $total_count + $order_count.unit_price*$order_count.quantity}
{/foreach}
Total: {$total_count}



It displays me properly each value of each order.

Many thanks again !
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP