Skip to main content
SwaderAIR
Participant
April 28, 2010
Question

Simple CFC, a newsbox

  • April 28, 2010
  • 1 reply
  • 385 views

I'm making a simple webcomic engine for myself, and am nearly done. However, I've had to include a newsbox into two different cfm pages, and will probably need more as I expand the site. This newsbox is a simple div container populated by query results - that is, news from my news table.

I would like to have a cfscript version of this as a component, so I can include it in any site I want without any complications regarding CSS or database access. I've found several tutorials about CFCs, but they all branch in odd directions seemingly more complex than what I need to accomplish, and I'd like to avoid the auto-generation ORM wizard (the CFC generator extension).

Could someone help me out and point me in the right direction? Literally, all I need the component to do is:

1. Connect to a database

2. Execute the query

3. Output results.

Right now, in the cfm file itself, it looks like this:

The query:

<cfscript>

queryNews = new Query();
    queryNews.setName("getNews");
    queryNews.setUsername("************");
    queryNews.setPassword("*************");
    resultNews = queryNews.execute(sql="SELECT * FROM news WHERE news_category='comic' ORDER BY news_id ASC");
    getNews = resultNews.GetResult();

</cfscript>

The output:

<div id="newsBox">


            <cfoutput>
        <div id="newsHeader">
            <h3>#getNews.news_title[getNews.RecordCount]#</h3>
            <h5>#DateFormat(getNews.news_date[getNews.RecordCount], "dddd, mmmm dd, yyyy")#</h5>
        </div>
        <div id="newsContent">
            #getNews.news_content[getNews.RecordCount]#
        </div>
        <div id="newsFooter">
          <a href="newsArchive.cfm?n=#getNews.news_id[getNews.RecordCount]#">News Archive</a>
        </div>
            </cfoutput>
    </div>

<!-- newsBox -->

If someone could point me in the direction I need to take to wrap this up into a nice object-shaped component reusable on any site as long as the datasource and username/password match, I'd be very much obliged.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Heh, sorry, I was foolish enough to post before trying it out properly. For future reference to anyone, here's what I did. Simple as pie, really, just took some trial and error.

<cfcomponent>

<cffunction name="getNews" access="public" returntype="Any" >

    <cfquery name="getNews" username="*************" password="**********">
        SELECT * FROM news WHERE news_category='comic' ORDER BY news_id ASC
    </cfquery>
   
    <cfoutput>
        <div id="newsHeader">
            <h3>#getNews.news_title[getNews.RecordCount]#</h3>
            <h5>#DateFormat(getNews.news_date[getNews.RecordCount], "dddd, mmmm dd, yyyy")#</h5>
        </div>
        <div id="newsContent">
            #getNews.news_content[getNews.RecordCount]#
        </div>
        <div id="newsFooter">
<!---            <a href="newsArchive.cfm?n=#getNews.news_id[getNews.RecordCount]#">News Archive</a>--->       
                <a href="index.cfm">News Archive</a>
        </div>
    </cfoutput>
   
    <cfreturn getNews>

</cffunction>

</cfcomponent>

If anyone has any additions or advice regarding the safety/security etc of this, please let me know. Thanks for your patience and sorry about the quick post-finger.

Cheers,

Swader

Message was edited by: SwaderAIR

This topic has been closed for replies.

1 reply

SwaderAIR
SwaderAIRAuthor
Participant
April 28, 2010

As stated in the OP edit, solution found. Code can be found above.