Solved

Create while loop for feed

Posted on 2011-09-03
3
313 Views
Last Modified: 2012-05-12
So I am pulling in and parsing an rss feed with this script:

<?php
error_reporting(E_ALL);

$xml = file_get_contents('http://feedurlhere');

// MUNG THE XML BECAUSE WE DO NOT HAVE THE NAMESPACE INFORMATION
$xml = str_replace('content:encoded', 'content__encoded', $xml);
$xml = str_replace('isc:',            'isc__',            $xml);

// CONVERT THE XML TO AN OBJECT
$obj = SimpleXML_Load_String($xml, 'SimpleXMLElement', LIBXML_NOCDATA);


// GET SOME FIELDS
$item_title = $obj->channel->item->title;
$item_link = $obj->channel->item->link;
$item_description = $obj->channel->item->description;

echo "$item_image<br/><strong>$item_title</strong> $item_description<br/>";

?>


My question is... Currently this script just returns the variables from the latest entry in the feed. How do I create something like a while loop that returns everything in the feed?
0
Comment
Question by:pixelscape
3 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 36478875
Try this:

foreach ($obj->channel->item as $element){
  echo "title is " . $element->title . "<br />";
  echo "item_link is " . $element->item_link . "<br />";
  echo "item_description is " . $element->item_description . "<br />"
}

Cheers
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 36479126
...everything in the feed?  That might be a lot of stuff.  But marqusG has the iterator correct.  No points for this answer, please.  You might want to think about a usort() and a foreach() construct that gives you access to the last five entries, or something like that.  Best regards, ~Ray
0
 

Author Comment

by:pixelscape
ID: 36482497
Thanks for the input guys, based my script on marqus' foreach suggestion. Works great.
0

Featured Post

Industry Leaders: 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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
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.
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 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 …

733 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