在具有相同价值的相同div产品中与smarty一起展示

时间:2019-05-07 00:40:24

标签: php smarty

我有4种产品,其中2种具有相同的order_id

这是我的聪明输出$orders

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"

我尝试了很多foreach来将具有相同order_id的产品“合并”到同一div中而没有成功

这是我想要的输出:

<div> 
  id 85 
</div> 
<div> 
  id 98 
  id 99 
</div> 
<div> 
  id 102 
</div>

我的foreach(可以轻松进入结果)

            {foreach $orders as $orderdisplay}

                    <table class="table" style="border: 1px solid black;">
                        <tbody>
                            <tr>
                                <td></td>
                                <td>ITEM</td>
                                <td>QUANTITY</td>
                                <td>PRICE</td>
                                <td>STATUS</td>
                            </tr> 
                            <tr>
                                <td>
                                </td>
                                <td>{$orderdisplay.title}<br />{$orderdisplay.variants}</td>
                                <td>{$orderdisplay.quantity}</td>
                                <td>{$orderdisplay.total_price}</td>
                                <td>{$orderdisplay.status}</td>
                            </tr>

                        </tbody>
                    </table>
                    <span>{$orderdisplay.total_price}</span>
            {/foreach}

1 个答案:

答案 0 :(得分:0)

bsmither在smarty论坛上给出的解决方案:

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

{foreach $OID as $o_id => $orders4OrderID} 
   <div> 
      {foreach $orders4OrderID as $order} 
         product id {$order.id} 
      {/foreach} 
   </div> 
{/foreach}