Introduction:
Ever wondered how you create an RSS feed dynamically? It's actually pretty simple. With some use of PHP's header() function, it's not a problem at all.
[php]<?php
$output = '
<rss version="2.0">
<channel>';
mysql_connect('localhost','user','pass');
mysql_select_db('news');
//set the content type to xml
header("Content-Type: text/xml");
$sql = mysql_query("SELECT * FROM news LIMIT 10");
while($res = mysql_fetch_array($sql))
{
$title = $res['title'];
$description = $res['description'];
$author = $res['author'];
$id = $res['id'];
$link = "http://www.yoursite.com/news.php?id=$id";
$output .= <<<EOT
<item>
<title>The Your Site News</title>
<link>http://www.yoursite.com/</link>
<description>$description</description>
</item>
EOT;
}
$output .= '
</channel>
</rss>';
echo $output;
?>[/php]
Explanation:
We start connecting to the database and whatnot, then we use the header() function to tell the user that this is an RSS feed. We then move on to grabbing the news article from the database. Once thats done we loop through the information and then output it.
Voila! You have a dynamically created RSS feed!