Php cart api只获取产品的一个详细信息,但如果存在,我需要每个产品的详细信息
这是我的控制器代码Php cart api只获取产品的一个详细信息,但如果存在,我需要每个产品的详细信息,php,mysql,api,codeigniter-3,Php,Mysql,Api,Codeigniter 3,这是我的控制器代码 public function usercart() { $this->default_file(); $responseData = array(); $id = $this->input->post('id'); $cartDetails = array(); $product_id = array(); if(!empty($id)) { $cartData = $this
public function usercart()
{
$this->default_file();
$responseData = array();
$id = $this->input->post('id');
$cartDetails = array();
$product_id = array();
if(!empty($id))
{
$cartData = $this->apm->getUserCartDetails($id);
if(!empty($cartData['result']))
{
foreach($cartData['result'] as $key => $cart)
{
$cartDetails[$key]['cart_product_id'] = $cart['cart_product_id'];
$cartDetails[$key]['cart_qty'] = $cart['cart_qty'];
$cartDetails[$key]['cart_price'] = $cart['cart_price'];
//$cartDetails[$key]['cart_product_price'] = $cart['cart_product_price'];
}
$responseData['cart_details'] = $cartDetails;
}
else
{
$responseData['cart_details'] = [];
}
$product_id = $cartDetails[$key]['cart_product_id'];
print_r($product_id);
$productData = array();
if(!empty($product_id))
{
$productDetails = $this->apm->productDetails($product_id);
if(!empty($productDetails['result']))
{
foreach($productDetails['result'] as $key => $prod)
{
$productData[$key]['product_img'] = base_url().'Images/ProductImages/'.$prod['product_img'];
$productData[$key]['p_id'] = $prod['p_id'];
$productData[$key]['product_title'] = $prod['product_title'];
$productData[$key]['product_color'] = $prod['product_color'];
$productData[$key]['product_size'] = $prod['product_size'];
}
$responseData['product_details'] = $productData;
}
else
{
$responseData['product_details'] = [];
}
}
if(!empty($responseData))
{
$responseArray = array(
'apiName' => 'user cart',
'version' => '1.0.0',
'responseCode' => 200,
'responseMessage' => "User cart list",
'responseData' => $responseData
);
}
else
{
$responseArray = array(
'apiName' => 'user cart',
'version' => '1.0.0',
'responseCode' => 204,
'responseMessage' => "User cart is empty",
'responseData' => $responseData
);
}
}
else
{
$responseArray = array(
'apiName' => 'user cart',
'version' => '1.0.0',
'responseCode' => 200,
'responseMessage' => "User cart is empty",
'responseData' => $responseData
);
}
echo json_encode($responseArray);
die();
}
这段代码的模态在这里
public function getUserCartDetails($id)
{
$arrData = array();
$this->db->select('*');
$this->db->from('cart');
$this->db->where('cart_user_id', $id);
$result = $this->db->get()->result_array();
if(!empty($result))
{
$arrData['result'] = $result;
}
else
{
$arrData['result'] = '';
}
return $arrData;
}
public function productDetails($id = array())
{
$arrData = array();
$this->db->select('*');
$this->db->from('product');
$this->db->where('p_id', $id);
$result = $this->db->get()->result_array();
if(!empty($result))
{
$arrData['result'] = $result;
}
else
{
$arrData['result'] = '';
}
return $arrData;
}
我在购物车表中有两个具有相同用户id的产品。我单击我的购物车图标,它将显示两个产品详细信息,但它仅显示一个产品详细信息。但是我可以看到这里我使用print\r传递了两个产品id,如果我使用一个变量来存储这些产品id,并使用foreach循环来获取每个产品的详细信息,它会弹出一个传递无效参数的错误