• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 280
  • Last Modified:

Parsing XML/RSS - Records keep repeating in output

Experts, I'm trying to parse through the top news stories of the day, insert them into my database and allow my users to discuss the topics.

I created the following code to go through the top political headlines.

It displays the stories, but each one is repeated over and over again.
For example, the first story probably appears over a hundred times throughout my output.

I think I have an issue with my foreach() loop.  
Does anything stand out as the issue?

Thanks in advance,

//DB Connect info here

 $feedURL = 'http://feeds.nytimes.com/nyt/rss/Politics';
 $doc = new DOMDocument();
 $arrFeeds = array();
 foreach ($doc->getElementsByTagName('item') as $node) {
  $itemRSS = 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
  array_push($arrFeeds, $itemRSS);
  foreach($arrFeeds as $arrItem) 
   echo "Story Title:  ";
   echo $arrItem['title'];
   echo "<br>";

   echo "Description:  ";
   echo $arrItem['desc'];
   echo "<br>";
   echo "Click link to view:  <A HREF='" . $arrItem['link'] . "'>";
   echo $arrItem['link'];
   echo "</A><br>";

   echo "Published Date:  ";
   echo $arrItem['date'];

   echo "<hr>";

// Insert into database here



Open in new window

1 Solution
Lukasz ChmielewskiCommented:
change line 17 from
foreach($arrFeeds as $arrItem)
$arrItem = $itemRSS;
dsg138Author Commented:
Perfect, Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now