Tips Centre Tuesday, December 3, 2024

Home Home | About Us About Us | Contact Us Contact Us

vrPakistanis Technologies - Web Development, Logo Designing, Web Designing

Home > PHP

Make An RSS Feed Using PHP

Ok you start off with just finding out the basic structure of a RSS feed channel. So you still have to include title, link, and description to each item you would like to display in your feed.

So the best way to learn how a RSS feed works is by looking at one and seeing were everything should go so take a look at the code below we provided for you and you can understand how it all works.

<?xml version='1.0'?>
<rss version='2.0'>
<channel>
<title>Websites Tittle/Name</title>
<link>Websites full URL</link>
<description>Website description</description>
<language>en-us</language>

<item>
<title>Items Title</title>
<link>Items URL goes here</link>
<description>Items description here</description>
</item>

</channel>
</rss>

Then once you have worked our how the basics of an RSS feed works we then will have to open up a new black page and start loop some PHP coding into it so it will then pick up our database information.

So I have broken the code up for you so I can also explain each step and what parts you have to change.

<?php
$connection = mysql_connect("localhost",
"db_accountname",
"password");
mysql_select_db("db_name", $connection);

Above we have started with connecting to the database. You will have to edit 3 areas in here, the db_accountname, password and db_name.

Then we move onto the SQL query and how many items we would like to display with putting the command ‘LIMIT 10’. You can change this to as much as you would like your feed to show.

$select = "SELECT * FROM db_tablesnamehere ORDER BY id DESC LIMIT 10";
$query = mysql_query($select) or die(mysql_error());

And now we will finish off the code with all the page information. This part it pretty easy so just copy this code below and edit all the areas such as the title, link and description. Also remember don’t forget to keep the $ in front of the database areas or it wont be able to pick up the information.

header("Content-type: text/xml");

echo "<?xml version='1.0'?>
<rss version='2.0'>
<channel>
<title>Websites Tittle/Name</title>
<link>Websites full URL</link>
<description>Website description</description>
<language>en-us</language>";

while($array = mysql_fetch_array($query)){
extract($array);
$content = htmlentities($content);
echo "<item>
<title>$titlehere</title>
<link>$urlhere</link>
<description>$descriptionhere</description>
</item>";
}
echo "</channel></rss>";
?>

When just save this file as a PHP formal and upload it.

This article has been taken from webdesign.org.

Tips Centre 2008-2021. All rights reserved. Developed by vrPakistanis Technologies.