I am getting an xml from curl and then i am using simplexml_load_string to get values.In that i am getting all my product details and cart total.And I am getting product type from my database.But i don't know how to add that result (which is an object )in productdetail array for each product.I am stuck over here.Can any one help please?
SimpleXMLElement Object
(
[PersonId] => 454545
[SyncedAt] => 2015-10-29T19:31:12
[TotalQuantity] => 3
[Tax] => 8.27
[Discount] => 0
[Total] => 91
[Contacts] => SimpleXMLElement Object
(
[Email] => SimpleXMLElement Object
(
)
[Phones] => SimpleXMLElement Object
(
[Mobile] => SimpleXMLElement Object
(
)
)
)
[ProductDetails] => SimpleXMLElement Object
(
[@attributes] => Array
(
[Type] => Array
[TotalRows] => 2
)
[ProductDetail] => Array
(
[0] => SimpleXMLElement Object
(
[Id] => 1
[ProductId] => 2880
[ColourId] => 2880
[SkuId] => 2880
[ProductCode] => 11FD
[ProductName] => Badminton
[Quantity] => 2
[Price] => 23
[Discount] => 0
[TotalPrice] => 46
)
[1] => SimpleXMLElement Object
(
[Id] => 2
[ProductId] => 2005
[ColourId] => 2005
[SkuId] => 2005
[ProductCode] => 55OK
[ProductName] => GLOVES
[Quantity] => 1
[Price] => 45
[Discount] => 0
[TotalPrice] => 45
)
)
)
[DeliveryOption] => SimpleXMLElement Object
(
[Id] => 522
[Name] => DeliveryCharges
[Value] => 0
)
[DeliveryOptions] => SimpleXMLElement Object
(
[DeliveryType] => SimpleXMLElement Object
(
[Id] => 522
[Name] => DeliveryCharges
[Value] => 0
)
)
[TaxAdjustment] => 0
)
My controller :
$xml = simplexml_load_string($output);
$cartdetail_arr=array();
$data['total'] = $xml->Total;
foreach ($xml->ProductDetails->ProductDetail as $curr_detail) {
//$cartdetail_arr[] = (array)$curr_detail;
$style = $curr_detail->ProductCode;
$prod_type = $this->cart_model->get_prod_type($style);
//print_r($prod_type);exit;
}
$data['cart_detail']= $cartdetail_arr;
My Model :
public function get_prod_type($style){
$sql = "SELECT product_type from product_master where style='$style'";
$query = $this->db->query($sql);
if($query->num_rows() == 0):
return false;
else:
return $query->row();
endif;
}
Output from query :
stdClass Object ( [prod_type] => Sports )
Desired output:
Array
(
[0] => Array
(
[Id] => 1
[ProductId] => 2880
[ColourId] => 2880
[SkuId] => 2880
[ProductCode] => 11FD
[ProductName] => Badminton
[Quantity] => 2
[Price] => 23
[Discount] => 0
[TotalPrice] => 46
[prod_type] => School
)
[1] => Array
(
[Id] => 2
[ProductId] => 2005
[ColourId] => 2005
[SkuId] => 2005
[ProductCode] => 55OK
[ProductName] => GLOVES
[Quantity] => 1
[Price] => 45
[Discount] => 0
[TotalPrice] => 45
[prod_type] => Sports
)
)
I have tried this,but am not getting desired output:
foreach ($xml->ProductDetails->ProductDetail as $curr_detail) {
$cartdetail_arr[] = (array)$curr_detail;
$style = $curr_detail->ProductCode;
$prod_type = new stdClass();
$prod_type = $this->cart_model->get_prod_type($style);
$cartdetail_arr[] = clone $prod_type;
}
And m getting this as output
Array
(
[0] => Array
(
[Id] => 1
[ProductId] => 2880
[ColourId] => 2880
[SkuId] => 2880
[ProductCode] => 11FD
[ProductName] => Badminton
[Quantity] => 2
[Price] => 23
[Discount] => 0
[TotalPrice] => 46
)
[1] => stdClass Object
(
[prod_type] => School
)
[2] => Array
(
[Id] => 2
[ProductId] => 2005
[ColourId] => 2005
[SkuId] => 2005
[ProductCode] => 55OK
[ProductName] => GLOVES
[Quantity] => 1
[Price] => 45
[Discount] => 0
[TotalPrice] => 45
)
[3] => stdClass Object
(
[prod_type] => Sports
)
)