Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CREATE COMPONENT-CFFUNCTION

Posted on 2008-10-03
3
Medium Priority
?
278 Views
Last Modified: 2013-12-24
Hello experts.
I have two queries like:
<cfquery name="q_test1" datasource="#dsn#">
SELECT Mobile_ID, User_ID
FROM dbo.artikel
WHERE User_ID = <cfqueryparam value="#SESSION.User_ID#" cfsqltype="cf_sql_numeric">
   AND Mobile_ID = 1
</cfquery>
<cfquery name="q_test2" datasource="#dsn#">
SELECT Mobile_ID, User_ID
FROM dbo.artikel
WHERE User_ID = <cfqueryparam value="#SESSION.User_ID#" cfsqltype="cf_sql_numeric">
 AND Mobile_ID = 2
</cfquery>
And output:
<cfoutput>#q_test1.Recordcount#</cfoutput>
<cfoutput>#q_test2 .Recordcount#</cfoutput>
The question is how to create component and function and use this in my page.
0
Comment
Question by:Panos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
dgrafx earned 2000 total points
ID: 22634473
1) lets say you are housing your cfc's at [webroot]\comm\cfc\
then comPath is comm.cfc

2) To start - the following code would be saved as test.cfc in your #comPath#
<cfcomponent>
      <cffunction name="getStuff" returntype="query">
            Notice that I set the DSN to Application.DSN so the cfc is aware of it
            Otherwise you need to send the DSN in as an argument just like user_id
            <cfquery datasource="#Application.DSN#" name="getQuery">
            SELECT #arguments.Columns#
            FROM dbo.artikel
            WHERE User_ID = <cfqueryparam value="#arguments.User_ID#" cfsqltype="cf_sql_numeric">
            AND Mobile_ID = <cfqueryparam value="#arguments.Mobile_ID#" cfsqltype="cf_sql_numeric">
            </cfquery>
      <cfreturn getQuery>
      </cffunction>
</cfcomponent>
<!--- end test.cfc --->

3) Then on your web page - you invoke your component sending required var values
<cfinvoke component="#comPath#.test" method="getStuff" returnVariable="Testing">
      <cfinvokeargument name="User_ID" value="#session.User_ID#"/>
      <cfinvokeargument name="Mobile_ID" value="1"/>      
      <cfinvokeargument name="Columns" value="Mobile_ID,User_ID"/>
</cfinvoke>
<cfdump var="#Testing#">
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 31502677
Thank you for your help.
regards
panos
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 22635034
keep in mind that this was a simple example
you can add to it or take away
for ex I added the Columns argument to show you that you can do stuff like that
and if using sql server you can use stored procedures instead of cfqueries in your functions which will run much faster for longer running queries

good luck
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question