2014-11-05 35 views
1
<?php 
include ("config.php"); 
$results = $mysqli->query 
(" 
    SELECT    orders_history.transaction_id, 
         orders_history.items, 
         orders_history.quantity, 
         orders_history.one_product_price, 

         orders_list.status, 
         orders_list.invoices, 
         orders_list.payment_method, 
         orders_list.order_method, 


         delivery_orders.address, 
         delivery_orders.service, 
         delivery_orders.cost, 
         delivery_orders.city 

    FROM    orders_history 
    LEFT JOIN   orders_list 
    ON     orders_history.transaction_id = orders_list.transaction_id 
    LEFT JOIN   infistall_order 
    ON     orders_history.transaction_id = infistall_order.transaction_id 
    LEFT JOIN   delivery_orders 
    ON     orders_history.transaction_id = delivery_orders.transaction_id 
    WHERE    orders_list.customer_name = 'Klaudia'"  
); 

$orders = array();  
$html = '';  
if ($results) { 
    while($obj = $results->fetch_object()) { 
     $orders[$obj->transaction_id][$obj->items] = array(
      'invoices' => $obj->invoices, 
      'status' => $obj->status, 
      'payment_method' => $obj->payment_method, 
      'service' => $obj->service, 
      'cost' => $obj->cost, 
      'quantity' => $obj->quantity, 
      'one_product_price' => $obj->one_product_price, 
      'city' => $obj->city); 
    } 

    $html .= '<table width="70%"><tr>'; 
    $html .= '<td>transaction_id</td>'; 
    $html .= '<td>items</td>'; 
    $html .= '<td>quantity</td>'; 
    $html .= '<td>one_product_price</td>'; 
    $html .= '<td>status</td>'; 
    foreach ($orders AS $order_id => $order) { 
     $html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>'; 
     $row = 1; 
     foreach ($order AS $item => $data) { 
      if ($row > 1) { $html .= '</tr><tr>'; } 
      $html .= '<td>' . $item . '</td>'; 
      $html .= '<td>' . $data['quantity'] . '</td>'; 
      $html .= '<td>' . $data['one_product_price'] . '</td>'; 
      $row++; 
      $html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>'; 
     } 
     $html .= '</tr><tbody>'; 
    } 
    $html .= '</table>'; 
} 
echo $html; 
?> 

上面的代碼會導致這樣的:防止一個數據環

----------------------------------------------------------------------- 
id_cart   products  quantity  invoices  status 
----------------------------------------------------------------------- 
       this    2    $20   
       -------------------------------------------------------  
    0001   that    1    $20  pending 
       -------------------------------------------------------  
       those    2    $20     pending pending 
----------------------------------------------------------------------- 
           Total Invoices: $60 
----------------------------------------------------------------------- 

       this    2    $20   
       -------------------------------------------------------  
    0002   that    1    $20  approved 
       -------------------------------------------------------  
       those    2    $20    approved approved 
----------------------------------------------------------------------- 
           Total Invoices: $60 

看一看的狀態欄,其中結果是循環。

我想要的是它不應該循環,只會得到一個結果作爲id_cart的列的樣子。這是狀態列的代碼

$html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>'; 

我需要解決它!

[編輯]

當我移動至這樣的代碼:

foreach ($orders AS $order_id => $order) { 
     $html .= '<tbody><tr><td rowspan="' . count($order) . '">' . $order_id . '</td>'; 
     $row = 1; 
     foreach ($order AS $item => $data) { 
      if ($row > 1) { $html .= '</tr><tr>'; } 
      $html .= '<td>' . $item . '</td>'; 
      $html .= '<td>' . $data['quantity'] . '</td>'; 
      $html .= '<td>' . $data['one_product_price'] . '</td>'; 
      $row++; 
     } 
     $html .= '<td rowspan="' . count($order) . '">' . $data['status'] . '</td>'; 
     $html .= '</tr><tbody>'; 


----------------------------------------------------------------------- 
id_cart   products  quantity  invoices  status 
----------------------------------------------------------------------- 
       this    2    $20   
       -------------------------------------------------------  
    0001   that    1    $20   
       -------------------------------------------------------  
       those    2    $20  pending 
----------------------------------------------------------------------- 
           Total Invoices: $60 
----------------------------------------------------------------------- 
       this    2    $20   
       -------------------------------------------------------  
    0002   that    1    $20   
       -------------------------------------------------------  
       those    2    $20  approved 
----------------------------------------------------------------------- 
           Total Invoices: $60 

的resultin的狀態中在TBODY的最底部的列。我想要它作爲id_cart的列是如何。

+0

你能呼應HTML輸出?我認爲這是排隊。 – Gudgip 2014-11-05 09:26:45

+0

爲什麼你將'rowspan'添加到STATUS列?你想要每個產品的狀態信息,對嗎? – 2014-11-05 09:41:41

+0

@trzEm,不,那不是我想要的。 – klaudia 2014-11-05 09:44:28

回答

-1

您需要每個tbody的狀態值爲1,因此請將其放在循環之外。另外,由於您正在重複使用$order,因此我已將count($order)置於單獨變量中。另外,關閉您的</tbody>標籤。

foreach ($orders AS $order_id => $order) { 
     $orderCount = count($order); 
     $html .= '<tbody><tr><td rowspan="' . $orderCount . '">' . $order_id . '</td>'; 
     $row = 1; 
     foreach ($order AS $item => $data) { 
      if ($row > 1) { $html .= '</tr><tr>'; } 
      $html .= '<td>' . $item . '</td>'; 
      $html .= '<td>' . $data['quantity'] . '</td>'; 
      $html .= '<td>' . $data['one_product_price'] . '</td>'; 
      if ($row == 1) 
       $html .= '<td rowspan="' . $orderCount . '">' . $data['status'] . '</td>'; 
      $row++; 
     } 
      $html .= '</tr></tbody>'; 
    } 
+0

我已經嘗試過了,但狀態是在tbody的非常靠後的位置。我希望它看起來類似於id_card的列。看看我的更新問題。 – klaudia 2014-11-05 09:39:48

+0

我把'count($ order)'放在一個單獨的變量中編輯了我的問題。 – Gudgip 2014-11-05 09:45:01

+0

它不會改變任何東西 – klaudia 2014-11-05 10:12:26

-1

對於td元素使用valign屬性來獲取垂直對齊中心的值。

$html .= '<td rowspan="' . $orderCount . '" valign="middle">' . $data['status'] . '</td>'; 

希望它能爲你工作。

-1

檢查了這一點:

foreach ($order AS $item => $data) { 
     if ($row > 1) { $html .= '</tr><tr>'; } 
     $html .= '<td>' . $item . '</td>'; 
     $html .= '<td>' . $data['quantity'] . '</td>'; 
     $html .= '<td>' . $data['one_product_price'] . '</td>'; 
     if($row == 1) 
     { 
     $html .= '<td style="vertical-align: middle" rowspan="' . count($order) . '">'. $data['status'] . '</td>'; 
     } 
     $row++; 
    } 
+0

Html看起來像http://jsfiddle.net/owbpvuws/ – 2014-11-05 10:25:21

+0

垂直對齊不應該是必要的(嘗試在你的jsfiddle中)。把最後一行放在循環之外也更清潔。 – Gudgip 2014-11-05 10:54:01