2017-07-13 110 views
0

如何加入產品和產品變體以獲取每個變體的產品名稱。我得到所有變體,但是我的循環每次重複使用產品變體的產品名稱。 我的代碼是:如何加入產品和產品變體以獲得每個產品名稱的變體

public function viewAllProducts($user_id){ 
    $SelectProducts_Query = mysqli_query($this->conn, "SELECT smpr.product_id, smpr.user_id, smpr.product_name, smpr.product_sizes, smpr.product_photo, smpr.product_created, smpr.product_updated, smpq.quantity_id, smpq.quantity_product_id, smpq.quantity_user_id, smpq.quantity_available, smpq.quantity_price, smpq.quantity_cost, smpq.quantity_size_name FROM ssm_products_quantity as smpq inner join ssm_products as smpr on smpq.quantity_product_id=smpr.product_id WHERE smpr.user_id = $user_id order by smpr.product_id DESC"); 
    if($SelectProducts_Query){ 
     while($view_all_products = mysqli_fetch_array($SelectProducts_Query)){ 
      $view_all_products_details[] = array(
       "product_id" => $view_all_products["product_id"], 
       "product_name" => $view_all_products["product_name"], 
       "product_photo" => $view_all_products["product_photo"], 
       "product_sizes" => $view_all_products["product_sizes"], 
       "product_created_date" => $view_all_products["product_created"], 
       "product_updated_date" => $view_all_products["product_updated"], 
       "sizes" => array(
        "size_id" => $view_all_products["quantity_id"], 
        "size_product_id" => $view_all_products["quantity_product_id"], 
        "size_available_id" => $view_all_products["quantity_available"] 
       ) 
      ); 
     } 
     return $view_all_products_details; 
    }else{ 
     return $view_all_products_details = "false"; 
    } 
} 

現在我得到的記錄是這樣的: enter image description here

我想這樣的記錄:

{ 
    "error":false, 
    "products_detail":[ 
     { 
      "product_id":"24", 
      "product_name":"Miror", 
      "product_photo":"product_images\/1-dummy-17-07-11-10-26-06.jpg", 
      "product_sizes":"2", 
      "product_created_date":"2017-07-11 13:26:06", 
      "product_updated_date":"0000-00-00 00:00:00", 
      "sizes":{ 
       "size_id":"1", 
       "size_product_id":"24", 
       "size_available_id":"50" 
      },{ 
       "size_id":"2", 
       "size_product_id":"24", 
       "size_available_id":"20" 
      } 
     }, 
     { 
      "product_id":"25", 
      "product_name":"Pipes", 
      "product_photo":"product_images\/1-pipes-17-07-11-10-22-08.jpg", 
      "product_sizes":"2", 
      "product_created_date":"2017-07-11 13:26:06", 
      "product_updated_date":"0000-00-00 00:00:00", 
      "sizes":{ 
       "size_id":"3", 
       "size_product_id":"25", 
       "size_available_id":"20" 
      },{ 
       "size_id":"4", 
       "size_product_id":"25", 
       "size_available_id":"20" 
      } 
     } 
    ] 
} 

有我的解決方案嗎?

回答

2

使用你的產品ID作爲數組索引

試試這個:

public function viewAllProducts($user_id){ 
    $SelectProducts_Query = mysqli_query($this->conn, "SELECT smpr.product_id, smpr.user_id, smpr.product_name, smpr.product_sizes, smpr.product_photo, smpr.product_created, smpr.product_updated, smpq.quantity_id, smpq.quantity_product_id, smpq.quantity_user_id, smpq.quantity_available, smpq.quantity_price, smpq.quantity_cost, smpq.quantity_size_name FROM ssm_products_quantity as smpq inner join ssm_products as smpr on smpq.quantity_product_id=smpr.product_id WHERE smpr.user_id = $user_id order by smpr.product_id DESC"); 
    if($SelectProducts_Query){ 
    while($view_all_products = mysqli_fetch_array($SelectProducts_Query)){ 
     $pid = $view_all_products["product_id"]; 
     //check the array for product with id is already exists, 
     //if not then add the data to array[pid] without the sizes 
     if(!isset($view_all_products_details[$pid]){ 
     $view_all_products_details[$pid] = array(
      "product_id" => $pid, 
      "product_name" => $view_all_products["product_name"], 
      "product_photo" => $view_all_products["product_photo"], 
      "product_sizes" => $view_all_products["product_sizes"], 
      "product_created_date" => $view_all_products["product_created"], 
      "product_updated_date" => $view_all_products["product_updated"] 
     ); 
     } 
     //add the array "sizes" as multidimensional array 
     //you can set the size_id as index too but i think it is not necessary 
     $view_all_products_details[$pid]["sizes"][] = array(
      "size_id" => $view_all_products["quantity_id"], 
      "size_product_id" => $view_all_products["quantity_product_id"], 
      "size_available_id" => $view_all_products["quantity_available"] 
    ); 
    } 
    return $view_all_products_details; 
    }else{ 
    return $view_all_products_details = "false"; 
    } 
} 
+0

能否請您解釋一下你沒有。這對其他的尋找者也會有幫助。 –

+0

我會更新它,請稍候... – Atmahadli

+0

@NanaPartykar更新,謝謝。 – Atmahadli