Breadcrumbs

RSS Feeds and PHP

For those who may know how to make a RSS Feed and want to go a step further you can do this using RSS and PHP to make an archive system using RSS to deliver the new articles to your subscribers. Using PHP you can make an automatically updating RSS Feed and if you already have an archive system using PHP you can easily add an RSS feed to it.

Just to recap on RSS Feeds take a look at how to create RSS feeds before you move on.

First we have to do the basics and connect to the MySQL database:

Code: PHP

<?php
$user
="user";
$pass="pass";
$database="database";
mysql_connect(localhost,$user,$pass);
@
mysql_select_db($database) or die( "Unable to select database");
?>

Just edit the above variables to fit your database connection and you should connect to the database and select the appropriate one.

Now here comes a part that is very vital to the document so that the RSS feeds works.

Code: PHP

<?php
header
("content-type: application/rss+xml");
?>

That says that the output of the document will be XML and RSS. Since it is a header value you need to place this before any output, so the easiest place to make sure that this is before any output is right at the top.

Since the only thing that will be altered when you use MySQL and PHP is the <item> tags you can place the start and end of the RSS document into your document:

Code: PHP

<?php
echo "<?xml version="1.0" encoding="iso-8859-15"?>
<?xml-stylesheet type="
text/css" href="feed_rss.css" ?>
<rss version="
2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">

<channel>

<
title>Lowter Studios Articles Newsfeed</title>
<
description>Internet News and Website Management</description>
<
language>en</language>
<
link>http://www.lowterstudios.com</link>";
?>

And the bottom of the RSS document:

Code: PHP

<?php
echo "</channel>
</rss>"
;
?>

Now here is what so far our document looks like including the database connection, content type declaration, and framework for the RSS Feed:

Code: PHP

<?php
header
("content-type: application/rss+xml");

$user="user";
$pass="pass";
$database="database";
mysql_connect(localhost,$user,$pass);
@
mysql_select_db($database) or die( "Unable to select database");

echo 
"<?xml version="1.0" encoding="iso-8859-15"?>
<?xml-stylesheet type="
text/css" href="feed_rss.css" ?>
<rss version="
2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">

<channel>

<
title>Lowter Studios Articles Newsfeed</title>
<
description>Internet News and Website Management</description>
<
language>en</language>
<
link>http://www.lowterstudios.com</link>";

echo "</channel>
</rss>"
;
?>

Now we have to query the MySQL database for the articles and then create the <item> tags for each one. To do this you need some MySQL experience if you don't already have any.

Now after reviewing MySQL or skipping this if you felt that this wasn't necessary you need to create the SQL query to get the articles. Here is an example one that I use to do this.

Code: PHP

<?php
$sql2 
"SELECT * FROM articles_db ORDER BY id DESC LIMIT 0, 7";
$result mysql_query($sql2);
?>

This statement takes only the newest 7 entries from the database so that when people update the RSS Feed or their reader does it they get the newest ones. You can set how many you want to have in it, but make sure to leave the DESC as that makes it so that you get the newest ones.

Now we have to loop through the results of our SQL statement:

Code: PHP

<?php
while($rss=mysql_fetch_array($result)) { 
$item "<item>
<title>$rss[name]</title>
<description>$rss[intro]</description>
<link>http://www.lowterstudios.com/articles.php?type=show&id=$rss[id]</link>
</item>"
;
}
?>

You have to alter this depending on your MySQL column names and your site for the <link> tag. So now our final document looks like this:

Code: PHP

<?php
header
("content-type: application/rss+xml");

$user="user";
$pass="pass";
$database="database";
mysql_connect(localhost,$user,$pass);
@
mysql_select_db($database) or die( "Unable to select database");

echo 
"<?xml version="1.0" encoding="iso-8859-15"?>
<?xml-stylesheet type="
text/css" href="feed_rss.css" ?>
<rss version="
2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">

<channel>

<
title>Lowter Studios Articles Newsfeed</title>
<
description>Internet News and Website Management</description>
<
language>en</language>
<
link>http://www.lowterstudios.com</link>";

$sql2 "SELECT * FROM articles_db ORDER BY id DESC LIMIT 0, 7";
$result mysql_query($sql2);

while(
$rss=mysql_fetch_array($result)) { 
$item "<item>
<title>$rss[name]</title>
<description>$rss[intro]</description>
<link>http://www.lowterstudios.com/articles.php?type=show&id=$rss[id]</link>
</item>"
;
}

echo 
"</channel>
</rss>"
;
?>

Now you have a working RSS Feed that when you insert an entry into the database it automatically updates the feed.

Tags: