Solved

Create while loop for feed

Posted on 2011-09-03
3
307 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 30

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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now