Solved

Parsing XML/RSS - Records keep repeating in output

Posted on 2011-02-26
2
269 Views
Last Modified: 2013-11-18
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,

-dsg
<?php 
//DB Connect info here

 $feedURL = 'http://feeds.nytimes.com/nyt/rss/Politics';
 $doc = new DOMDocument();
 $doc->load($feedURL);
 $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);
  //print_r($arrFeeds);
  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

0
Comment
Question by:dsg138
2 Comments
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34988006
change line 17 from
foreach($arrFeeds as $arrItem)
to
$arrItem = $itemRSS;
0
 

Author Comment

by:dsg138
ID: 34988052
Perfect, Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now