grucker Posted August 7, 2010 Report Share Posted August 7, 2010 I maintain a golf club website and the members want latest news from the English Golf Union. How, exactly would one load a rss feed to a php webpage? Regards David Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 7, 2010 Report Share Posted August 7, 2010 You could use SimpleXML (http://php.net/manual/en/book.simplexml.php) to grab the data and display it. I did a tutorial on this a while back that's available in the KS University -- it's called "Use PHP, jQuery & AJAX to Load XML Data" and it's under PHP > Misc PHP Videos. It shows how to use SimpleXML to grab data from an RSS feed and display it in the page using PHP/jQuery/AJAX. That's how the "Recent Web Design Magazine Posts" section on the KillerSites homepage works. Quote Link to comment Share on other sites More sharing options...
grucker Posted August 17, 2010 Author Report Share Posted August 17, 2010 I have messed about with the function and the page source from the web both shown below but I only get one item of information. I think it is the first one. How do I get the others. Thanks function render_products_from_ROR() { // starts the product table $output = "<table><tr>"; // sets loop to 0 $i = 0; // starts looping through products foreach (get_xml_ror() as $item) { $output .= ' <td> <div> <p style="font-size:.75em;text-align:center;"> '. $item->title.' </p> <p style="font-size:.75em;text-align:center;"> '. $item->description.' </p> </div> </td> '; // checks the loop. If it's >= 2 (indicating that it's looped 3 times)... if ($i >= 0){ // start new row $output .= "</tr><tr>"; // reset loop $i = 0; } // if not, add one to the loop else $i++; } // finishes table $output .= '<td></td></tr></table>'; return $output; } <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet title="XSL_formatting" type="text/xsl" href="rss.xsl"?> <rss version="2.0"> <channel> <title>English Golf Union (EGU) | NEWS</title> <link>http://www.englishgolfunion.org</link> <description>English Golf Union (EGU) | NEWS</description> <language>en-gb</language> <copyright>Copyright English Golf Union (EGU)2010.</copyright> <lastBuildDate>Tue, 17 Aug 2010 13:41:15 GMT</lastBuildDate> <ttl>60</ttl> <item> <guid isPermaLink="false">http://www.englishgolfunion.org-4419</guid> <title><![CDATA[sittingbourne & Milton Regis Golf Club receives EGU/EWGA GolfMark Award]]></title> <link><![CDATA[http://www.englishgolfunion.org/news.asp?id=4419&code=0001000100010029]]></link> <description><![CDATA[]]></description> <pubDate>Tue, 17 Aug 2010 00:00:00 GMT</pubDate> </item> <item> <guid isPermaLink="false">http://www.englishgolfunion.org-4418</guid> <title><![CDATA[Pine Ridge Golf Club receives EGU/EWGA GolfMark Award]]></title> <link><![CDATA[http://www.englishgolfunion.org/news.asp?id=4418&code=0001000100010029]]></link> <description><![CDATA[]]></description> <pubDate>Tue, 17 Aug 2010 00:00:00 GMT</pubDate> </item> <item> <guid isPermaLink="false">http://www.englishgolfunion.org-4416</guid> <title><![CDATA[England on top in Home Internationals again]]></title> <link><![CDATA[http://www.englishgolfunion.org/news.asp?id=4416&code=000100030002]]></link> <description><![CDATA[England retained the Raymond Trophy when they completed a clean sweep in the Home Internationals at Ashburnham in Wales.]]></description> <pubDate>Mon, 16 Aug 2010 00:00:00 GMT</pubDate> </item> <item> <guid isPermaLink="false">http://www.englishgolfunion.org-4414</guid> <title><![CDATA[Hat-trick man Francis lands London Pride Gold Medal]]></title> <link><![CDATA[http://www.englishgolfunion.org/news.asp?id=4414&code=000100020001000300150001]]></link> <description><![CDATA[Kevin Francis from Cheshire completed three wins in a row when he carried off the London Pride EGU Gold Medal in the sunshine at Woodhall Spa. ]]></description> <pubDate>Sun, 15 Aug 2010 00:00:00 GMT</pubDate> </item> </channel> </rss> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 17, 2010 Report Share Posted August 17, 2010 Are you using SimpleXML for this? What does your get_xml_ror() function do? Quote Link to comment Share on other sites More sharing options...
grucker Posted August 17, 2010 Author Report Share Posted August 17, 2010 The following lines of code are in a function folder. The function does pick some info up but only the first item of information. The function worked for items in the webstore. Thanks define('STORE_XML_ROR_FILE' , 'http://www.englishgolfunion.org/rss.asp'); function get_xml_ror() {return new SimpleXMLelement(file_get_contents(STORE_XML_ROR_FILE));} Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 17, 2010 Report Share Posted August 17, 2010 Just because the function worked for the webstore doesn't necessarily mean that it will work for this -- the XML files may (most likely) have a different format. The first place thing I would do would be to figure out what the get_xml_ror() function is returning, and what format it is in. You could do that by adding this to the top of your render_products_from_ROR() function. echo "<pre>"; print_r(get_xml_ror()); echo "</pre>"; exit; You'll notice that the object that is returned has this format: Object > Channel > Item. In your loop, you are getting the title from the Channel, not from the items inside of the channel. I was able to get it to work by doing something slightly different using if ($xml = simplexml_load_file(STORE_XML_ROR_FILE)) { // sets loop to 0 $i = 0; // starts looping through products foreach ($xml->channel->item as $item) { ... code here ... } } else { $output .= 'News cannot be displayed at this time.'; } Quote Link to comment Share on other sites More sharing options...
grucker Posted August 17, 2010 Author Report Share Posted August 17, 2010 Thanks Ben, Just to clarify what you have just said, The short code for the top is only needed to see what format the rss feed is in. I left it out and it worked and when I added it it still worked but with the info you pointed out. One other thing I had to leave the else statement out to get it to work might have something to do with } . Anyway thanks again and on the other nonrelated matter I have persuaded my daughter to allow a picture in the banner. Thanks for that especially. Regards David Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 17, 2010 Report Share Posted August 17, 2010 The short code for the top is only needed to see what format the rss feed is in. If you mean the four lines of code that end with "exit;" above, yes, that was only to show what format the RSS feed is in, so you can make sure you are pulling the correct data. One other thing I had to leave the else statement out to get it to work might have something to do with } Just make sure that it doesn't display an ugly error to the user if the RSS feed isn't available for some reason. You can test this by changing the URL to the RSS feed to something that doesn't exist, and seeing how your code handles it. Quote Link to comment Share on other sites More sharing options...
grucker Posted August 18, 2010 Author Report Share Posted August 18, 2010 Hi, Just out of interest, is it possible to show only the first, ie newest, item and if so how does the code change? Thanks David Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 18, 2010 Report Share Posted August 18, 2010 If you add a "break;" just before the "}" that closes the foreach loop, that should kick you out of the loop. Realistically though, if you only want one item, there should be a way to get just the specific element you need and not have to use a foreach loop at all. I'd have to experiment to figure out the code though. Quote Link to comment Share on other sites More sharing options...
grucker Posted August 18, 2010 Author Report Share Posted August 18, 2010 Thanks Ben, Works a treat. Just need a discount code now. I will have another go. Thanks again. David Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.