Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/seopher/seopher.com/how-to-write-rss-generator2.php on line 40


Tutorials: How to write a PHP & MySQL RSS Feed Generator


Right, on the last page we set the variables up defining your descriptions etc, and we connected to your database to get your new content, now it's time to write your RSS feed generator.


Start writing the XML document
Right, everything is set up, let's start writing your RSS XML document! Because we set the variables up earlier, the code (below) should work as-is.


echo
'<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">
<channel>
<title>'.$rss_title.'</title>
<link>http://www.'.$rss_site.'</link>
<description>'.$rss_description.'</description>
<language>en-en</language>
<image>
<url>'.$rss_logo.'</url>
<title>'.$rss_site.'</title>
<link>http://www.'.$rss_site.'</link>
</image>';

That sets up the RSS channel for your feed, defining your web address, the brief description of your feed, the language, a logo (remember, if you didn't define a logo earlier, you should probably remove that bit). This sets up the channel that all your RSS items (the articles/blogs) will be published under.


Writing your items
Now the channel is all set up, it's time to write each of your articles/blogs into the file. To do this, it's good to loop through the last 5-6 items in your database to add a small history to your RSS from day one (that's the purpose of the for-loop).

for($i=0;$i<6; $i++)
{

$photo_name = 'http://www.yoursite.com/images/whatever-the-image-could-be.gif';

$subject = mysql_result($result,$i,'Article_Title');

$url_product = '/articles/whatever'; //however you would link to your article

$description = mysql_result($result,$i,'Article_Content');

// Clean the description

$description = str_replace ("&","",htmlspecialchars(strip_tags($description)));

// Pass tags to describe the product (this has been left out of this example)

$rss_tags = 'tag1, tag2';

//makes a 500 character long copy of the desciption - a teaser of your content for people to read

$short_description = substr($description,0,500) . "...";

//to record when the feed was published

$timestamp = mysql_result($result,$i,'Time');

//converts the timestamp into a RSS-friendly format

$pubdate = date("r", strtotime($timestamp));

Ok, it is a bit much to take in but bare with me - it is worth it. Basically all you're doing above is:
–  Setting a subject line
–  Defining how users would link to your article/blog
–  Defining a description of this particular article/blog (in this case, obtained from the content)
–  Making a short "teaser" description that subscribers will be able to read without navigating to your site
–  Setting a timestamp to allow RSS-readers to know when the article/blog was published to RSS


Write that information to RSS
All you need to do now, is write the above variables into the RSS/XML format.

echo
'

'.$subject.'
http://www.'.$rss_site.$url_product.'
http://www.'.$rss_site.$url_product.'
'.$short_description.'
'.$pubdate.'


';

} //this closes the for-loop

The above code writes the variables we set earlier into an individual RSS item, and, because of the for-loop, when it reaches the end of one item, it will write another until all 6 have been done.


Finishing the RSS generator
And we're essentially done! All you need to do is close the database and the XML file, and your generator is done! You close the file by adding in:

mysql_close();

echo
'
';

And your RSS generator is done! If the instructions have been a bit hard to follow, you can download a barebones edition of the file here