0

Please read carefully and I appreciate for your help and answers.

My code is getting data from xml and show but its works just on my categories.xml not on my products.xml.

This is my categories.xml structure:

 <?xml version="1.0"?>
<categories>
<category>
<categories_name>name</categories_name>
<sort_order>order number</sort_order>
</category>
<category>
<categories_name>name</categories_name>
<sort_order>order number</sort_order>
 </category>
</categories>

and the code I used

<?php
$xml=simplexml_load_file("categories.xml") or die("Error: Cannot create object");
foreach ($xml->children() as $category): ?>
    <div class="product">
            <img src="<?= $category->categories_name; ?>"><br>
            <?= $category->sort_order; ?>
    </div>
<?php endforeach; ?>

and its working perfectly.

But when I tried this code on my products.xml its showing blank. Here is my products.xml:

<TABLE-RECORDS>
<EXPORT-RECORDS>
<PRODUCT>
  <PRODUCTS_NAME>name </PRODUCTS_NAME>
  <PRODUCTS_DESCRIPTION>details</PRODUCTS_DESCRIPTION>
  <PRODUCTS_IMAGE>number</PRODUCTS_IMAGE>
  <PRODUCTS_PRICE>$</PRODUCTS_PRICE>
</PRODUCT>
<PRODUCT>
  <PRODUCTS_NAME>name </PRODUCTS_NAME>
  <PRODUCTS_DESCRIPTION>details</PRODUCTS_DESCRIPTION>
  <PRODUCTS_IMAGE>number</PRODUCTS_IMAGE>
  <PRODUCTS_PRICE>$</PRODUCTS_PRICE>
</PRODUCT>
</EXPORT-RECORDS>
</TABLE-RECORDS>

and here is the php code for fetching:

<?php $xml=simplexml_load_file("products.xml") or die("Error: Cannot create object"); 
foreach ($xml->children() as $product): ?>
    <div class="product">
            <?= $product->PRODUCTS_NAME; ?>
    </div>

I think I'm getting error on TABLE-RECORDS. I checked many times but I didn't fetch any thing ... it shows me a blank page.

1 Answer 1

1

I think you forget to find the sub child, can you try

<?php $xml=simplexml_load_file("products.xml") or die("Error: Cannot create object"); 
foreach ($xml->children()->children() as $product): ?>
  <div class="product">
    <?= $product->PRODUCTS_NAME; ?>
  </div>
}
Sign up to request clarification or add additional context in comments.

1 Comment

OMG how can i forget this lol :P :D i'm working on this whole night i think i need rest now Thank you so much :P :D FRESH MIND GET FRESH IDEA's :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.