0

I have a php file setup to pull through ONE XML data feed, What I would like to do is load up to 4 feeds into it and if possible make it select a random item too. Then parse that into an jQuery News Ticker.

My current PHP is as follows...

<?php
$feed = new DOMDocument();
$feed->load('/feed');
$json = array();

$json['title'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
$json['description'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
$json['link'] = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('link')->item(0)->firstChild->nodeValue;

$items = $feed->getElementsByTagName('channel')->item(0)->getElementsByTagName('item');

$json['item'] = array();
$i = 0;


foreach($items as $item) {

   $title = $item->getElementsByTagName('title')->item(0)->firstChild->nodeValue;
   $description = $item->getElementsByTagName('description')->item(0)->firstChild->nodeValue;
   $pubDate = $item->getElementsByTagName('pubDate')->item(0)->firstChild->nodeValue;
   $guid = $item->getElementsByTagName('guid')->item(0)->firstChild->nodeValue;

   $json['item'][$i++]['title'] = $title;
   $json['item'][$i++]['description'] = $description;
   $json['item'][$i++]['pubdate'] = $pubDate;
   $json['item'][$i++]['guid'] = $guid; 

   echo '<li class="news-item"><a href="#">'.$title.'</a></li>';

}


//echo json_encode($json);


?>

How can I modify this to load more than one feed into the file?

Thanks in advance

9
  • It looks like this is possible to rewrite this, yeah. Do you have a specific question about the task? Commented Jun 13, 2012 at 14:23
  • 1
    What I would like to do is load up to 4 feeds into it and if possible make it select a random item too Commented Jun 13, 2012 at 14:24
  • 1
    .... This site sometimes is useful, sometimes isnt. How can I get a vote down? My question is quite clear nothing short of putting it into bold! Commented Jun 13, 2012 at 14:37
  • 2
    Again! What exactly is your programming question? Commented Jun 13, 2012 at 14:49
  • 2
    Look, you say you are a web dev and that you work with Zend and CI. From that, one assumes that one knows the basics of the language, and then this question looks really strange and one wonders what the problem is, like I did. Be open about your level of actual knowledge and you'll get better answers Commented Jun 13, 2012 at 14:59

2 Answers 2

1

The simplest approach to doing this is wrapping another loop around the code you have. It's not the cleanest way but will probably suffice for the purpose.

In general, IMO, it's always beneficial to learn the basics of the language first. E.g. PHP manual on foreach

This is roughly what the loop needs to look like:

$my_feeds = array("http://.....", "http://.....", "http://.....");

foreach ($my_feeds as $my_feed)
 {
  // This is where your code starts
  $feed = new DOMDocument();
  $feed->load($my_feed); <--------------- notice the variable
  $json = array();

 ... and the rest of the code

 }

this will walk through all the URLs in $my_feeds, open the RSS source, fetch all the items from it, and output them.

Sign up to request clarification or add additional context in comments.

3 Comments

Thats the principles I'd like yeah. I need to try convert that $feed variable to a string so that the URL's are 'blabla.com' not blabla.com as I am getting the error : Catchable fatal error: Object of class DOMDocument could not be converted to string
@Stu ugh, sorry, my bad. Fixed, try again
Thanks for that @Pekka I then created a function to shuffle the array and it works a treat
0

If I'm reading your question right, what you may want to do is turn your code into a function, which you would then run inside a foreach loop for each url (which you could store in an array or other data structure).

Edit: If you don't know much about functions, this tutorial section might help you. http://devzone.zend.com/9/php-101-part-6-functionally-yours/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.