2014-11-03 73 views
0

這是我的PHP代碼,用於回顯數據庫中的內容。在回聲多重循環的結構中堆棧

<?php 
include ("config.php"); 
$results = $mysqli->query 
("SELECT transaction_id FROM orders_list WHERE customer_name = 'Klaudia' ORDER BY id LIMIT 100");   
if ($results) { 
    while($obj = $results->fetch_object()) { 
     echo '<div id="thisklop">'; 
     echo '<ul class="ulung">Id_cart';   
     echo '<li>'.$obj->transaction_id.'</li>'; 
     echo '</ul>'; 
       $thisthat=$obj->transaction_id;   
       $otherresults = $mysqli->query 
          ("SELECT transaction_id, items, quantity, one_product_price FROM orders_history 
           WHERE transaction_id = '$thisthat'");   
       if ($otherresults) { 
        while($obj = $otherresults->fetch_object()) { 
         echo '<div>'; 
         echo '<ul class="ulung">Products'; 
         echo '<li>'.$obj->items.'</li>'; 
         echo '</ul>'; 
         echo '</div><br>'; 

         echo '<div>'; 
         echo '<ul class="ulung">Quantity'; 
         echo '<li>'.$obj->quantity.'</li>'; 
         echo '</ul>'; 
         echo '</div><br>'; 

         echo '<div>'; 
         echo '<ul class="ulung">Invoices'; 
         echo '<li>'.$obj->one_product_price.'</li>'; 
         echo '</ul>'; 
         echo '</div><br>'; 
        } 
       }          
     echo '</div>'; 
    } 
} 
?> 

我想要的結果是什麼樣的:

[UPDATE TABLE]

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

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

從上面的表中,id_cart在循環,然後id_cart裏面有也是循環中的內容。

我被困在結構上,我在迴應它。其結構是在一個爛攤子

enter image description here

+0

你的意思是卡住了,而不是堆棧? – Jake 2014-11-03 07:28:17

回答

0

首先,讓所有的數據在一個查詢(快),然後首先收集你的數據在陣列中。最後,根據數組構造輸出:

<?php 
include ("config.php"); 
$results = $mysqli->query 
(" 
    SELECT DISTINCT orders_history.transaction_id, 
         orders_history.items, 
         orders_history.quantity, 
         orders_history.one_product_price 
    FROM    orders_history, orders_list 
    WHERE    orders_history.transaction_id = orders_list.transaction_id 
    AND     orders_list.customer_name = 'Klaudia'" 

); 

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

     $html .= '<table><tr>'; 
     $html .= '<th>id_cart</th>'; 
     $html .= '<th>products</th>'; 
     $html .= '<th>quantity</th>'; 
     $html .= '<th>invoices</th></tr>'; 
     foreach ($orders AS $order_id => $order) { 
      $html .= '<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['invoices'] . '</td>'; 
/* INSERTED CODE FOR STATUS */ 
       if ($row == 1) { 
        $html .= '<tr><td rowspan="' . count($order) . '">' . $status . '</td>'; 
       } 
/* END OF INSERTION */ 
       $row++; 
      } 
      $html .= '</tr>'; 
     } 
     $html .= '</table>'; 
    } 
    echo $html; 
    ?> 

注:這是寫未經檢驗出我的頭,可能有一些錯誤,但主要的路徑應該是清楚的。

+0

這沒有顯示任何東西。請糾正它。 – klaudia 2014-11-03 13:29:19

+0

您是否收到任何錯誤消息?你可以發佈'var_dump($ results)'的結果嗎?(在$ orders = array();'line)之前插入? – Paul 2014-11-03 18:17:56

+0

它只是打印'bool(false)' – klaudia 2014-11-03 22:33:10