2015-06-27 133 views
0

我试图显示来自查询的结果,但无法显示它应该显示的结果。 我有两个表 - ordersheaderorderitems显示来自查询的数据 - 我无法正确显示

ordersheader是订单表。它的结构是: idOrder,idCustomer,createdDate,orderDueDate

我的表 '的OrderItems' 包含。它具有下列结构中的每个单独的顺序的OrderItems: orderitem_ididOrderitem_numbereggSizequantityprice

这两个表加入了ON IDOrder。 ordersheader中的一行对应于orderitems中的许多行,因为一个订单可能包含许多订货项。 我的问题是我无法正确显示它们。它要显示这样的:

http://prntscr.com/7lwnz7

,当您单击详细信息应该看起来像:

http://prntscr.com/7lwoan

现在,他们都显示在这样的新行一个OrderItem的:

http://prntscr.com/7lworg 我必须使每个idOrder的结果显示在一行中。 我应该使用内部循环,以及如何做到这一点? 我的模式是:

<?php 
 
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> <?php for($i=0;$i<count($row->eggSize); $i++) { ?> 
 
        <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 } ?> 
 
<?php } } ?> 
 
      </tbody></table>

+1

试试你的qyery用'$这个 - > DB->其中('DATE(ordersheader.orderDueDate)',$ curr_date);' – Saty

+0

我做到了这一点:$ this-> db-> where('DATE(orderDueDate)',$ curr_date);此列orderDueDate仅在表'ordersheader'中。问题是每个订单都有很多订单项,我想将它们合并为一个订单。 –

+0

在您的查询中使用group by – Saty

回答

1
<?php 
    function getOrders(){ 
     $date = new DateTime("now"); 
     $curr_date = $date->format('Y-m-d'); 
     $this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.item_number SEPARATOR ",") as item_number'); 
$this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.priceSEPARATOR ",") as price'); 
$this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.eggSize SEPARATOR ",") 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); 

     $query = $this->db->get(); 
     return $query->result(); 
    ?> 
+0

谢谢!在模型中,我添加了:$ this-> db-> group_by('orderitems.idOrder');但是当你点击“详细信息”时如何使这些项目在不同的行中?当你点击“详细信息”来确定idOrder时,它应该只为这个idOrder打开单独的订单项目。我应该使用内循环吗? –