Tag cloud
themes weight meteor camera loops wrc vehicle render effects helicopter environment special effects demo composite rally web update veyron unwrap site online game bbc l4d interactive update match tutorial physics web design directx reaction motorway red bull multiplayer character progress car rss 2009 rigging black hawk machines unreal cars bullet time modelling crash f1 map matrix html5
Categories
Featured work
Finished animations
Interactive features
Maps and mods
Work In progress
Character animation and loops
Images
Site news and updates
Tutorials
Random blog posts
Uncategorised
Finished animations
Interactive features
Maps and mods
Work In progress
Character animation and loops
Images
Site news and updates
Tutorials
Random blog posts
Uncategorised
Most popular
How to Build a Car Rig in 3ds Max
Car Rig Project - 3ds max - Updated
Unreal Tournament 3 - UT3 - City Project
Car Rig Tutorial - 3ds Max and Reactor
Motorway and Foliage - Grass
Car Rig Project - 3ds max - Updated
Unreal Tournament 3 - UT3 - City Project
Car Rig Tutorial - 3ds Max and Reactor
Motorway and Foliage - Grass
Archive
August 2010 [1]
July 2010 [3]
June 2010 [4]
May 2010 [3]
April 2010 [3]
March 2010 [3]
February 2010 [6]
January 2010 [1]
October 2009 [2]
September 2009 [3]
August 2009 [4]
July 2009 [2]
June 2009 [3]
May 2009 [1]
April 2009 [7]
March 2009 [2]
February 2009 [3]
January 2009 [3]
December 2008 [4]
November 2008 [1]
October 2008 [7]
September 2008 [3]
July 2008 [1]
July 2010 [3]
June 2010 [4]
May 2010 [3]
April 2010 [3]
March 2010 [3]
February 2010 [6]
January 2010 [1]
October 2009 [2]
September 2009 [3]
August 2009 [4]
July 2009 [2]
June 2009 [3]
May 2009 [1]
April 2009 [7]
March 2009 [2]
February 2009 [3]
January 2009 [3]
December 2008 [4]
November 2008 [1]
October 2008 [7]
September 2008 [3]
July 2008 [1]
Blog
"xml"
Items tagged with xml:
Creating Dynamic RSS Feeds in PHP
APR
12
2009
I'll assume you have content being fed from an SQL database, know how to connect to the database from your website to display the information, and this is the content you expect to be put into your feed. It's quite a basic tutorial, so aimed at PHP novices, as I guess many tutorials are as that's kind of the point.
To begin with, create a blank .xml file on your website, and give it full control access in your web host settings. Mine's in the root of my website - http://www.adamgleeson.co.uk/rss.xml.
Next, in your admin area, make the rss.php page. This needs to include your database connection information as usual.
Then from this point onwards, the RSS/XML content will be added to as one single variable in PHP. This means the file will effectively be one long line, but web browsers and feed readers will sort this out and make it into a sensible layout as long as all the tags are correct.
The XML file needs to start with a definition and a channel open tag:
$xml = "
$xml .= "
Note the ".=" to keep adding to what is previously in the variable without discarding it.
Continue by adding title and description tags for your feed. A title tag I would say is the minimum, but if you don't even want that you can skip this section I guess.
$xml .= "
$xml .= "
$xml .= "http://adamgleeson.co.uk/";
$xml .= "
Now for the feedy goodness. Grab your SQL from the database like you would for a normal page:
$sql = "SELECT * FROM table_name LIMIT 25";
$rs = mysql_query($sql);
while($rss = mysql_fetch_array($rs)){
And open up tags to start a new feed item:
$xml .= "
$xml .="
Add the title of the item, or whatever your table field heading is, then close the item tag.
$xml .= $rss["title"];
$xml .= ";
Next comes the description. In my description I just have the date and category, rather than reformatting the text:
$xml .= "
$xml .= $rss["date"];
$xml .= "(";$xml .= $rss["category"];
$xml .= ")";
$xml .= "
Last tag to be entered in my feed is the all-important link. Just to make it easier, I've linked it to the ID of the relevant item, which the page then uses PHP GET to decide what to display by grabbing the ID from the URL.
$xml .= "http://www.adamgleeson.co.uk/blog/?article=";
$xml .= $rss["id"];
$xml .= "";
Remember to close the item tag, then rinse and repeat as necessary. If all your data is contained within one table, this should be enough to display everything. The close bracket here is to end the while loop, while the close item tag only ends this current part of the feed, before continuing down the rest of the table as required.
$xml .= "";
}
Finally, close your channel and RSS tags that are still open.
$xml .= "";
$xml .= "";
Now to actually write the file. Using the empty file we created earlier, the PHP fwrite command is used to add the $xml variable we have just created into the file. Remember that PHP will only allow relative links in this stage.
Last thing to do then is simply to close the PHP tag.
$file = fopen("../../rss.xml", "w");
fwrite($file, $xml);
fclose($file);?>
To add some confirmation that it has been created (or rather, that nothing went wrong if you see no error), a simple link to the file is always helpful.
And that's it, you should now have a working RSS feed. To make the feed visible in your web browser like on many websites, add this to your head tags on the pages you want the RSS feed to be visible:
As with most web things, there are many ways of doing the same thing, and this is simply one of them. I'm not suggesting it is the best way, but it works for me, so I hope this will help someone. I appreciate any comments, let me know if it's any use to you, or any requests for future tutorials!
Related posts
RegisterLogin
12/04/2009 - 18:30:57
Post a comment here