Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parsing XML/RSS - Records keep repeating in output

Posted on 2011-02-26
2
Medium Priority
?
277 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

609 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