systomic26 Posted December 9, 2008 Share Posted December 9, 2008 Hey all I got this script: $x = new SimpleXmlElement($content); foreach($x->channel->item as $entry) { echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>"; } This should take an rss xml feed and save the title with a link to the original post as $entry. The problem is that instead of the foreach() function I need a function or a different way to do this that will just take the first item on the list. Please help this is driving me nuts. Link to comment https://forums.phpfreaks.com/topic/136180-php-question/ Share on other sites More sharing options...
mrdamien Posted December 9, 2008 Share Posted December 9, 2008 <?php $x = new SimpleXmlElement($content); $children = $x->children(); $entry = $children[0]; // Child[0] is the first element. echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>"; ?> Link to comment https://forums.phpfreaks.com/topic/136180-php-question/#findComment-710333 Share on other sites More sharing options...
systomic26 Posted December 9, 2008 Author Share Posted December 9, 2008 ok that worked kind of, when i run the script it displays the title of the rss feed. this is the first title xml tag. So I guess what I need is to get the second child, i guess. Is this possible? Link to comment https://forums.phpfreaks.com/topic/136180-php-question/#findComment-710408 Share on other sites More sharing options...
systomic26 Posted December 9, 2008 Author Share Posted December 9, 2008 so I guess it can't be done then? Link to comment https://forums.phpfreaks.com/topic/136180-php-question/#findComment-710737 Share on other sites More sharing options...
mrdamien Posted December 10, 2008 Share Posted December 10, 2008 If you mean the second child, then use: <?php $x = new SimpleXmlElement($content); $children = $x->children(); $entry = $children[1]; // Child[1] is the second element. echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>"; ?> If you mean the first sub-child of the first node, then use: <?php $x = new SimpleXmlElement($content); $level1 = $x->children(); $level2 = $level1->children(); $entry = $level2[0]; echo "<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>"; ?> Link to comment https://forums.phpfreaks.com/topic/136180-php-question/#findComment-711055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.