如何显示查询中的数据 - 数据库表之间的一对多关系

How to display data from query - one to many relationship between db tables

我正在尝试显示来自查询的数据,但无法按应有的方式显示。 我在数据库中的表是:ordersheaderorderitems。他们之间是一对多的关系。一个 orderheader 有许多 orderitems。他们加入了 idOrder。 ordersheader中的一个idOrder对应orderitems中的多行。 我必须显示当前日期的所有订单并显示每个 idOrder 的所有订单项。所有订单都应如下所示:

http://prntscr.com/7lwoan

第一行是订单 - 它必须显示该订单的所有订单项,当您单击 Details 时,它必须在单独的行中显示每个订单项。 现在,当我使用下面的代码时,每个订单项都显示在单独的行中。如何让它看起来像上面的link?谢谢! 我的模型是:

 function getOrders(){
    $date = new DateTime("now");
 
    $curr_date = $date->format('Y-m-d ');
    $this->db->select('ordersheader.*,customer.name,orderitems.*');
    $this->db->from('ordersheader'); 
    $this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
    $this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
    $this->db->where('DATE(orderDueDate)', $curr_date);   

    $query = $this->db->get();
    return $query->result();
    
    }

我的看法是:

<?php
 if($orderlist){
foreach($orderlist as $row) {
 ?> <tr class="overview">
                        <td><?php echo $row->idOrder; ?></td>
                        <td class="text-center"><img src="<?= base_url();?>/assets/images/tick.png"></td>
                        <td><?php echo $row->name; ?></td>
                        <td><?php echo $row->eggSize; ?></td>
                        <td><?php echo $row->quantity; ?></td>
                        <td>
                            <div class="progress">
                                <div class="progress-bar" role="progressbar" style="width: 60%;">
                                    60%
                                </div>
                            </div>
                        </td>
                        <td>18:00</td>
                        <td><button type="button" class="btn btn-link btn-xs view-full-order">Full</button></td>
                    </tr> 
                    <tr class="full hide">
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><?php echo $row->eggSize; ?></td>
                        <td><?php echo $row->quantity; ?></td>
                        <td>
                            <div class="progress">
                                <div class="progress-bar progress-bar-info" role="progressbar" style="width: <?php echo $rand; ?>%;">
                                  <?php echo $rand; ?>%  
                                </div>
                            </div>
                        </td>
                        <td>14:00</td>
                        <td></td>
                    </tr>
 <?php } } ?>
             </tbody></table>

已编辑:我用 group_concat 制作并爆炸。但是现在,问题是对于每个 idOrder,我应该计算所有 orderitems 的平均兰特(这是进度条的值)。这应该针对每个订单 - 订单项的平均总和。现在,它的计算不正确。 现在看起来像这样:

http://prntscr.com/7mu0k3 Billa所在的这一行应该是接下来两行的平均值。 我的观点是:

<tbody class="client">
            <?php 
            if ($orderlist) {
                foreach ($orderlist as $row) {
                    $i = 0;

                    ?> <tr class="overview">
                        <td><?php echo $row->idOrder; ?></td>
                        <td class="text-center">
                            <?php
                             $s = $row->eggSize; 
                    $egg_size_array = explode(",", $row->eggSize);
                    $quantity_array = explode(",", $row->quantity);
                    
                    if (!empty($egg_size_array))
                        foreach ($egg_size_array as $key => $value) {
                            $rand[$key] = rand(1, 100);
                            $random_hour[$key] = rand(01, 24);  
                          
                        }
?>                      
                            
                        </td>
                        <td><?php echo $row->name; ?></td>
                        <td><?php echo $row->eggSize; ?></td>
                        <td><?php
                            $s = $row->quantity;
                            $s = explode(',', $s);
                            echo array_sum($s);
                         print_r($rand);  echo "<br>";print_r(array_sum($rand)); echo "<br>";   print_r(count($egg_size_array));
                            ?></td>
                        <td>
                            <div class="progress">
                            
                                <div class="progress-bar" role="progressbar" style="width: <?php 
                                echo array_sum($rand)/ count($egg_size_array); ?>%;">
                                     
                                      
                                <?php  echo array_sum($rand)/ count($egg_size_array); echo "%";  ?>

                                </div>
                            </div>
                        </td>
                        <td><?php echo max($random_hour); echo " ч."; ?></td>
                        <td><button type="button" class="btn btn-link btn-xs view-full-order">
                    <?php echo lang('details'); ?>  </button></td>
                    </tr> 
                    <?php
                   
                    if (!empty($egg_size_array))
                        foreach ($egg_size_array as $key => $value) {
                            // $rand = rand(1, 100);
                            //$random_hour = rand(01, 24);
                            ?>
                            <tr class="full hide">
                                <td></td>
                                <td></td>
                                <td></td>
                                <td> 
                <?php echo $value; ?></td>
                                <td><?php echo $quantity_array[$key]; ?></td>
                                <td>
                                    <div class="progress">
                                        <div class="progress-bar progress-bar-info" role="progressbar" style="width: <?php echo $rand[$key]; ?>%;">
                <?php echo $rand[$key]; ?>%  
                                        </div>
                                    </div>
                                </td>
                                <td><?php echo $random_hour[$key] . ' ч.' ?></td>
                                <td></td>
                            </tr>
                            <?php
                        }
                    ?><?php
                }
            }
            ?>
        </tbody></table>

我的模型是:

  function getOrders(){
        $date = new DateTime("now");
        $curr_date = $date->format('Y-m-d');
        $this->db->select('orderitems.eggSize as size');
        $this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.itemNumber) as itemNumber');
        $this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.quantity ) as quantity ');
        $this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.unitPrice) as unitPrice');
        $this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.eggSize ) as eggSize');
        $this->db->from('ordersheader'); 
        $this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
        $this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
        $this->db->where('DATE(orderDueDate)', $curr_date);
        $this->db->group_by('orderitems.idOrder');
        $query = $this->db->get();
        return $query->result();
    }

您应该创建一个函数来 return 订购 children。

function GetItems($idOrder)
{
    return $this->db->get_where("orderitems", "idOrder" => $idOrder)->array_result();
}

之后,您将删除包含订单项的 JOIN table。然后你应该得到你的查询的 array_result 并通过 foreach 迭代它并在你的集合中附加 children 像这样:

$curr_date = $date->format('Y-m-d ');
$this->db->select('ordersheader.*,customer.name');
$this->db->from('ordersheader'); 
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);   

$query = $this->db->get()->array_result();

$index = 0;

foreach($query as $item)
{
    $query[$index]["children"] = GetItems($item->idOrder);
    $index++;
}

我在 fiddle 中创建了一个模型来向您展示一个示例:http://codepad.org/LfjQJQCP