In Coldfusion 8 they intriduced a new tag called <CFFEED>. This Tag is used to make either RSS or ATOM Feeds. (It can additionally make ITunes Playlists and other XML-based contents, however those are other tutorials).

In this Tutorial We'll learn how to make an RSS Feed using an SQL Query and saving that query data as an RSS Feed using the <CFFEED> tag.

The Query::
(Note you can change this query to however or what ever contents your want)

<cfquery name="contents" datasource="#datasrc#" username="#db_user#" password="#db_pass#">
    select * from site_contents where contents_active = '1' order by contents_id ASC
</cfquery>

(If you want to exclude certain contents from being added into your RSS FEED, you can add a line similiar to the below into your where statement:
 where ... and contents_id !='54'

This will exclude contents with the ID of 54)

The Code:

We'll Start by Generating the RSS FEED Header:
(Additonally, change anything here in ITALICS)

<cfset myStruct = StructNew() />
<cfset mystruct.link = "http://yourdomain.com/rss.cfm" />
<cfset myStruct.title = "Available contents on Yoursite.com" />
<cfset mystruct.description = "Yoursite's RSS Feeds" />
<cfset mystruct.pubDate = Now() />
<cfset mystruct.version = "rss_2.0" />
<cfset myStruct.item = ArrayNew(1) />

Now We'll loop over the query and set our structure:

<cfloop query="contents">
    <cfset myStruct.item[currentRow] = StructNew() />
    <cfset myStruct.item[currentRow].guid = structNew() />
    <cfset myStruct.item[currentRow].guid.isPermaLink="YES" />
    <cfset myStruct.item[currentRow].guid.value = 'http://yoursite.com/index.cfm' />
    <cfset myStruct.item[currentRow].pubDate = createDate(year(#contents.contents_startdate#), month(#contents.contents_startdate#), day(#contents.contents_startdate#)) />
    <cfset myStruct.item[currentRow].title = xmlFormat(#contents.contents_title#) />
    <cfset myStruct.item[currentRow].description = StructNew() />
    <cfset myStruct.item[currentRow].description.value = xmlFormat(#contents.contents_content#) />
    <cfset myStruct.item[currentRow].link = 'http://yoursite.com/?id=#contents_id#' />
    </cfloop>

Now That we've done that, We'll generate our XML Feed File:

<!---\\ Generate the feed and save it to a file and variable. //--->
<cffeed action="create" name="#myStruct#" overwrite="true" xmlVar="myXML" outputFile = "#expandpath('feeds.xml')#"/>

and That's It. You've just Created your XML Feed using CFFEED and a Database Query.

Below is the Full code for Copy and Pasting:

About This Tutorial
Author: James Harvey
Skill Level: Beginner 
 
 
 
Platforms Tested: CF8
Total Views: 64,357
Submission Date: October 17, 2009
Last Update Date: October 17, 2009
All Tutorials By This Autor: 12
Discuss This Tutorial
Advertisement


Website Designed and Developed by Pablo Varando.