Jump to content

php question


systomic26

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.