2017-10-10 105 views
0

如果有人可以幫忙,會很棒嗎?我目前有下面的數據由底部的代碼輸出,但比它爲每個order_product_id重複得多,我想從數據構建一個多級數組。來自多層級數組中的多個表的MySQL數據

電流輸出:

[0] => Array 
     (
      [order_product_id] => 43 
      [order_id] => 1 
      [product_id] => 161 
      [name] => Hoodie 
      [model] => Hoodie 
      [quantity] => 1 
      [price] => 23.9500 
      [total] => 23.9500 
      [tax] => 0.0000 
      [reward] => 0 
      [option_id] => 141 
      [option_name] => Hoodie Style 
      [option_value] => Pull over 
     ) 

    [1] => Array 
     (
      [order_product_id] => 43 
      [order_id] => 1 
      [product_id] => 161 
      [name] => Hoodie 
      [model] => Hoodie 
      [quantity] => 1 
      [price] => 23.9500 
      [total] => 23.9500 
      [tax] => 0.0000 
      [reward] => 0 
      [option_id] => 142 
      [option_name] => Hoodie Colour 
      [option_value] => Light Pink 
     ) 

    [2] => Array 
     (
      [order_product_id] => 43 
      [order_id] => 1 
      [product_id] => 161 
      [name] => Hoodie 
      [model] => Hoodie 
      [quantity] => 1 
      [price] => 23.9500 
      [total] => 23.9500 
      [tax] => 0.0000 
      [reward] => 0 
      [option_id] => 143 
      [option_name] => Adult Sizes 
      [option_value] => Ladies Meduim 10-12 
     ) 

期望中的數組,其中每個產品都有它的多層次的所有選項,而不是目前開設如何顯示它:

[0] => Array 
     (
      [order_product_id] => 43 
      [order_id] => 1 
      [product_id] => 161 
      [name] => Hoodie 
      [model] => Hoodie 
      [quantity] => 1 
      [price] => 23.9500 
      [total] => 23.9500 
      [tax] => 0.0000 
      [reward] => 0 
      Array 
       (
       [0] => Array 
        (
        [option_id] => 141 
        [option_name] => Hoodie Style 
        [option_value] => Pull over 
        ) 
       [1] => Array 
        (
        [option_id] => 142 
        [option_name] => Hoodie Colour 
        [option_value] => Light Pink 
        ) 
       [2] => Array 
        (
        [option_id] => 141 
        [option_name] => Adult Sizes 
        [option_value] => Ladies Meduim 10-12 
        ) 
       ) 
     ) 

的電流輸出是由建這個:

$sql = "SELECT site_order_product.*, 
site_order_option.order_option_id AS option_id, site_order_option.name AS option_name, site_order_option.value AS option_value 
FROM site_order_product 
INNER JOIN site_order_option ON site_order_product.order_product_id = site_order_option.order_product_id; "; 

$result=mysqli_query($dbh2,$sql); 
    if($result) { 
      $row = mysqli_fetch_all($result,MYSQLI_ASSOC); 
      return $row; 
     } else { 
      return false; 
     } 

感謝您的幫助!

+0

如果您能給我們一個合適的DB架構,這將會很有幫助。 –

+0

你需要的是一個ORM框架。 https://stackoverflow.com/questions/108699/good-php-orm-library –

回答

0

注意:您的外觀是惰性加載的功能。 ORM最適合您的問題 。命名一些ORM的RedBean,Doctrine,Propel。

這僅僅是爲了演示的一個例子。

沒有任何正確的數據庫表模式它很難給你準確的答案輸出你正在尋找的東西。但會幫助你的一些例子,這將做同樣的事情。

認爲你有產品表和訂單表。

Products 

id | product_name | product_price | product_notes 

Orders 

id | product_id | order_number 

問題你的問題:現在,如果你查詢的產品,那麼你會得到產品的詳細信息列表。但是你面臨的問題也是爲了獲得各自的訂單。

解決方案:當您循環訪問每個產品時,獲取相應的產品ID並查詢該產品的所有訂單並將其分配到一個主陣列中。

/* Get the list of products */ 
$productsQuery = mysqli_query($link, "SELECT * FROM products"); 
/* Array to hold the details or products and its respective order per product */ 
$productDetails = array(); 

if(mysqli_num_rows($productsQuery) > 0){ 
    $productCount = 0; 
    while($product = mysqli_fetch_assoc($productsQuery)){ 
     $productId = $product['id']; 

     /* Assign the product details to productDetails array */ 
     $productDetails[$productCount] = $product; 

     /* Get the details of orders which respective productid */ 
     $ordersQuery = mysqli_query($link, "SELECT * FROM orders WHERE product_id = ".$productId); 
     if(mysqli_num_rows($ordersQuery) > 0){ 
      while($order = mysqli_fetch_assoc($ordersQuery)){ 
       /* Here we have assigned the product orders to respective product */ 
       $productDetails[$productCount]['orders'] = $order; 
      } 
     }else{ 
      /* If no order assign an empty array */ 
      $productDetails[$productCount]['orders'] = []; 
     } 
    } 
}