Solved

Parsing XML/RSS - Records keep repeating in output

Posted on 2011-02-26
2
273 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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