Showing posts with label rss feed. Show all posts
Showing posts with label rss feed. Show all posts

05 January 2011

How to create dynamic rss feed using php mysql

Dynamic rss feed can be created in the similar manner as static rss feed, but it will be saved in a php file (eg. Rss.php ). In dynamic rss feed first we define the content type of the script and then we write xml script exactly same to the static feed except that the item tag is now written inside a while loop which is fetching data from the database.

Let’s make an example dynamic rss feed to give you guys a clear picture of what we are talking about. To make this example simple I will not be including the database connection code in this script.

Syntax

First we define the content type of the script as xml and rss. This can be done using the following line of code:
<?php header("Content-Type: application/rss+xml; charset=ISO-8859-1"); ?>
After defining the content type we will initialize the rss feed by placing following xml code:
<?php echo '<?xml version="1.0" encoding="ISO-8859-1" ?>'; ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
Now we need to define information about our site. Place the following code immediately after the previous code in your xml file:
<title>Skills 2 Earn</title>
<description>Knowledge for all</description>
<link>http://skills2earn.com</link>
In the above code “<title>” tag is used for setting the title of your site, “<description>” tag for description and “<link>” tag for the link of your site.

Now comes the most important part of your dynamic rss feed, in this part you need to define the information about your content. For this purpose “<item>” tags are used. If you want to put four topics in your feed then you need to use four “<item>” tags. We will do it by defining “<item>” tags inside a while loop. Following is an example code to let you know how these item tags can be used:
<?php
$sql="SELECT title,description,link FROM table1 LIMIT 5";
$result=mysql_query($sql) or die ($sql."<br />".mysql_error());
while ($row=mysql_fetch_array($result))
{
?>
   <item>
   <title><?php echo $row[‘title’]; ?></title>
   <description><?php echo $row[‘description’]; ?></description>
   <link><?php echo $row[‘link’]; ?></link>
   </item>
<?php
}
?>
In the above code “<title>” tag is used for setting the title of your topic, “<description>” tag for description and “<link>” tag for the link of your topic.

In the end you need to close all open tags. As I am doing in the code below:
</channel>
</rss>
That’s it guys, you have done it, you have created dynamic rss feed using php and mysql, but remember this example code is not supposed to work as it’s a dummy code just to make you guys understand how a dynamic rss feed is created.

Complete Code

Complete code is mentioned below. You need to safe it in a php file and you can name it as “rss.php”.
<?php header("Content-Type: application/rss+xml; charset=ISO-8859-1"); ?>
<?php echo '<?xml version="1.0" encoding="ISO-8859-1" ?>'; ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Skills 2 Earn</title>
<description>Knowledge for all</description>
<link>http://skills2earn.com</link>
<?php
$sql="SELECT title,description,link FROM table1 LIMIT 5";
$result=mysql_query($sql) or die ($sql."<br />".mysql_error());
while ($row=mysql_fetch_array($result))
{
?>
   <item>
   <title><?php echo $row[‘title’]; ?></title>
   <description><?php echo $row[‘description’]; ?></description>
   <link><?php echo $row[‘link’]; ?></link>
   </item>
<?php
}
?>
</channel>
</rss>
For any problems or queries, you may have, feel free to comment on this post. I would love to resolve your problems.

31 December 2010

How to create rss feed for static website

What is RSS?

RSS is most commonly abbreviated as Really Simple Syndication. It is an xml document used to provide the summary of the site contents. RSS is used to send users notification about what’s new on the site since they have last visited it.

Syntax

For creating a RSS feed for your site you must be familiar with xml tags. Basically rss feed comprises of two parts first part is about your site and second part is about your content.

You have to place the following code on the beginning of your xml file. This will initialize the xml document as rss feed.

< ?xml version="1.0" ? >
< rss version="2.0">
< channel >

Then comes the first part of your rss feed which is about your site information. In this part you need to define information about your site. Place the following code immediately after the previous code in your xml file:

< title>Skills 2 Earn< /title>
< description>Knowledge for all< /description>
< link>http://skills2earn.com< /link>

In the above code “< title>” tag is used for setting the title of your site, “< description>” tag for description and “< link>” tag for the link of your site.

Now comes the second the most important part of your rss feed, in this part you need to define the information about your content. For this purpose “< item >” tags are used. If you want to put four topics in your feed then you need to use four “< item>” tags. Following is an example code to let you know how these item tags can be used:

< item>
< title>title of your topic< /title>
< description>description of your topic< /description>
< link>http://www.example.com< /link>
< /item>

In the above code “< title>” tag is used for setting the title of your topic, “< description>” tag for description and “< link>” tag for the link of your topic.

In the end you need to close all open tags. As I am doing in the code below:

< /channel>
< /rss>

Now in the last, rss files are like other xml files and they will be treated as text, so if you run your xml file on your local server or web server then the contents of the file will be displayed on the screen. So you need to tell apache to treat these xml files as php files. This can be done by adding following code in the .htaccess file:

AddType application/x-httpd-php .xml

That’s it guys, you have done it, you have created an rss feed for your website.

Complete Code

Complete code is mentioned below. You need to safe it in an xml file and you can name it as “rss.xml”.


< ?xml version="1.0" ? >
< rss version="2.0">
< channel>
< title>Skills 2 Earn< /title>
< description>Knowledge for all< /description>
< link>http://skills2earn.com< /link>
< item>
< title>title of your topic< /title>
< description>description of your topic< /description>
< link>http://www.example.com< /link>
< /item>
< item>
< title>title of your topic< /title>
< description>description of your topic< /description>
< link>http://www.example.com< /link>
< /item>
< /channel>
< /rss>


For any problems or queries, you may have, feel free to comment on this post. I would love to resolve your problems.