| |
|
PHP RSS feed updating the xml file with data |
Here we will read the recent data from the table and update the xml file in the specified format. You have seen on how to enter the data to the table in part one of this tutorial. You can downlaod the code of this tutorial at the end of this page. 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.
$quer2=mysql_query("SELECT * from rss order by rss_id desc limit 0,6");
Now we will use a variable to store the standard header of the xml file and then to this we will add the data we got form the table. Please note that here we have used the date format again to update the RSS file the time of running this script but you can directly read the date and add to the file. Here is our variable with RSS format stored in it.
$body="<?xml version=\"1.0\"
encoding=\"iso-8859-1\"?>
<rss version=\"2.0\">
<channel>
<title>Plus2net Free Tutorials on Web programming</title>
<link>http://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>
";
This is the common header so we kept it outside the loop of the table data. Now let us enter into the loop and add data to the variable.
while($nt = mysql_fetch_array($quer2)) {
$body .="
<item>
<title> $nt[title]</title>
<link> $nt[link]</link>
<description> $nt[description]</description>
<pubDate>$nt[pubdate]</pubDate>
</item>";
}
$body .="
</channel>
</rss>";
echo $body;
Now once all the data with the RSS format is stored then we will write the variable into the file and save the file. We must have write permission for the file for this. Here is the code to do this.
$path="rss/rss.xml";
$filenum=fopen($path,"w");
fwrite($filenum,$body);
fclose($filenum);
Now our RSS feed file is ready. Don't forget to validate and submit your URL. You will get all links here
Download the source code for this tutorial
| |
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|
|
|