__________________________________________________________________
This tutorial was on my site before It went down and I've decided to bring it here. Hope you like
__________________________________________________________________
In this tutorial you will learn how to create a table in phpMyAdmin and create an RSS feed that goes along with it!
Requirements: Know basics of php, phpMyAdmin, notepad, mouse and keyboard.
Now let’s start with our SQL Table.
CREATE TABLE `news` ( `id` tinyint(11) NOT NULL auto_increment, `title` text NOT NULL, `author` text NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) );That simply created a table called news. The table news contains 4 rows. Each row stands for something different. So in this case, we have 4 rows.
Click to enlarge
Now lets fill the table with some random text so we can see how this RSS works.
INSERT INTO `news` VALUES (1, ‘test1', 'BigDog', ' This is Test 1'); INSERT INTO `news` VALUES (2, ‘test2', 'BigDog', ' This is Test 2'); INSERT INTO `news` VALUES (3, ‘test3', 'BigDog', ' This is Test 3');If you don’t understand what I did above, I just simply used INSERT TO and inserted values into each row.
Now that we have the table setup, we can start with our RSS Feed for it. Since this is going to be a .php file instead of .xml, we need to specify the server that this file will have xml content.
<? header('Content-type: text/xml'); ?>
Now lets connect to our database.
<?php $dbhost = "localhost"; // almost always localhost. $dbname = "news"; // Database Name, In our case, its news $dbuser = "UserName"; // Database Username $dbpass = "Password"; // Databse Password $connect = mysql_connect("$dbhost","$dbuser","$dbpass");// Connecting to Database mysql_select_db($dbname) or die (mysql_error()); // Selecting Database ?>
Now that we have specified our username, password, and table, we can start the main rss.
<rss version="2.0"> <channel> <title>Dark Pixels Tutorials</title> <description>Latest tutorial on Dark Pixels</description> <link>http://darkpixels.net/</link>This code above is like any other rss/xml feed. We are using rss version 2.0. You can change title, description, and link to your needs. Title can be anything but normally it’s your site name. Give a little information about your site in the description and your websites link in the <link>
<? $sql = "SELECT * FROM news limit 5"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)){ ?>The above code is simply getting a query from our sql table. You can change
$sql = "SELECT * FROM news limit 5" to anything you want! Change the limit, table, or even add more information like WHERE author = 'BigDog'. That will only show the submissions by BigDog.
Hold on, we are almost done!
Now lets get the main rss.
<item> <title><?=$row['title']; ?></title> <author><?=$row['author']; ?></author> <link>http://MYSITE.com/news.php?id=<?=$row['id']; ?></link> </item>Now lets explain.
the <item> tag defines an row the RSS/SQL feed. Since we have it so it would show 3 rows from our table, at the end you will have 3 <item> tags.
The <title> tag is what will show up in the rss. If you use Mozilla Firefox, it’s the name that shows up when you select the drop down menu.
The <author> tag shows the owner of the news or content. In our case, it would be BigDog.
The <link> tag will show the ID for us. But as you can, I've added a link before it. That link is for my news. So If I have a page called news.php and news.php?id=# will show the specified news, that will link it to the specified contact/news.
And that’s it. Now let’s end our tags and we are done!
<? } ?> </channel> </rss>
Here is our final code!
<? header('Content-type: text/xml'); ?> <?php $dbhost = "localhost"; // almost always localhost. $dbname = "news"; // Database Name, In our case, its news $dbuser = "UserName"; // Database Username $dbpass = "Password"; // Databse Password $connect = mysql_connect("$dbhost","$dbuser","$dbpass");// Connecting to Database mysql_select_db($dbname) or die (mysql_error()); // Selecting Database ?> <rss version="2.0"> <channel> <title>Test Title</title> <description>This is an example description</description> <link>http://pixel2life.com</link> <? $sql = "SELECT * FROM news limit 5"; $result = mysql_query($sql); while($row = mysql_fetch_assoc($result)){ ?> <item> <title><?=$row['title']; ?></title> <author><?=$row['author']; ?></author> <link>http://MYSITE.com/news.php?id=<?=$row['id']; ?></link> </item> <? } ?> </channel> </rss>
Thank you for reading.
Edited by Faken, 12 February 2007 - 09:58 PM.