PHP RSS feed generator form to entering data to a table

If you're a web publisher, offering an RSS feed allows visitors or other webmasters to easily share your content. RSS simplifies content syndication, and this guide shows how to create one using PHP. The process involves two steps: first, updating a table with new content, and second, generating the RSS feed from that table. The form requires inputs like title, link, description, and publication date, which must follow the RFC-822 format.
The format looks like this

<pubDate>Sat, 11 Oct 2004 09:00:00 EST</pubDate>
<pubDate>Mon, 05 Sep 2002 14:00:00 GMT</pubDate>
<pubDate>Wed, 14 Oct 2003 17:00:00 +0200</pubDate>
So we will pre populate the date field with present time in this format, but we will have option of changing it. Here is the sample form.
echo "<hr>";
$tm=time();  // time stamp of the present time
$tm=date("D, d M Y H:i:s",$tm); // generating the format
$tm=$tm. " GMT"; // added GMT but you can add your format
echo "<form method=post action=rsspostck.php>
Title<input type=text name=title size=100><br>
Link<input type=text name=page_link size=100><br>
Description<textarea name=description cols=60 rows=5></textarea><br>
Publication Date<input type=text name=pubdate size=35 value='$tm'>Wed,
21 Apr 2005 08:20:47 GMT<br>
<input type=submit value='Add'></form>
";
We will go with a simple date field for storing the RSS feeds, but we will have option of changing it.

You can download the zip file with all the scripts to create your own RSS feed or to understand how the script works at the end of this page. Inside this zip file these are the files available.

config.php

Store all database connection details and this page is included wherever database operation is involved.

rsspost.php

This page display the form for the user to enter page details. This page post details to rsspostck.php where all the data is inserted to our rss table.

rsspostck.php

Collects all the form data from rsspost.php file and then stores them in a table.
require 'config.php';
$title=$_POST['title'];
$link=$_POST['link'];
$description=$_POST['description'];
$pubdate=$_POST['pubdate'];

echo "<hr>";

$sql=$dbo->prepare("insert into rss(title,link,description,pubdate) values(:title,:link,:description,:pubdate)");
$sql->bindParam(':title',$title,PDO::PARAM_STR, 250);
$sql->bindParam(':link',$link,PDO::PARAM_STR, 250);
$sql->bindParam(':description',$description,PDO::PARAM_STR,250);
$sql->bindParam(':pubdate',$pubdate,PDO::PARAM_STR);
if($sql->execute()){
$rss_id=$dbo->lastInsertId();
echo " Thanks .. Your Rss Id = $rss_id ";
}
else{
echo " Not able to add data please contact Admin ";
print_r($sql->errorInfo());
}

update_rss.php

Here we will read the recent data from the table and update the xml file in the specified format. Now we will read the data from the table and with the new data we will update the xml file. We will be using PHP file open command to open the xml file and then enter the new data to this file and then close the file. You can read the related tutorials here to get the idea on how to handle files in PHP. We will use limit command in SQL to get the most recently added 6 records from the table. Here we have used the order by command with desc to collect the records from the end of the table. Here is our SQL to collect the records from the table. Here is the code of it.
<?Php
require 'config.php';

///////// Getting the data from Mysql table for first list box//////////
$sql="SELECT * from rss order by rss_id desc limit 0,6"; 
///////////// End of query for first list box////////////

$body="<?xml version="1.0" encoding="iso-8859-1"?> 
<rss version="2.0"> 
<channel> 
	<title>Plus2net Free Tutorials on Web programming</title> 
	<link>https://www.plus2net.com</link> 
	<description>The latest Tutorials and free codes on Web programming from www.plus2net.com</description> 
	<copyright>(c) 2005, Plus2net.com. All rights reserved.</copyright> 
";

foreach ($dbo->query($sql) as $nt) {
$body .="
		<item>
			<title> $nt[title]</title>
				
		<link> $nt[link]</link> 
			<description> $nt[description]</description>				
				
			<pubDate>$nt[pubdate]</pubDate>
		</item>";
}

$body .="
</channel>
</rss>";
echo $body;
$path="rss.xml";
	$filenum=fopen($path,"w");
	fwrite($filenum,$body);
	fclose($filenum);

?>
After running of this update_rss.php file, one new rss.xml file will be generated. So you must give write permission to this directory to create this file.

Download the code for this tutorial here


XML XML count()

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com











    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer