How to Create A Dynamic Rss Feed

6 years ago

Hello everyone!
I want to share a piece of code, 'few' or 'most' people usually have issues creating--->A dynamic rss feed.
Dynamic means-- fetching articles from the database.
Instead of you updating the rss manually.
Like a month back, some guy was here having issues with it, I don't know if I helped with his issue, because I never really ran the codes on my machine.
Until one day I decided to try, and I discovered why he had issues.
Now I'll be using Mysqli queries first,
Then I'll put up the Mysql query syntax for those still using mysql in a comment(//) below the mysqli syntax.
Ok to cut the story story...
Here's the code.

PHP Code:


<?php 

<?php
//first include your database connection //setup
//lets call it include_database.php
Include 'include_database.php';

//now let's set the header content to xml
//because we won't be saving the file with
 //the extension .xml instead it will be .php
//That is dynamic_rss.php
header("Content-Type: application/xml");
echo 
'<?xml version="1.0" encoding="utf-8" standalone="yes" ?>'."
<rss version='2.0'>
<channel>
<title>Website Feed</title>
<description>Dynamic Website Rss Feed</description>
<link>LinkToWebsiteRss.com</link>"
;
$query "SELECT * FROM `articles` LIMIT 30";
//you can change your limit values!
$result mysqli_query($dbconn$query);
//if u are still using mysql
//do this $result = mysql_query($query)
//remove the 'i' in the mysqli
 
While($row mysqli_fetch_array($result))
{
echo 
' <item><title>'.$row["title"]. '</title>
<description><![CDATA['
.$row["article_story"].']]></description>
<link>http://link_to_the_full_article_story.com</link>
<pubDate>date of article</pubDate></item>'
;
}
echo 
'</channel>';
echo 
'</rss>';
?> ?>


I hope this helps!
Feel free to contact me if you have any issues.
Thank you :)

Related forums
Forum

Akeredolu Promises To Create More Local Govts If Reelected

2 months ago
Forum

Microsoft launches Expressive Pixels app to help you create animations

2 months ago
Forum

Chadwick Boseman’s High School To Create Scholarship In His Honor

3 months ago
Forum

6 Factors that create an instant attraction between two people

3 months ago