-1

When using the below code I get

Notice: Undefined index: title in /home/.../php on line 34
Notice: Undefined index: date in /home/.../php on line 37

I'm not sure why its doing it!

  <?php
$rss = new DOMDocument();
$rss->load('http://www.example.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 

        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,

        );
    array_push($feed, $item);
}
$limit = 2;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';

    echo '<p>'.$description.'</p>';
}

?>

hannah
  • 3
  • 2

2 Answers2

0

Because you have not defined title and date value in $feed array.So it's been treated as undefined since the index is not specified.Try this:

$item = array ( 
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
Joke_Sense10
  • 5,341
  • 2
  • 18
  • 22
0

You push $item array into $feed array. In $feed array no such key value available for title and date.

$item = array ( 

    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'date' => $node->getElementsByTagName('date')->item(0)->nodeValue
    );

I am assuming for title and date you have value if not set appropriate value.

Roopendra
  • 7,674
  • 16
  • 65
  • 92