Solved

CREATE COMPONENT-CFFUNCTION

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
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 set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now