Page 1 of 1

Get a compact RSS system for your site RSS feed system in two formats, filebased and databased Rate Topic: -----

#1 kiwi2  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 178
  • Joined: 16-September 08

Post icon  Posted 06 November 2008 - 10:54 PM

Build an easy to manage rss system for your site to increase vistors and let the world know what you are doing. This tutorial will demonstrate:-

1. How to set up a single news item per day manager.
2. How to create an multi news feed from a database of articles.
3. How to read an rss file and output it to your page.
4. How to submit your rss to sites that will syndicate your content.
5. How to check if your feed is valid.
6. Two systems are demonstrated here:
a. a simple file based system.
b. a more complex database system.
NB:You need a php enabled website to run this code.

The simple single feed system.


To begin open a new file on your site and call it adminrss.html, copy and paste the following code into it:
<html><head><title>MyRss Manager</title></head><body>
<ul>
<li><a href='makerss.html'>Make a new feed</a></li>
<li><a href='makemultirss.php'>Manage multiple feeds</a></li>
<li><a href='readmyrss.php'>Read file based feed</a></li>
<li><a href='readmyrssdb.php'>Read database feed</a></li></ul></body></html> 

Next, open 3 new files on your site and call them makerss.html, myrss.php and myrss.xml(the permissions for this file must be set to 777).
into the first paste the following code:
<html><head<<title>My Rss</title></head><body><h4>My Rss</h4><hr>
<form action="myrss.php" method="post"><b>Title:</b><br /><input type='text' name='title' /><br /><b>Description:<b><br /><textarea name='description'></textarea><br /><b>Link:</b><br /><input type='text' name='link' /><br /><input type='submit' name='submit' value='Make Rss' /></form></body></html> 

This file will send your news item feed to the myrss.php file which will trigger your xml feed.
Now for something a little more educational, the myrss.php code:
<?php
$title = trim($_POST['title']);
$desc = trim($_POST['description']);
$link = trim($_POST['link']);
$ele1 ='<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">' . "\n";
$ele2 = '<channel>' . "\n";
$ele3 = '<title>Your sites Title, or the title of the feed</title>' . "\n";
$ele5 = '<link>http://www.yoursite.com</link>' . "\n";
$ele4 = '<description>Description of the feed</description>' . "\n";
$ele6 = '<item>' . "\n";
$ele7 = '<title>' . "$title" . '</title>' . "\n";
$ele9 = '<link>' . "$link" . '</link>' . "\n";
$ele8 = '<description>' . "$desc" . '</description>' . "\n";
$ele10 = '</item>' . "\n";
$ele11 = '</channel>' . "\n";
$ele12 = '</rss>';
$val = "$ele1" . "$ele2" . "$ele3" . "$ele4" . "$ele5" . "$ele6" . "$ele7" . "$ele8" . "$ele9" . "$ele10" . "$ele11" . "$ele12";
$file = 'myrss.xml';
$fp = fopen($file, "w");
fwrite ($fp, $val);
fclose($fp);
echo "<a href='adminrss.html'>New RSS added, return to admin page</a>";
?> 




The initial part of the xml file contains the posted data from the makerss.html file, the second snippet is the xml constructor, which you must edit to reflect your websites data, the feed description and primary link(your homepage) must also be added, these three elements must be left unchanged throughout the life of this feed.
Finally the code contains the variables which are written to your xml file using the php built-in fopen method, you can now change the xml file with every new feed you add.
Now we need a system to read the feed, and to allow visitors to the site the same privilege.
A new file must be opened, call it readmyrss.php add the following code to it:
<?php
$file = "myrss.xml";
$sim = simplexml_load_file($file);
 foreach ($sim->channel->item as $it) 
{ 
print "<h4>$it->title</h4><br />";
print "<a href='$it->link'>Go there ..</a><br />";
print "$it->description<br />"; 
} 
?>
Place a link to this file in a prominent spot on your site using the link:
<a href='readmyrss.php'>Read Rss</a>
(Tip:go to the website http://www.feedvalidator.org, enter the xml file's name from your site, once the feed has been validated you may copy the orange rss button and 'valid feed' badge to your site).
Now, this system is quick, mean, easy to implement and suitable for websites that do not have a database such as mysql, but it has several flaws.
If you make an error it is difficult to correct without rewriting the feed, you have no record of your news items, and if more than one person is writing up feeds for the site there is no method for the site manager to check the rss before it goes live, so in order to mend the error of our ways  lets set up a database to manage and track all items written, and lets add a method to add feeds after they have had a chance to be checked and edited.


Complex Rss Manager.


To begin we will need a database to manage our data set it up in phpMyAdmin add a user and give that user all permissions, open a two files call them config.php and creatersstable.php, copy the next bit of code into config.php, changing the necessary info to that of your database:
[code]
<?php 

$db_name ="database_name"; 
$db_user ="user"; 
$db_pass ="password"; 
$db_host = 'localhost';
$link = mysql_connect("localhost", $db_user, $db_pass); 
if (! $link ) 
{ die ("could not connect!" . mysql_error()); 
} 
@mysql_select_db($db_name) or die("could not open $db_name: " .mysql_error()); 
?> 

Into the second file add:
<?php
include "config.php";
$query = "CREATE TABLE myrss(id INT 
NOT NULL
AUTO_INCREMENT,
PRIMARY KEY(id), title VARCHAR(255), descrip TEXT, link VARCHAR(255), tmestampeds  DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL)";
print "running query";
mysql_query($query, $link) or die
("CREATE TABLE error: " . mysql_error());
mysql_close($link);
?> 

Run this file to create your table.
This table will give you a title, description , link and timestamp for your feed, the extra columns could also be added to any other database on your site eg: A products table detailing items of stock, when new stock is added a feed could be sent out alerting potential customers, or it could be added to an articles table, to let members know when new stories have been written, ect..ect.
Another form...
simply alter makerss.html(call the new file makerssdb.html) as follows:
<html><head<<title>My Rss</title></head><body><h4>My Rss</h4><hr>
<form action="myrssdb.php" method="post"><b>Title:</b><br /><input type='text' name='title' /><br /><b>Description:<b><br /><textarea name='description'></textarea><br /><b>Link:</b><br /><input type='text' name='link' /><br /><input type='submit' name='submit' value='Make Rss' /></form></body></html> 

The myrssdb.php file:
<?php
include "config.php";
$title = trim($_POST['title']);
$desc = trim($_POST['description']);
$link = trim($_POST['link']);
$q = "INSERT INTO myrss(title, descrip, link, tmestampeds) VALUES('$title', '$desc', '$link', NOW())";
$results = mysql_query($q)
or die ("An error occured try again: $results . " . mysql_error());
echo "<a href='adminrss.html'>New Data added, return to admin page</a>";
?> 

The management page makemultirss.php:
<html><head><title>MyRss Manager</title></head><body>
<ul>
<li><a href='makerssdb.php'>Make a new feed</a></li>
<li><a href='editor.php'>Edit,delete, add rss</a></li></ul></body></html> 

editor.php code as follows:
<?php
echo "Click on the MAKE RSS link to make your feed";
include "config.php";
$q =mysql_query("SELECT * FROM myrss");
while ($row = mysql_fetch_object($q) ) 
{ 
echo "<b>$row-title</b></br>"
echo "<a href='edit.php?id=$row->id'>EDIT|</a>><a href='feed.php?id=$row->id'>MAKE RSS</a>"; 
} 
echo "<a href='adminrss.php'>Return to admin main page</a>";
?> 

The edit.php page:

<?php
include "config.php";
$id = $_GET[id];
if($id == " ") 
{ 
echo "<a href='adminrss.php'>Go back, no id</a>"; 
} 
else 
{ 

if(! isset($_POST[submit]) ) 
{ 

$q = mysql_query("SELECT * FROM myrss WHERE id = $id");
while ($row = mysql_fetch_object($q) ) 
{ 

echo "<form action='edit.php' method='post'><b>Title:</b><br /><input type='text' name='title' value='$row->title' /><br /><b>Description:<b><br /><textarea name='description'>$row->descrip</textarea><br /><b>Link:</b><br /><input type='text' name='link' value='$row->link' /><br /><input type='hidden' name='id' value='$id' /><br /><input type='submit' name='submit' value='Edit Rss' /></form>"; 
} 
} 
 
else 
{ 
$title = trim($_POST['title']);
$id = trim($_POST['id']);
echo "$title<br />";
$desc = trim($_POST['description']);
echo  "$desc<br />";
$link = trim($_POST['link']);
echo  "$link<br />";
$q = "UPDATE myrss SET title = '$title', descrip = '$desc', link = '$link', tmestampeds = NOW() WHERE id = '$id'";
$results = mysql_query($q)
or die ("An error occured try again: $results . " . mysql_error()); 
 
echo "<a href='adminrss.php'>Data has been edited, go back to admin page</a>"; 
} 
} 
?> 


The edit.php page prints out the data from the database and pops it into the orginal form used to create the rss instance, the data in the form fields can now be changed or corrected if errors have occured(PS: I have not added any validation to the processing of the form data since this form will only be used by the sites owners or managers and not by general site users)


Now what this article has been leading up to:
The feed.php page
<?php
include "config.php";
$id = $_GET['id'];
$file = "mysite.xml";
$q = mysql_query("SELECT * FROM myrss WHERE id = '$id'");
if (mysql_num_rows($q) > 0) 
{ 
while ($row = mysql_fetch_object($q) ) 
{
$title = $row->title;
$desc = $row->descrip;
$link = $row->link;
echo "This feed was added at $row->tmestampeds<br />";
echo '<b>Title:</b>:' . "$title" . '<br />';
echo 'Description:' . "$desc<br />";
echo 'Link:' . "$link<br />";
$sim = simplexml_load_file($file);
foreach($sim->channel->item as $it)
{ 

$feed[] = '<item>' . "\n" . '<title>' . "$it->title" . '</title>' . "\n" . '<description>' . "$it->description" . '</description>' . "\n" . '<link>' . "$it->link" . '</link>' . "\n" . '</item>';
} 
} 

$fd = fopen($file, "w");
$ele1 ='<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0">' . "\n";
$ele2 = '<channel>' . "\n";
$ele3 = '<title>Your sites Title, or the title of the feed</title>' . "\n";
$ele5 = '<link>http://www.yoursite.com</link>' . "\n";
$ele4 = '<description>Description of the feed</description>' . "\n";

$eles =  "$ele1" . "$ele2" . "$ele3" . "$ele4" . "$ele5"; 
fwrite($fd, $eles);
fclose($fd);
$fp = fopen($file, "a");
$ele10 = '<item>' . "\n";
$ele11 = '</item>' . "\n";
$title2 = '<title>' . "$title" . '</title>' . "\n";
$desc2  = '<description>' . "$desc" . '</description>' . "\n";
$link2 = '<link>' . "$link" . '</link>' . "\n";
$rty = "$ele10" . "$title2" . "$desc2" . "$link2" . "$ele11"; 
fwrite($fp, $rty);
fclose($fp);
$total = count($feed);
echo "$total<br />"; 
if ($total < 7) 
{ 
$amount = $total;
$num = 0; 
} 
else if ($total == 0) 
{ 
$amount = 1;
$num = 0; 
} 
else 
{ 
$amount = 7;
$num = 1; 
}  
for($i=$num;$i<$amount;$i++) 
{ 
 
$titl[$i] ="$feed[$i]";
$fg = fopen($file, "a");
fwrite($fg, $titl[$i]);
} 
$ele11 = '</channel>' . "\n";
$ele12 = '</rss>';
$rss = "$ele11" . "$ele12";
$fy = fopen($file, "a");
fwrite($fy, $rss);
fclose($fy);
echo "<a href='adminrss.php'>Feed created, go back</a><br />"; 
} 
?> 

The code above will add the latest feed item at the top of the xml file, and will set a limit of no more than 7 items to a file, eliminating the oldest news item first and so on, but enen if the feed now holds only the latest data the site still maintains a database of all items written as a reference.
There are many ways to manipulate rss feed data, not the least of which, DOM (document object model) allows the data to be read in node form, however I have chosen to use a simple 'echo', method of writing to the rss file, since it is easy to undersand, and will work on the majority of websites. SimpleXML offers quick and easy functions to deal with the data and if you look it up on php.net you will find that it offers many additional ways of dealing with xml data.
Finally to read our new file.
readmyrssdb.php:
<?php
$file = "mysite.xml";
$sim = simplexml_load_file($file);
 foreach ($sim->channel->item as $it) 
{ 
print "<h4>$it->title</h4><br />";
print "<a href='$it->link'>Play</a><br />";
print "$it->description<br />"; 
} 
?> 

The feed format used in the example is only in very basic format, many more items such as a timestamp, name of author ect, may be added, but this system is initself perfectly valid and enough to get you started the extra fields can be added, if you feel it is necessary, as you become more proficient at using it.
Have fun with it...

submit your feed to Newsmob.com
Feedfire.com, Scyndi8.com, Yahoo.com and Google.com
PS: DON'T FORGET TO SET THE permissions for your xml files to 777.
PPS: YOU WON'T HAVE A FEED UNTIL YOU CLICK THE MAKE RSS LINK ON THE EDITOR PAGE.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Page 1 of 1