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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

include files

Status
Not open for further replies.

Aeros

Programmer
Oct 7, 2002
166
US
I have two include files. In order they load:

inc_file1.cfm
inc_file2.cfm

I need to pass some query info back from inc_file2.cfm to inc_file1.cfm. Is there a way to accomplish this?

Thanks
 
Why not load inc_file2 first?

Unless there are items in inc_file1 that are required for 2, then I suppose you could always just include inc_file1 again.
 
you can put the query in question in a custom tag and call it anywhere you want.

Beware of programmers who carry screwdrivers.
 
Because of the order of the way the page loads switching them aroudn is not an option. I ended up takign the query out of page 2 and doing an include with that. I dotn know why I didnt think of that sooner...errr

thanks for the replies
 
If you would like to 'pass' variables from one of your includes to another, set your values using Request scope. Request scope is visible for all includes, cfmodules ect.. originating from the same request.

For example 'inc_file2.cfm' would have:
Code:
<cfquery name="request.myQuery" datasource=" .. " .....>
 Select *
 FRom myTable
</cfquery>
 ...

Then in 'inc_file1.cfm', you could reference 'request.myQuery'.

Hope this helps,

jalpino
 
assuming

main.cfm contains
Code:
<html><body>
Hello everyone<br>
<cfinclude template = "firstInclude.cfm">
<cfinclude template = "secondInclude.cfm">
</body></html>

firstInclude.cfm contains
Code:
My name is: <cfoutput>#request.MyName#</cfoutput>

secondIclude.cfm contains
Code:
<cfset request.MyName = "joe">

if we put all of this together (like how i understand cf to include files) you get

Code:
<html><body>
Hello everyone<br>
My name is: <cfoutput>#request.MyName#</cfoutput>
<cfset request.MyName = "joe">
</body></html>

using #request.MyName# prior to defination would cause problems would it not? Or does the request scope bend the rules, or is my understanding of included files off the wall?

Beware of programmers who carry screwdrivers.
 
Hi SimulatedFun,

The first time you use your variable it becomes defines and because you are using it within request scope, it is immediately available to any subsequent requests for that variable. Even if the first 'appearance' of the variable is within the include, your calling page and any include there after will have access to it. Try it out:
Code:
<!--- Main.cfm --->
<cfinclude template="test.cfm">
<cfoutput> #request.myVar# </cfoutput>
Code:
<!--- test.cfm --->
<cfset request.myVar = "Hello World">

jalpino
 
right, i got that much, but I think aeros' problem was he was trying to use the variable in an include that was called BEFORE the include which held the variable. to use your example the order would be swapped. output before include.

Code:
<cfoutput> #request.myVar# </cfoutput>
<cfinclude template="test.cfm">


which produces the expected "myVar is undefined in request" error. (tested mx 6.1)

Beware of programmers who carry screwdrivers.
 
Hi SimulatedFun,

You are correct, using a variable before its defined will definitely toss an error. To resolve this, I would initialize the variable before hand, and if need be, test for an expected result before using it. (ie: <cfparam name="request.myVar" default=""> ).

Having taken a second look at Aeros question, no its not possible to pass information back to an include from one that executes after it. Its the same reason why you can't use a variable and then later define it, it won't exist.

Alternatives would be to extract the portion of dependant code from 'inc_file1.cfm' and place it in 'inc_file2.cfm' or vice-versa.

jalpino
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top