2017-10-10 4 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を取得し、その製品のすべての注文を照会し、1つのメインアレイに割り当てます。

/* 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'] = []; 
     } 
    } 
} 
関連する問題