Hello Shantanuo
The code I have included in this answer is the solution to your
problem. First of all I need to tell you how to drive the code.
The code takes a number of parameters from the URL, without these it
would be impossible for the script to run. These parameters are:
db - this is the database name
dbt - this is the table in the database that contains the information
t - this is the column name that contains the <title> information for the RSS file
u - this is the column name that contains the <link> information for the RSS file
d - this is the column name that contains the <description>
information for the RSS file
id - this is the column name that the data will be sorted by, this is
usually an id tag or date in the database table so that the latest
results are used
Without passing these parameters the script would not know which
database, table or columns are the correct one.
The URL to run the script should be something like this:
http://www.thisistheurlofyourdomainname.com/rss.php?db=database_name&dbt=table_to_use_in_the_database&t=column_in_table_with_the_title_info&u=column_in_the_table_with_the_link_info&d=column_in_the_table_with_the_description_info&id=column_in_the_table_to_sort_by
For each RSS file you wish to create this URL will be slightly
different but you need only one rss.php file.
Finally the code requires one small edit on your part - you need to
put in the username and password for your MySQL database. You can
change this in the code below - it only appears once on this line: $db
= mysql_pconnect("localhost", "yourusername", "yourpassword");
If you have any further questions on this please ask for clarification
and I will do my best to help.
The final code:
<?
/*****************************************************************************
* rss.php
* Version: 1.00
* Author: Rogers Cadenhead
* Date: 05/21/2004
* http://www.cadenhead.org/workbench/
*
* Edited by palitoy-ga on Google Answers for shantanuo-ga
******************************************************************************
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This work is hereby released into the Public Domain. To view a copy of the public
domain dedication, visit http://creativecommons.org/licenses/publicdomain/ or
send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
94305, USA.
******************************************************************************/
// prepare HTML text for use as UTF-8 character data in XML
function cleanText($intext) {
return utf8_encode(
htmlspecialchars(
stripslashes($intext)));
}
// set the file's content type and character set
// this must be called before any output
header("Content-Type: text/xml;charset=utf-8");
// get the names of the database table and columns
$name_db = $_GET["db"];
$name_dbt = $_GET["dbt"];
$name_title = $_GET["t"];
$name_url = $_GET["u"];
$name_desc = $_GET["d"];
$name_orderby = $_GET["id"];
// stop here if there is a parameter missing
if ( !$name_db or !$name_dbt or !$name_title or !$name_url or
!$name_desc or !$name_orderby ) {
error_log("Error: One of the parameters required in the URL is missing.");
exit;
}
// retrieve database records
$db = mysql_pconnect("localhost", "yourusername", "yourpassword");
// if there is an error stop here
if (!$db)
{
error_log("Error: Could not connect to database in rss.php.");
exit;
}
// store items from the database in the $result1 array
mysql_select_db($name_db);
$query1 = "select * from {$name_dbt} where {$name_title}!='' and
{$name_desc}!= '' and {$name_url}!= '' order by {$name_orderby} desc
limit 10";
$result1 = mysql_query($query1);
$phpversion = phpversion();
// display RSS 2.0 channel information
ECHO <<<END
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>This is the title of your website</title>
<link>This is the link to your page</link>
<description>A small description of your site</description>
<language>en-us</language>
<docs>http://backend.userland.com/rss</docs>
<generator>PHP/$phpversion</generator>
END;
// loop through the array pulling database fields for each item
for ($i = 0; $i < mysql_num_rows($result1); $i++) {
@$row = mysql_fetch_array($result1);
print $row[$name_title];
$title = cleanText($row[$name_title]);
$link = cleanText($row[$name_url]);
$description = cleanText($row[$name_desc]);
$guid = $row[$name_orderby];
// display an item
ECHO <<<END
<item>
<title>$title</title>
<link>$link</link>
<description>$description</description>
<guid isPermaLink="false">$guid</guid>
</item>
END;
}
ECHO <<<END
</channel>
</rss>
END;
?> |