Solved

CREATE COMPONENT-CFFUNCTION

Posted on 2008-10-03
3
273 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 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

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
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…

635 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