I got sick of looking at shit pieces of code that either didn’t work properly, or produced XML so badly formatted you couldn’t read the raw source to debug it, so I decided to write my own, based on the BBC’s RSS source.
<?php
/*
* @abstract PHP script to correctly generate an RSS feed
* @author Jon Thompson
* @version 0.1
* @since 11 Dec 2009
*
* Requires an SQL table with the following fields to read:
* - title
* - link
* - description
* - thumbnail
*
* Will generate an on-the fly RSS feed from database-driven content.
* For best results, amend the following and add to your pages in the <head></head> section:
*
* <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.yoursite.com/link/to/this/script/rss.php" />
*
*/
//***** Database login details *****
$db_server = "localhost";
$db_database = "database";
$db_username = "username";
$db_password = "password";
//***** RSS feed details *****
$rss_title = "Your title here";
$rss_link = "URL to web page that displays these items";
$rss_description = "An overall description of your feed";
$rss_language = "en-gb";
$rss_copyright = "Your copyright notice";
$rss_docs = "URL to a page explaining distribution and use of these articles";
//***** SQL query to generate list *****
$sql = "SELECT * FROM table limit 15";
//############################################################################################################
//# NOTHING FURTHER TO EDIT FROM THIS POINT ON #
//############################################################################################################
//***** Connect to database *****
$db = mysql_connect($db_server, $db_username, $db_password) or die("Unable to connect to MySQL server\n\n");
mysql_select_db($db_database, $db) or die("Unable to select database\n\n");
//***** Execute the SQL query *****
$result = mysql_query($sql, $db);
//***** Loop through the results to get the rest of the feed *****
if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$output .= " <item>
<title>".htmlentities($row["title"])."</title>
<link>".htmlentities($row["link"])."</link>
<description>".htmlentities($row["description"])."</description>
<media:thumbnail url=\"".htmlentities($row["thumbnail"])."\" />
</item>
";
}
}else{
$output .= " <item>
<title>No entries</title>
<link></link>
<description>Sorry, there are no new entries to view</description>
<media:thumbnail url=\"\" />
</item>
";
}
//***** Build the entire feed from our results *****
//***** This will also format it correctly so it's readble in plain text format *****
$output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
<?xml-stylesheet title=\"XSL_formatting\" type=\"text/xsl\" href=\"/shared/bsp/xsl/rss/nolsol.xsl\"?>
<rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss\">
<channel>
<title>$rss_title</title>
<link>$rss_link</link>
<description>$rss_description</description>
<language>$rss_language</language>
<lastBuildDate>".date("D, d M Y H:i:s T")."</lastBuildDate>
<copyright>$rss_copyright</copyright>
<docs>$rss_docs</docs>
<ttl>15</ttl>
".$output." </channel>
</rss>\n";
//***** Output the feed correctly *****
header("Content-Type: text/xml");
echo $output;
exit(0);
?>
![Subscribe to [serpents kiss dot com]](http://www.serpentskiss.com/wordpress/wp-content/themes/black-hat/images/rss_feed.gif)
Newcastle United defender Steven Taylor is hoping the side can clinch the Championship after claiming the Sport Newcastle Sports Personality award.
Newcastle striker Peter Lovenkrands says there is no pressure on the players to maintain their unbeaten home record.
Peter Lovenkrands and Danny Guthrie both score twice as Newcastle ease to victory over 10-man Barnsley.
Newcastle director Derek Llambias pleads with fans to end their abuse of Mike Ashley, claiming the owner has saved the club from administration.
The last surviving member of Newcastle United's 1951 FA Cup winning team dies at the age of 85. 