Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

Create while loop for feed

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
pixelscape
Asked:
pixelscape
1 Solution
 
Marco GasiFreelancerCommented:
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
 
Ray PaseurCommented:
...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
 
pixelscapeAuthor Commented:
Thanks for the input guys, based my script on marqus' foreach suggestion. Works great.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now