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.Whoa, last night I must have been so burnt out that my eyeballs were beginning to fail me. I just woke up this morning, made a quick tweak, and now it's working. For future reference to anyone else who might like to try SimpleXML in the future, I'm posting the updated code below. I've changed a couple other things, such as making my chosen variables simpler and easier to remember.
<?php
if (file_exists($myRoot . 'data/navigation.xml')) {
$xml = simplexml_load_file($myRoot . 'data/navigation.xml');
echo '<dl>';
foreach($xml->section as $section) {
echo '<dt>' . $section->title . '</dt>';
echo '<dd>' . $section->description . '</dd>';
}
echo '</dl>';
} else {
exit('Failed to open navigation.');
}
?>
Hope this helps somebody else, though I admit, it's really an elementary problem. Another question: I'd really like to put this into an OO class, but I haven't yet studied classes enough to wrap my head around how to write them. If somebody would be willing to give me a quick example using the piece of code above, I'd be grateful. Once I understand the logic, I can apply it to all of my little script snippets that, up until now, I've been simply re-working for each situation. I already understand and can write functions, but classes are a different story.
Thanks a bunch!
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.Whoa, last night I must have been so burnt out that my eyeballs were beginning to fail me. I just woke up this morning, made a quick tweak, and now it's working. For future reference to anyone else who might like to try SimpleXML in the future, I'm posting the updated code below. I've changed a couple other things, such as making my chosen variables simpler and easier to remember.
<?php
if (file_exists($myRoot . 'data/navigation.xml')) {
$xml = simplexml_load_file($myRoot . 'data/navigation.xml');
echo '<dl>';
foreach($xml->section as $section) {
echo '<dt>' . $section->title . '</dt>';
echo '<dd>' . $section->description . '</dd>';
}
echo '</dl>';
} else {
exit('Failed to open navigation.');
}
?>
Hope this helps somebody else, though I admit, it's really an elementary problem. Another question: I'd really like to put this into an OO class, but I haven't yet studied classes enough to wrap my head around how to write them. If somebody would be willing to give me a quick example using the piece of code above, I'd be grateful. Once I understand the logic, I can apply it to all of my little script snippets that, up until now, I've been simply re-working for each situation. I already understand and can write functions, but classes are a different story.
Thanks a bunch!