Hi there,
I'm having a problem with
simplexml_load_file
in php5. I'm quite familiar with php coding in general as I began teaching myself little-by-little a couple years ago. But I've been busy with other things and haven't done any coding for quite a while. I've never done any really major programming, but if the code is clean I can usually decipher it fairly well.
So anyway, I'm trying to output an xml file using a
foreach
loop. Unfortunately, the script is only printing the first record/item in my xml file rather than printing out the entire list. I'm going to be using this as an include in virtually every other script/page in my sites, so it's important I get it right.
The PHP is:
<?php
if (file_exists($myRoot . 'data/navigation.xml')) {
$xml = simplexml_load_file($myRoot . 'data/navigation.xml');
echo '<dl>';
foreach($xml->section->title as $sectionTitle) {
echo '<dt>' . $sectionTitle . '</dt>';
foreach($xml->section->description as $sectionDescription) {
echo '<dd>' . $sectionDescription . '</dd>';
}
}
echo '</dl>';
} else {
exit('Failed to open navigation.');
}
?>
The XML is:
<?xml version="1.0" encoding="UTF-8" ?>
<navigation>
<section>
<title>portfolios</title>
<description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce luctus. Aenean posuere, diam quis feugiat rhoncus, felis magna varius felis, ut convallis augue ipsum sit amet lorem. Donec sed metus et neque facilisis gravida. Sed eleifend est id purus.</description>
<category></category>
</section>
<section>
<title>galleries</title>
<description>Maecenas orci dui, dapibus non, consectetuer at, consequat rutrum, lectus. Morbi blandit rhoncus libero. Fusce viverra dui. Aenean ac purus. Sed in sem eget nisi sodales fringilla. Duis sit amet turpis id tellus posuere tempus. Maecenas tincidunt dignissim erat. Nullam vitae mauris. Aliquam erat volutpat.</description>
<category></category>
</section>
<section>
<title>libraries</title>
<description>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Fusce luctus. Aenean posuere, diam quis feugiat rhoncus, felis magna varius felis, ut convallis augue ipsum sit amet lorem. Donec sed metus et neque facilisis gravida. Sed eleifend est id purus.</description>
<category></category>
</section>
</navigation>
There are more records than this in the text xml file, but I truncated it for use on this forum.
Instead of printing out a definition list containing all of the "sections", the script is only printing out the first item titled "portfolios".
Does anybody know what I need to do to fix this? If so, could you also explain why? I'm not satisfied just having something work. I like to know exactly how it works, so I can learn more and figure it out on my own in the future.