ethan.ellis Posted July 10, 2008 Share Posted July 10, 2008 Ok I have this feed reader code right and I want to use it on my site <?php $DOM = new DOMDocument(); $feedurl="http://feeds.feedburner.com/FEEDSITE"; $DOM->load($feedurl); $feed = $DOM->getElementsByTagName("item"); $number = 0; foreach($feed as $feedvalue) { $feedtitles = $feedvalue->getElementsByTagName("title"); $feedtitle = $feedtitles->item(0)->nodeValue; $feedtexts = $feedvalue->getElementsByTagName("description"); $feedtext = $feedtexts->item(0)->nodeValue; $feedurls = $feedvalue->getElementsByTagName("link"); $feedurl = $feedurls->item(0)->nodeValue; if($number<2){ echo('<a href="'.$feedurl.'" ><h4>'.$feedtitle.'</h4></a><br/>'.$feedtext."<br/><br/>"); $number++; } } ?> but I want to use many feed like maybe 10 - 40 how could I do this without having to repost the code over. Also I want the post to be in the order from which they have been posted so if site 1 posted a post 30 min ago and site 2 posted a post 3 min go then site 2 must be on top of 1 ... if this can not be done then could I just get a php code that choses a random php code of mine and displays that so everytiem they refreash they get a new feed.. if you understand please help ps I hope this is the right place Link to comment https://forums.phpfreaks.com/topic/114007-random-feed/ Share on other sites More sharing options...
ethan.ellis Posted July 11, 2008 Author Share Posted July 11, 2008 Bump can no1 help me here? Link to comment https://forums.phpfreaks.com/topic/114007-random-feed/#findComment-588034 Share on other sites More sharing options...
chronister Posted July 12, 2008 Share Posted July 12, 2008 to use it over and over without having to recode it... make it a function and pass the needed data and call the function when u need to. As far as the ordering, you will need to determine which one was posted first. I have not looked at feedburner so I have no idea if you can get a "time modified" variable from their class. Link to comment https://forums.phpfreaks.com/topic/114007-random-feed/#findComment-588187 Share on other sites More sharing options...
ethan.ellis Posted July 12, 2008 Author Share Posted July 12, 2008 hmm ya I have no idea what u talking about as I am still trying to code css but I need that code therefore could you show me how to create this function? Link to comment https://forums.phpfreaks.com/topic/114007-random-feed/#findComment-588200 Share on other sites More sharing options...
chronister Posted July 12, 2008 Share Posted July 12, 2008 Creating a function is really easy... it is simply this... <?php function MyFunction($someVar) { echo $someVar; } ?> This is a REALLY simplistic example, but it is the same steps no matter what you want to do... To use it you simply do this. <?php myFunction('Hello'); myFunction('How Are You Today?'); ?> This would then echo those 2 strings I passed into the function. So it would be HelloHow Are You Today? Hello & How are run together on purpose because of the way I called the functions. I did not add any space or anything between them. To apply this to your code we do a similar thing. <?php function GetFeed($feedurl) { $DOM = new DOMDocument(); $DOM->load($feedurl); $feed = $DOM->getElementsByTagName("item"); $number = 0; foreach($feed as $feedvalue) { $feedtitles = $feedvalue->getElementsByTagName("title"); $feedtitle = $feedtitles->item(0)->nodeValue; $feedtexts = $feedvalue->getElementsByTagName("description"); $feedtext = $feedtexts->item(0)->nodeValue; $feedurls = $feedvalue->getElementsByTagName("link"); $feedurl = $feedurls->item(0)->nodeValue; if($number<2){ echo('<a href="'.$feedurl.'" ><h4>'.$feedtitle.'</h4></a><br/>'.$feedtext."<br/><br/>"); $number++; } } } // our function is defined, so now we call it.... GetFeed('http://www.somefeed.com'); GetFeed('http://www.anotherfeed.com'); GetFeed('http://feeds.feedburner.com/path/to/feed'); ?> That's functions in a nutshell. There is LOADS more to learn about them, but this is the basics. They can save a LOT of time and coding by allowing you to write something once, and then re-use it over and over. Take a look in the PHP manual about functions. It is obvious that your not familiar with PHP, so I ask that you do familiarize yourself with it so you can understand this stuff more. Please don't be one of those people that just come here and say "Will someone write this code for me?"... I got one of those I am dealing with tonight as a matter of fact. Hope this helps. Nate Link to comment https://forums.phpfreaks.com/topic/114007-random-feed/#findComment-588210 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.