Reputation: 135
I have an rss feed that is formatted something like this:
<item>
<posturl>mysite.com/abc.html</posturl>
<imagepath>123.jpg</imagepath>
</item>
<item>
<posturl>mysite.com/def.html</posturl>
<imagepath>456.jpg</imagepath>
</item>
<item>
<posturl>mysite.com/ghi.html</posturl>
<imagepath>789.jpg</imagepath>
</item>
I have a script where I want to extract both the values above. I can extract one of the values and build html code using code like:
<?php
$xml = simplexml_load_file('http://myrssfeed.com');
$imgs = $xml->xpath('//imagepath');
echo '<ul>';
foreach($imgs as $output1) {
echo '<li><img src="' . $output1 . '" ></li>';
}
echo '</ul>';
?>
This works like a charm thanks to all the help from people here. But I need to expand that second echo line to read something like:
echo '<li><a href="' . $output2 . '"><img src="' . $output1 . '" ></a></li>';
I need help on how to extract $output2 = posturl within the same item as $output1.
Basically I need to display the images as links... both imagepath and posturl are elements within item.
Many thanks.
Upvotes: 1
Views: 352
Reputation: 157967
You could select the <item>
elements instead of <posturl>
and <imagepath>
. Like this:
$xml = simplexml_load_file('http://myrssfeed.com');
echo '<ul>';
foreach($xml->xpath('//item') as $item) {
printf('<li><a href="%s"><img src="%s" /></a></li>',
$item->posturl, $item->imagepath);
}
echo '</ul>';
Upvotes: 1