Solved

Parsing XML/RSS - Records keep repeating in output

Posted on 2011-02-26
2
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 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