0

I am pretty new to PHP and hope someone can help me with the following:

I have a SQL table from where I fetch data with a stored procedure. This returns the below XML. Then on my PHP page I load this XML as $objCat in order to echo it on the site with the script below.

This works so far to echo just the category groups but not for the items belonging to each group, i.e. the ones with the same category. I guess I need to nest another foreach loop here (where I have my comment below) so that it looks similar to the original XML but couldnt get this to work yet.

My XML:

<ranks>
  <categories>
    <categoryX>Category 1</categoryX>
    <groupCount>3</groupCount>
      <itemID>ID 1</itemID>
        <dateX>2013-11-12</dateX>
        <subjectX>Subject 11</subjectX>
      <itemID>ID 2</itemID>
        <dateX>2013-11-05</dateX>
        <subjectX>Subject 7</subjectX>
      <itemID>ID 3</itemID>
        <dateX>2013-10-23</dateX>
        <subjectX>Subject 2</subjectX>
  </categories>
  <categories>
    <categoryX>Category 2</categoryX>
    <groupCount>2</groupCount>
      <itemID>ID 4</itemID>
        <dateX>2013-11-27</dateX>
        <subjectX>Subject 6</subjectX>
      <itemID>ID 5</itemID>
        <dateX>2013-10-30</dateX>
        <subjectX>Subject 3</subjectX>
    </categoryX>
  </categories>
  // ...
</ranks>

My PHP:

<?php 
    foreach ($objCat->categories as $cat) { 
        $catGroup = $cat->categoryX; 
        echo $catGroup;
        // echo all itemIDs below each other where categoryX = $catGroup.
    }
?>
3
  • Since you have a few levels of nesting, try to create a recursive function that prints an item and calls recursively to all the nested "children" Commented Dec 1, 2013 at 22:09
  • Thanks. How do I do that ? Basically I just need two levels: one for the category groups and then below for the items belonging to that category. Commented Dec 1, 2013 at 22:14
  • Which XML parser are you using? Commented Dec 1, 2013 at 22:16

2 Answers 2

2

Assuming you have only one level of nesting:

$xml = simplexml_load_string($theXML);
foreach ($xml->categories as $cat) { 
    echo "category: \n";
    foreach ($cat as $key => $value) {
        echo $key . " " . $value."\n";
    }
}

After fixing an incorrect closing tag (</categoryX>) in the example xml you posted above, I get the following result:

category: 
categoryX Category 1
groupCount 3
itemID ID 1
dateX 2013-11-12
subjectX Subject 11
itemID ID 2
dateX 2013-11-05
subjectX Subject 7
itemID ID 3
dateX 2013-10-23
subjectX Subject 2
category: 
categoryX Category 2
groupCount 2
itemID ID 4
dateX 2013-11-27
subjectX Subject 6
itemID ID 5
dateX 2013-10-30
subjectX Subject 3
Sign up to request clarification or add additional context in comments.

Comments

1

If you're sing simplexml, it should be as straight forwards as...

foreach ($objCat->categories as $cat) { 
    $catGroup = $cat->categoryX; 
    echo $catGroup;
    $index = 0;
    foreach($cat->itemID as $itemID)
    {
      echo $itemID;
      echo $cat->dateX[$index];
      echo $cat->subjectX[$index];
      $index++;
    }

}

If you have control over your XML structure I think you should nest your "child" elements differently...

<categories>
  <categoryX>Category 1</categoryX>
  <groupCount>3</groupCount>
  <childdata>
    <itemID>ID 1</itemID>
    <dateX>2013-11-12</dateX>
    <subjectX>Subject 11</subjectX>
  </childdata>
  <childdata>
    <itemID>ID 2</itemID>
    <dateX>2013-11-05</dateX>
    <subjectX>Subject 7</subjectX>
  </childdata>
  <childdata>
    <itemID>ID 3</itemID>
    <dateX>2013-10-23</dateX>
    <subjectX>Subject 2</subjectX>
  </childdata>
</categories>

And then you'll have a single object with all your values for each iteration of the "childdata" node...

foreach ($xml->categories as $cat) { 
  $catGroup = $cat->categoryX; 
  echo $catGroup;
  foreach($cat->childdata as $child)
  {
    echo $child->itemID;
    echo $child->dateX;
    echo $child->subjectX;
    $index++;
  }
}

2 Comments

Thanks a lot ! - I decided for this one as the comments on the XML help me a lot as well.
Please see my edit, I couldn't get the numerical index for each iteration of the first "itemid" node to be able to get the index of the dateX/subjectX elements so I resorted to a counter, I really suggest re-structuring your XML to make looping this data easier.

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.