Friday, March 16, 2012

create rss feed using php with image in description

0


In php it is simple to create the rss feed. following one i create for my self and
sharing the code so that some one get benefit from it.

To create rss feed following are step by step code:

DB table:

CREATE TABLE `data` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `added_date` datetime NOT NULL,
  `image` varchar(30) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; 

Rss code(php):

<?php
error_reporting(E_ALL);
DEFINE ('USER', 'user name');
DEFINE ('PASSWORD', '');
DEFINE ('HOST', 'host name');
DEFINE ('DB_NAME', 'database name');
// Make the connnection and then select the database.
$link = @mysql_connect (HOST, USER, PASSWORD) OR die 

('Could not connect to MySQL: ' . mysql_error() );
mysql_select_db (DB_NAME) OR die ('Could not select the database:

 ' . mysql_error() );
?>
<?php
header("Content-Type: application/xml; charset=utf-8");
$query = "SELECT * FROM data";
$result = mysql_query($query);

$items = '<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"><channel>
<generator>FeedCreator 1.7.2</generator>
';
while($row = mysql_fetch_array($result))
{
// title
$title = htmlentities(strip_tags($row['title']));
//description
$description =htmlentities(strip_tags($row['description']));
//url
$link = 'http://localhost/detail.php?id='.$row['id'];
//image
$image='images/'.$row['image'];
$items .= '
<item>
<title>'. $title.'</title>
<link>'. $link .'</link>

<description><![CDATA[<img src=' . $image . ' hspace=0 vspace=0 

border=0 align=left height="100" width="150" /> ' .

 $description . ' ]]></description>
<pubDate>'.date("D, d M Y H:i:s O", strtotime($row['added_date']))

 .'</pubDate>

</item>';

}
$items .= '</channel></rss>';
echo  $items;
?>


how it will look?:

No comments :

Post a Comment