There is a foreach that fills the fields on the form from the table by the following query from the database: $mysqli->query("SELECT * FROM tblOrder INNER JOIN tblOrderProduct ON tblOrderProduct.OrderID=tblOrder.OrderID INNER JOIN tblProduct ON tblOrderProduct.ProductID=tblProduct.ProductID WHERE tblOrder.CustomerID ='$customerid'");

tblOrderProduct table looks like:
|OrderID||ProductID| |1 ||1 | |1 ||2 |

Those. There may be several products in one order. So the problem occurs when I try to fill the fields in a loop. I need to push values ​​with the same OrderID into one div so that the product information follows each other in the limit of this diva, but I create separate divs, which results in the product information being shared. How can I achieve such a union? Fill in the fields on the page like this:

 <?php foreach ($_SESSION['orders'] as $orders): ?> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?php echo $orders["OrderID"];?>">Номер заказа #<?php echo $orders["OrderID"];?><div class="pull-right"><?php echo $orders["DateOrder"]; ?></div></a> </h4> </div> <div id="collapse<?php echo $orders["OrderID"];?>" class="panel-collapse collapse"> <div class="panel-body"> <table class="table"> <thead> <tr> <td>Название</td> <td>Кол-во</td> <td>Цена</td> </tr> </thead> <tbody> <tr> <td><?php echo $orders["ProductName"];?></td> <td><?php echo $orders["Quality"];?></td> <td><?php echo $orders["TotalSum"];?></td> </tr> </tbody> </table> </div> </div> </div> <?php endforeach; ?> 

Result var_dumb () : array(6) { [0]=> array(6) { ["OrderID"]=> string(2) "22" ["ProductID"]=> string(1) "3" ["ProductName"]=> string(67) "Котел твердотопливный ZOTA Carbon 32 уголь" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-05" ["TotalSum"]=> string(4) "5000" } [1]=> array(6) { ["OrderID"]=> string(2) "23" ["ProductID"]=> string(1) "7" ["ProductName"]=> string(67) "Котел твердотопливный ZOTA Carbon 32 уголь" ["Quality"]=> string(1) "2" ["DateOrder"]=> string(10) "2016-06-06" ["TotalSum"]=> string(5) "10334" } [2]=> array(6) { ["OrderID"]=> string(2) "23" ["ProductID"]=> string(1) "6" ["ProductName"]=> string(34) "Автомат 1П 01А ABB S201C1" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-06" ["TotalSum"]=> string(5) "10334" } [3]=> array(6) { ["OrderID"]=> string(2) "24" ["ProductID"]=> string(1) "4" ["ProductName"]=> string(32) "Стартер S10 4-65W Philips" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-09" ["TotalSum"]=> string(4) "7334" } [4]=> array(6) { ["OrderID"]=> string(2) "24" ["ProductID"]=> string(1) "6" ["ProductName"]=> string(34) "Автомат 1П 01А ABB S201C1" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-09" ["TotalSum"]=> string(4) "7334" } [5]=> array(6) { ["OrderID"]=> string(2) "24" ["ProductID"]=> string(1) "7" ["ProductName"]=> string(67) "Котел твердотопливный ZOTA Carbon 32 уголь" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-09" ["TotalSum"]=> string(4) "7334" } } array(6) { [0]=> array(6) { ["OrderID"]=> string(2) "22" ["ProductID"]=> string(1) "3" ["ProductName"]=> string(67) "Котел твердотопливный ZOTA Carbon 32 уголь" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-05" ["TotalSum"]=> string(4) "5000" } [1]=> array(6) { ["OrderID"]=> string(2) "23" ["ProductID"]=> string(1) "7" ["ProductName"]=> string(67) "Котел твердотопливный ZOTA Carbon 32 уголь" ["Quality"]=> string(1) "2" ["DateOrder"]=> string(10) "2016-06-06" ["TotalSum"]=> string(5) "10334" } [2]=> array(6) { ["OrderID"]=> string(2) "23" ["ProductID"]=> string(1) "6" ["ProductName"]=> string(34) "Автомат 1П 01А ABB S201C1" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-06" ["TotalSum"]=> string(5) "10334" } [3]=> array(6) { ["OrderID"]=> string(2) "24" ["ProductID"]=> string(1) "4" ["ProductName"]=> string(32) "Стартер S10 4-65W Philips" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-09" ["TotalSum"]=> string(4) "7334" } [4]=> array(6) { ["OrderID"]=> string(2) "24" ["ProductID"]=> string(1) "6" ["ProductName"]=> string(34) "Автомат 1П 01А ABB S201C1" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-09" ["TotalSum"]=> string(4) "7334" } [5]=> array(6) { ["OrderID"]=> string(2) "24" ["ProductID"]=> string(1) "7" ["ProductName"]=> string(67) "Котел твердотопливный ZOTA Carbon 32 уголь" ["Quality"]=> string(1) "1" ["DateOrder"]=> string(10) "2016-06-09" ["TotalSum"]=> string(4) "7334" } }

    1 answer 1

    You should first convert the resulting array of products into an array "заказ" -> "продукты в заказе" . Those. run through the loop source array and form an array of the form:

     [ 'OrderID_1' => [ 'ProductID_1_1', 'ProductID_1_2', ... 'ProductID_1_N', ], ... 'OrderID_M' => [ 'ProductID_M_1', 'ProductID_M_2', ... 'ProductID_M_N', ], ] 

    And already such an array without any problems can display in a table, as you need. There will be two cycles - by the external one, go over the orders and output the div , and the internal cycle inside the div output the products.

    Update

    Judging by what you have in the code, I will assume that the structure of the array is approximately as follows:

     [ ["OrderID", "ProductID", "ProductName", "TotalSum", "Quality"], ... ] 

    To bring it to the desired form, you can:

     $result = array(); foreach($orders as $order) { if (!isset($result[$order['OrderID']])) { $result[$order['OrderID']] = array(); } $result[$order['OrderID']][] = $order; } 

    Update 2

    Here is your code with the required conversions. Naturally, it was not tested on the data, because you can check and correct errors, if any.

     <?php if (!empty($_SESSION['orders'])): ?> <?php $orderMap = array(); foreach($_SESSION['orders'] as $order) { if (!isset($orderMap[$order['OrderID']])) { $orderMap[$order['OrderID']] = array( 'products' => array(), 'date' => $order['DateOrder'] ); } $orderMap[$order['OrderID']]['products'][] = $order; } ?> <?php foreach ($orderMap as $orderId => $orderData): ?> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapse<?=$orderId;?>">Номер заказа #<?=$orderId;?><div class="pull-right"><?=$orderData["date"]; ?></div></a> </h4> </div> <div id="collapse<?=$orderId;?>" class="panel-collapse collapse"> <div class="panel-body"> <table class="table"> <thead> <tr> <td>Название</td> <td>Кол-во</td> <td>Цена</td> </tr> </thead> <tbody> <?php foreach($orderData['products'] as $product): ?> <tr> <td><?=$product["ProductName"];?></td> <td><?=$product["Quality"];?></td> <td><?=$product["TotalSum"];?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <?php endforeach; ?> <?php endif; ?> 
    • No matter how hard I tried, I didn’t understand how to do it ... After all, besides the ProductID I also need to get other fields. - Denis
    • Well, I only roughly indicated the structure. If it's not difficult for you, add to the question what a single record from your array looks like that you output. And then I'll try to update the answer. - Gino Pane
    • Tell me, is this how I correctly convert the array or not? foreach ($order as $key => $value){ $orderProd1= array($key['OrderID']=>$value['ProductID']); } foreach ($order as $key => $value){ $orderProd1= array($key['OrderID']=>$value['ProductID']); } - Denis
    • I think no. At least $ orderProd1 you are constantly overwriting. - Gino Pane
    • I updated the answer. - Gino Pane