I'm trying to create XML File from API (in this case Instagram API). But i face a problem since i create it with forEach function. Look at productName and productPrice attribute from product section:
<product productName="No Title" productID="001" thumbPath="http://IMAGE_URL" productPrice="0">
<details>
detail1
</details>
</product>
<product productName="Sushi Homemade " productID="002" thumbPath="http://IMAGE_URL" productPrice="50.000 ">
<details>
detail2
</details>
</product>
<product productName="Sushi Homemade " productID="003" thumbPath="http://IMAGE_URL" productPrice="50.000 ">
<details>
detail3
</details>
</product>
Value productName="Sushi Homemade " and productPrice="50.000 " is just for productID="002". But why the value also filled to next productID >= 002.
Here's the snippet to creating xml with PHP. Maybe i've gonna wrong with it:
<products>
<category categoryName="Instagram">
<?php foreach ($contents->data as $data) {
foreach($data->comments->data as $comment){
if(preg_match('/#title/', $comment->text)){
$komen = preg_replace('/#title/', '', $comment->text);
break;
} else { $komen = 'No Title'; }
}
foreach($data->comments->data as $comment){
if(preg_match('/#price/', $comment->text)){
$harga = preg_replace('/#price/', '', $comment->text);
break;
} else { $harga = '0'; }
}
echo '<product productName="'. $komen .'" productID="' . $data->id . '" thumbPath="' . $data->images->thumbnail->url . '" productPrice="'. $harga .'">
<details>
<![CDATA[
<img src="' . $data->images->low_resolution->url . '" width="100%"/>
' . $data->caption->text . '
]]>
</details>
</product>