如何加入产品和产品变体以获得具有产品名称的每个变体

时间:2017-07-13 13:53:34

标签: php mysql join

如何加入产品和产品变体以获取具有产品名称的每个变体。我得到了所有变种,但我的循环每次都使用产品变体重复产品名称。 我的代码是:

    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"
            }
        }
    ]
}

对我来说有什么解决方案吗?

1 个答案:

答案 0 :(得分: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";
  }
}