Php 如何加入产品和产品变体以获得每个具有产品名称的变体
如何加入产品和产品变体以获得每个具有产品名称的变体。我得到了所有的变体,但我的循环每次都用产品变体重复产品名称。 我的代码是:Php 如何加入产品和产品变体以获得每个具有产品名称的变体,php,mysql,join,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
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";
}
}
现在我得到了这样的记录:
我想得到这样的记录:
{
"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"
}
}
]
}
有什么解决方案吗?使用您的产品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";
}
}
将产品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";
}
}
你能解释一下你做了什么吗。这对其他人的搜索者也有帮助。@Atmahadli,这对我有用,但我有一个问题。return语句在“products\u detail”对象之后显示一个产品ID作为对象。我想删除这个“24”:{从行号(4、24等)处的产品详细信息中,这可能吗?图像示例尝试以下操作:
returnarray\u merge($view\u all\u products\u details)
read quoted note:数字键将被重新编号。请您解释一下您所做的事情。这对其他的搜索者也会有帮助。@Atmahadli,它对我有用,但我有一个问题。return语句在“products\u detail”对象之后显示一个产品ID作为对象。我想从第行的产品详细信息中删除此“24”:{(4、24等)这可能吗?图像示例尝试以下操作:returnarray\u merge($view\u all\u products\u details);
read quoted注意:数字键将重新编号