2015-06-27 136 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打開單獨的訂單項目。我應該使用內循環嗎? –