Mar 6, 2008

Easy CFC

This is a simple coldfusion pages and the accompanying cfc.  
There are two functions that do different things. It might be
clear as mud so don't hesitate to drop me a line if you get lost,
but here is the idea, I am craeting two functions and invoking
them in differnt ways one example is through createObject and
the other is the cfinvoke tag.

(first our index.cfm page)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>
Untitled Document
</title>
</head>
<body>

<!---
Using cfset to call an object and output a value
In a cfc I have a component that has a function called timer.
Below I an 'instanciating' the object and
assigning it to a variable named 'time'
instead of using cfinvoke I am using the function
createObject. createObject has several parameter settings.
For a CFC and if that CFC is in the same directory as this file.
Then I simply set the createObject to call a component named time.
--->

<cfset time = createObject('component', 'myCfc')>

<!---
Below I am outputting the variable I set above.
Notice I am using the dot notation.
time.timer()
Notice the paranthesis. If my object
took an argument I would place it there.
Like this time.timer(today,tommorow,yesterday,etc)
But I have nothing to pass since
this is only a simple example.
--->

<cfoutput>
#time.x("This is a argument/parameter being passed to my object")#
</cfoutput>
<br<br>

<!---Here is a quick example of a cfc that does some arithmetic.--->

<!--- We are using the invoke method (tag) to pull this object into play --->

<cfinvoke component="myCfc" method="z" returnvariable="goober">

<cfoutput>
#goober#
</cfoutput>

</body>
</html>

(my next file is called myCfc.cfc)

<cfcomponent hint="amigo formats the date...if argument gets passed then i append it to the amigo string">
<cffunction
name="x"
description="the function name is x">
<cfargument
name="today"
type="string"
required="no">

<cfset amigo = timeFormat(now(), "h:MM")>

<cfif isdefined("arguments.today")>
<cfset amigo = arguments.today & " " & amigo>
</cfif>
<cfreturn amigo>
</cffunction>

<cffunction name="z">
<cfset x = 100>
<cfset y = 5>
<cfreturn x/y>
</cffunction>
</cfcomponent>

Labels: , , , , , , , ,

Dec 19, 2007

Simple session and setting sessions with cfset example

As part of my effort for creating simple Coldfusion examples. I want to take about variables. As we know in a normal website we are taught to pass URL or FORM variable from page to page. This is great for a one or two things or for a form of variables/values. So there is another very useful way to pass variables and that is by sessions. I won’t explain what a session 'is' except that it works with cfset tag like normal variables.

Let me show you

URL variables look like this when you pass it from a URL query string.

http://www.sitename.com/index.cfm?binky=boink

then you pull it into the the target page like this:

<cfoutput>
#url.binky#
</cfoutput>

You don't need the value after the equal sign because when the page resolves it will show on the screen.

imagine a form on a page that has a textbox input area. The label of the this 'input' text field is called 'name'. Then you type in the word 'frank' and hit submit.

It passes the value to a page called formCatch.cfm

You would display it like this.

<cfoutput>
#form.name#
</cfoutput>

It would output the word frank.

And now lets look at a session:

<cfset session.name = 'frank'>

<cfoutput>
#session.name#
</cfoutput>

It would output the name Frank, but here is the good thing about sessions, you can go to any page after setting the session variable and call it.

So lets go back to the FORM and URL variables. those are great for one page justs, but after that they loose purpose.

sessions stay for the duration of your access to the website or until you explicitly tell it togo away.

Here is how you do that.

<cfset StructDelete(Session, "name")>

One other note. Before you can use sessions in your website you have to 'tell' it that it will use sessions.

Here is how that works.

In your web application directory you can create a blank .cfm file called 'Application.cfm'. Put this file on the directory where you will use sessions.

Then add this line of code:

<cfapplication sessionmanagement="yes">

So that is sesssions in a nutshell. This should get anyone by with sessions.

Frank

Labels: , , , , , , ,