Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Call functions within a cfc

Status
Not open for further replies.

johk02

Programmer
Aug 20, 2003
169
AU
Hi ,
I am trying to learn cfc's and I have problem that I can't figure out. Basically I have one function that returns a query.
And for practise I want to display the result of the first function in the second function.
The problem I have is how do I define the result from the first function in the second function.
Any suggestions would be much appreciated.
Code:
<cffunction name="ListCat" hint="Get query for business categories"  >
<!--- Optional argument--->
	<cfargument name="CatID" required="No" default="" >
	
	
<!--- Run Query --->
	<cfquery name="GetBusinessCat" datasource="#request.SiteDSN#">
		SELECT BusinessCatID, BusinessCatName
		FROM holanet.holanet_businesscat
<!--- If argument is defined --->
		<cfif ARGUMENTS.CatID NEQ "">
		WHERE BusinessCatID = #ARGUMENTS.CatID#
		</cfif>
		ORDER BY holanet_businesscat.BusinessCatName
	</cfquery>
	
<!--- Return Result  --->
<cfreturn GetBusinessCat>		
</cffunction>


<cffunction name="ListCatAgain" >

	<cfset NewList = GetBusinessCat.BusinessCatName >
	
<cfreturn NewList>
</cffunction>
 
coud you please try this ?

<cffunction name="ListCatAgain" access="public" returntype="query">
<cfset myResult = ListCat().BusinessCatName>

<cfreturn myResult>
</cffunction>


hope it hellps

 
Thanks,
I tried your suggestion but I got the following error message:
You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.

This is the code in the cfc
Code:
<cffunction name="ListCatAgain" access="public" >
   <cfset myResult = ListCat().BusinessCatName>
 <cfreturn myResult>
</cffunction>
This is the codeI used to call the function
Code:
<cfinvoke component="#cfcpath#.holanet" method="ListCatAgain" returnvariable="ListCat1">
<cfoutput>
#ListCat1.myResult#<br />
</cfoutput>

Is it that I call the function wrong?

Thanks

jonas
 
BusinessCatName is a column in the table holanet_businesscat.

Thanks

jonas
 
First, congrats to learning and trying to keep up with the new tech.

Here is a simplified test showing that you can indeed do what you want. remeber to always use var in component methods. Not doing so CAN and WILL eventually catch up to you!

also, please make sure to try and specify as many of the cffuntion attributes as you can, it makes things easier and more robust later.

Code:
<cffunction name="ListCat" hint="Get query for business categories"  >
<!--- Optional argument--->
    <cfargument name="CatID" required="No" default="" >
    
		<cfset var GetBusinessCat = structNew()>
<!--- Run Query --->
    <cfset GetBusinessCat.CatName = "Phoebe">
		<cfset GetBusinessCat.CatSound = "Meow">
    
<!--- Return Result  --->
<cfreturn GetBusinessCat>        
</cffunction>


<cffunction name="ListCatAgain">
    <cfset var	newlist = ListCat().catname>

<cfreturn NewList>
</cffunction>

Kevin

Phase 1: Read the CFML Reference
Phase 2: ???
Phase 3: Profit!
 
The different business categories are stored in the table "businesscat". In the same table is the english name and the spanish name stored with a "|" as a delimiter like spanish|english. Unfortunately it is not my choice how the categories are stored.
I have set up that depending on what session you are in it will display english or spanish content on the site.
At the moment I only have the query in the cfc (code 1) and then dealing with the with what language that are going to be displayed outside the cfc (code 2).
Ultimately I want to have that if statement in the cfc as well. I initially thought that I had to do the query in one cffunction and then have the if-statement in another cffunction. I hope it make sense:
imstillatwork - I tried your approach but I couldn't get it to work.
Code 1
Code:
<cffunction name="ListCat" hint="Get query for business categories" returntype="query"  >
<!--- Optional argument--->
	<cfargument name="CatID" required="No" default="" >
<!--- Run Query --->
	<cfquery name="GetBusinessCat" datasource="#request.SiteDSN#">
		SELECT BusinessCatID, BusinessCatName
		FROM holanet.holanet_businesscat
	<!--- If argument is defined --->
		<cfif ARGUMENTS.CatID NEQ "">
		WHERE BusinessCatID = #ARGUMENTS.CatID#
		</cfif>
		ORDER BY holanet_businesscat.BusinessCatName
	</cfquery>
<!--- Return Result  --->
<cfreturn GetBusinessCat>		
</cffunction>

Code 2
Code:
<cfoutput query="ListCategories">
<cfif session.Lang EQ "Eng">
#trim(listlast(ListCategories.BusinessCatName,'|'))#
</cfelse>
#trim(listfirst(ListCategories.BusinessCatName,'|'))#
  </cfif>
</cfoutput>

All suggestions are most welcome

Thanks

Jonas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top