|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
|
How to use Coldfusion Components
Okay...
I'm trying to improve my coding skills and practices so I'm looking into CFCs. It would be nice to split out my logic and my display. I read over Ben Forta's article on how to use CFCs but it seems to be leaving some things out. For example, I've got a CFC that I built. All it does is to pull out the contents of a "page" in my database and display it. I thought that this would be a simple thing to do to get started. It works when I hard code the page id into the CFC, but I want to be able to pass the page id from the page calling the component. Here's my CFC: PHP Code:
PHP Code:
WHERE page_id = '#pageid#'; But it doesn't work. What am I doing wrong? Does someone have a good resource for this sort of thing? I really want to learn but nothing I've found on the web so far is really helpful. They all sort of assume that you know what CFCs are already.
__________________
Adobe Certified Coldfusion MX 7 Developer Adobe Certified Advanced Coldfusion MX Developer My Blog (new) | My Family | My Freelance | My Recipes |
|
|
|
|
|
#2 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2001
Location: Western CT, USA
Posts: 813
|
try:
WHERE page_id = '#arguments.pageid#' you need them quotes? putting integers in a varchar fields, are we? tsk tsk ![]() |
|
|
|
|
|
#3 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
|
No integers in varchar fields. I know better. I've found though that when I single quote the field, I don't get an error if for some ungodly reason the variable isn't there.
Actually, I found out what I needed, although your way might work as well. In the function, I defined an argument like so: <cfargument name="pageid" type="numeric" required="true"> Works just fine. I'm going to try your way though. Seems silly to have to define an argument when I'm already passing on in. But, whatever works. Tested your way and it works as well. I'll use that one instead. Thanks for your help.
__________________
Adobe Certified Coldfusion MX 7 Developer Adobe Certified Advanced Coldfusion MX Developer My Blog (new) | My Family | My Freelance | My Recipes |
|
|
|
|
|
#4 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2001
Location: Western CT, USA
Posts: 813
|
no problemo...
are you sure you're saving anything? if the query has to be run for every page, why not just put it Application.cfm ? |
|
|
|
|
|
#5 | |
|
minister of propaganda
![]() Join Date: Feb 2004
Location: Midsouth
Posts: 1,399
|
Quote:
try something more like this: Code:
<cfcomponent hint="">
<cffunction name="selectpage" hint="Selects page content from the database based on query string variable." output="true" returntype="query">
<cfargument name="pageid" type="numeric" required="true">
<cfquery name="getPageInformation" dataSource="#Application.DSN#" dbType="#Application.DBType#" username="#Application.username#" password="#Application.password#">
SELECT *
FROM tblpages
WHERE page_id = #arguments.pageid#;
</cfquery>
<!---<cfoutput>#getPageInformation.page_content#</cfoutput>--->
<cfreturn getpageinformation>
</cffunction>
</cfcomponent>
Code:
<cfinvoke component="(put the path to the component here, ask if you need help with this)" method="selectpage" returnvariable="selectpageret"> <cfinvokeargument name="pageid" value="4"/> </cfinvoke> there are several other things you can do and reasons to do it with components, let me know and ill give some more examples. and let me know if you have any trouble with what ive put here
__________________
rynoguill Ryan Guill, AKA Mark Roman |
|
|
|
|
|
|
#6 |
|
SQL Consultant
![]() ![]() ![]() ![]() Join Date: Jul 2002
Location: Toronto, Canada
Posts: 32,859
|
has anybody done timings on components versus inline cfml?
all i can say is, for simplicity, i prefer this method i save the query as getPageInformation.txt: Code:
<cfquery name="getPageInformation" ... >
select foo, bar, quatsch
from tblpages
where page_id = #pageid#
</cfquery>
Code:
<cfset pageid=4> <cfinclude template="getPageInformation.txt"> i can use and reuse this cfquery anywhere, just as easily as that what am i missing?
__________________
r937.com | rudy.ca | Buy my SitePoint book: Simply SQL "giving out my real stuffs"
|
|
|
|
|
|
#7 | |
|
minister of propaganda
![]() Join Date: Feb 2004
Location: Midsouth
Posts: 1,399
|
Quote:
reusability of code, i only have to change things in my code in one place, i can use them as webservices, im an oop kinda guy, i can do a lot of error checking, it is really easy to use them on multiple sites, just a few changes to port them, plus, its the wave of the future man! and i havent noticed any performance differences, in fact if anything i feel like its faster or at least just as fast as inline code. and i have switch a couple older sites over to components. just my two cents anyway...
__________________
rynoguill Ryan Guill, AKA Mark Roman |
|
|
|
|
|
|
#8 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Jul 2001
Location: Western CT, USA
Posts: 813
|
whelp,
r937 gets down with OPP? insert into table e!?!? yeah, you know me! (bad joke, about bad music) the real advantage to OOP is you can write functions that EXTEND other functions. uhhhh, inheritance is the word (i think). but do typical websites require this level of abstraction? prolly not. if you were working for a hospital, insurance company, or some other giant outfit with a complicated intranet and website you would certainly find situations. but for simple stuff, i think it's overkill. |
|
|
|
|
|
#9 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Location: Nashville, TN USA
Posts: 1,775
|
To be quite honest, I wish I could rewrite my companies entire application with CFMX using components. It's a large app that gets millions of hits a day and it desperately needs restructuring.
For my own websites, though, the only time I may use them is experimentally at first to see what they're like to use.
__________________
WishList.com - Universal Gift Registry KodeFusion.com | AgentOvation | Web Dev Sucess Blog | Net Realty |
|
|
|
|
|
#10 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
|
Rudy...
While I'm not a great judge of speed, it seems to me as if the page loads faster after converting that inline code to a component. Sinec I'm using it in multiple places, it might be caching the bulk of the query.
__________________
Adobe Certified Coldfusion MX 7 Developer Adobe Certified Advanced Coldfusion MX Developer My Blog (new) | My Family | My Freelance | My Recipes |
|
|
|
|
|
#11 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,851
|
While I'm on components, here's another question.
I've got banner management built into this new site I'm working on: http://997wtn.com.icglink.com In the header I have two banner areas defined, top right (top right corner) and bottom left (under the nav buttons on the left). I created another component to manage my banner queries; one function to get a random banner, and another function to update that banner's impression count. I would love to use this component in my header, just for consistencies sake but I've got an issue. I have two plain old inline queries right now, one query to get the top banner, and one to get the bottom. Then I have ONE update query which loops through those two banner ids and increments their impression. How would I go about using components here? I've read that you can invoke the component using the cfobject tag, then CALL it using the cfinoke tag with a name attribute. But it doesn't seem to work. So, rynogull or anyone else, can you guide me here? How can I use two calls to the same component, returning the same information, just with a different banner_id? thanks
__________________
Adobe Certified Coldfusion MX 7 Developer Adobe Certified Advanced Coldfusion MX Developer My Blog (new) | My Family | My Freelance | My Recipes |
|
|
|
|
|
#12 | |
|
minister of propaganda
![]() Join Date: Feb 2004
Location: Midsouth
Posts: 1,399
|
straight from the coldfusion 6.1 documentation,
Quote:
__________________
rynoguill Ryan Guill, AKA Mark Roman |
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 09:23.













Linear Mode
