0

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>
michi
  • 6,565
  • 4
  • 33
  • 56
cutez7boyz
  • 791
  • 8
  • 16
  • What is your question, what doesn't work? – Pekka Jan 10 '14 at 01:46
  • possible duplicate of [How to convert Instagram API in JSON to XML with PHP?](http://stackoverflow.com/questions/21016976/how-to-convert-instagram-api-in-json-to-xml-with-php) – Giacomo1968 Jan 10 '14 at 01:47
  • Also this. http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml – Giacomo1968 Jan 10 '14 at 01:47
  • No, it's new different problem. Look at for XML results: http://pastebin.com/ubGGyp9b (I can't paste xml formatted here). The problem is: Value productName="Sushi Homemade " and productPrice="50.000 " is just for productID="002". But why the value also filled to next productID >= 002. – cutez7boyz Jan 10 '14 at 01:50

0 Answers0