Solved

CREATE COMPONENT-CFFUNCTION

Posted on 2008-10-03
3
253 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
  • 2
3 Comments
 
LVL 25

Accepted Solution

by:
dgrafx earned 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IIS 8.5 2 52
JVM encoding. How to change encoding. 27 102
AWS New EC2 Instance and EBS Storage 2 78
How to best troubleshoot slow internet connections via proxy server? 2 81
A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
The purpose of this video is to demonstrate how to properly insert a Vimeo Video into a WordPress site or Blog. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

777 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