Mar 25, 2008

Coldfusion Powerball lottery pick tool

Ever wanted to pick lottery numbers with your own tool? This tool is setup for the powerball. and has set by step explanations.

<!---in coldfusion pick random values is a pain, because if you need five numbers changes are you will get duplicates.--->

<!---We have added some code to deal with that.--->

<!---First we are setting a parameter to create an array called randArray.--->

<cfparam

name="randArray"

type="any"

default="#ArrayNew(1)#" />

<!---I am setting a loop incremental to zero because I need to work with it in my loop. I need five numbers, but first I set it to zero...stay with me.--->

<cfset loopIncremental = 0>

<!---Now we are creating a conditional loop to check to see if the loop is less than five. It is so we are are going to loop and add 1 to the loop incremental until it hits five then stop (see we need five white balls). --->

<cfloop condition="loopIncremental lt 5">

<!---The arrRandRange is the range of values to pick numbers from. In powerball we need 1 through 54.--->

<cfset arrRandRange = randrange(1, 54)>

<!---We are now taking our array we set above int he parameter and change it to a list--->

<cfset randArrayToList = arraytolist(randArray)>

<!---Now we are checking to see if the list has a duplicate value. and setting that to an if statement which goes like this. If the array/list has the number we just randomly picked, we do nothing. If we have a fresh number we append the array and add that value to it.--->

<cfif listfind(randArrayToList, #arrRandRange#)>

<cfelse>

<!---here is where we add the value if the 'condition' is non-duplicate.--->

<cfset ArrayAppend(randArray,"#arrRandRange#")>

<!---we then increment the loop incremental by 1 (see if if it doesn't get added to the array we are still one number short and so we do not set the incriment. Clear as mud?--->

<cfset loopIncremental = #loopIncremental# + 1>

<!---then we exit the if statement--->

</cfif>

<!---next we exit the loop--->

</cfloop>

<cfoutput>

<!---I'm probably doing a usless step here but my basic effort is to sort the array's numbers from lowest to highest and then output the numbers to the screen.--->

<cfset xArrayToList = ArrayToList(randArray)>

<cfset yListSort = listsort(xArrayToList, "numeric")>

#yListSort#

</cfoutput>

<!---And for my last trick I am grabbing one red ball from a range 1 through 50--->

<cfset arrRandRange2 = randrange(1, 50)>

<!---then outputting that value--->

<cfoutput>

#arrRandRange2#

</cfoutput>

and abracadabra I have my lottery number picks...I hope they are winners.

Labels: , , ,

Mar 24, 2008

Looping backwards over a list or array

Many months back I was on a job interview and someone asked me to loop backwards over a name, essentially spelling it backwards. What an odd request. I fumbled around and was totally stumped. I didn't get the job, but you can bet your ass I figured out how to loop backwards over a list/array or whatever. Below is the proof of concept example code.

Enjoy.

<!---Loop backwards example.--->

<!---First we are going to create a list.--->

<cfset myList = "F,r,a,n,k,T,u,d,o,r">

<!---Next we are going to create an array out of it.--->

<cfset myArray = listToArray(myList)>

<!---Now weare going to set up our loop with parameters.--->

<br>

<!---Note that I am setting the from and to max to min and

setting step to -1 so it can step backwards throught he loop.

--->

<cfoutput>

<cfloop from="#arrayLen(myArray)#" to="1" step="-1" index="i" >

#myArray[i]#<br>

</cfloop>

</cfoutput>

Labels: , , , ,

Coldfusion try/catch example.

Like other languages, Coldfusion has the ability to attempt code and then provide a custom error if it fails. This is good if you cannot test your code and example would be if you are waiting for other groups to get database development done, or server environment constraints.

So here is a simple example.

Say I have a query that adds favorites to my table first I am going to set some simple variables

<cfset me = "Frank">

<cfset myFav = "Franklin">

Next, I am going to set up my try/catch code Note I have a primary key made up of fvrt_src, fvrt_sel, meaning I cannot add it twice and if I refresh the screen the error will be produced.

<cftry>

<cfquery

datasource="#request.dsn#"

name="addingToFav">

insert into favorites (fvrt_src, fvrt_sel)

values ('#me#', '#myfav#')

</cfquery>

If the error is produced the catch will sense the database error and produce a nice controlled session error instead of a screen with gibberish (client confusion screens).

<cfcatch type="database">

<cfset session.favAddError = "User has already been added to your favorties<br>

please check your favorites list above.">

</cfcatch>

</cftry>

Next if the session has been created I display the error message below.

<cfif isdefined(“session.favAddError”)>

<cfoutput>

#session.favAddError#

</cfoutput>

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: , , , , , , , ,

Mar 3, 2008

CFC Simplicity

Ok so here is my quick and dirty primer on CFCs. CFCs are the defacto method for keeping your development nice and clean. For this example i am using a simple query and then invoking it in a web page. There are two files you need the CFC and the CFM calling the CFC.

So here are the files assuming you have a database registered in your Coldfusion administrator.

index.cfm
qryPerson.cfc

For the index page here are the contents with explanations and the cfc with explanations.

<!---First we are invoking the cfc component, it's method

which is the same name as the component, and giveing

the return variable the same name. You can get

creative but you will atleast have to have these

three items. --->

<cfinvoke component="qryPerson" method="qryPerson" returnvariable="qryPerson">

<!---Here ais the only argument I am passing the first name is frank.--->

<cfinvokeargument name="first" value="Frank">

<!---Closing the invoke.--->

</cfinvoke>

<!---Now I am dumping out the return query.--->

<cfdump var="#qryPerson#">

<!---If you want to actually present the return...do this...--->
<cfoutput query="qryPerson">

#first_name# #last_name# #userID#

</cfoutput>

Here is the qryPerson.cfc (component).

<!---This cfcomponent can be modified to alter combinations

of first and last or even first, last and userID.

But your if statement would be more complex.--->

<!---Keeping it very simple here first cones the component name.--->

<cfcomponent>

<!---The function comes next make sure it is sent to return type query--->

<cffunction name="qryPerson" access="public" returntype="query">

<!---Here is my arguement I am calling it is not requiried--->

<cfargument name="first" type="string" required="yno">

<!---Then my query it is very simple but notice the embedded

if statement. Notice that the variable says with

arguments.first ARGUMENTS is what you use inside the CFC unless

you set it to a different variable but for simplicity I pulled

it directly, which is perfectly acceptable to do.--->

<cfquery datasource="emp" name="qryPerson">

Select * from emp

<cfif isdefined("arguments.first")>

where first_name = '#arguments.first#'

</cfif>

</cfquery>

<!---Now we return the query to this cfreturn tag..

just use the name of the query.--->

<cfreturn qryPerson>

</cffunction>

</cfcomponent>

Any questions? drop me a line at frank_tudor@yahoo.com

Labels: , , , , , , ,

Dec 21, 2007

Variable Primer

Vocab:

Variable: a container that holds something

Value: assigned to a variable

example = variable = value

In coldfusion the simple variable can be handled in a few ways.

For example say we have the word 'apple' and we want to take this word and pass it throughout our application.

In a form this is how you would do it:

<form action="variables2.cfm" method="post">
<input name="fruit" value="apple" type="text">
<input value="submit" name="submit" type="submit">
</form>

and on the variable2.cfm page you would need code that looked like this to display it.

<cfoutput>
</cfoutput><p>passed variables</p>
<cfif>
#form.name#
</cfif>

This is a URL what we can a query string variable

<a href="http://www.blogger.com/variables2.cfm?name=frank">URL variable example</a>

For a session you would set your variable like this: Session are great for storing variables that are needed throughout your website. so instead of move hidden input fields through forms, or long crazy querystring you simple set the session.whatever and assign the value.

<cfset firstname=" 'Doug'">
<cfoutput>
#session.firstname#
</cfoutput>

Here is another type of variable called a 'request' variable. This type of variable only lasts for the life of the page...Move to another page and it is gone. request.variables are useful for database references where you would assigned the data source to something like request.dsn (datasource name).

<cfset frank=" 35">
request.frank is <cfoutput>#request.frank#</cfoutput>

The last variable is just the variable's variable where you would set your variable in somethign like this &lt;cfset fruit = 'apple'&gt; it only lasts for the duration of the page, move away from the page and it is gone.

Frank

Labels: , , , , , , ,

Dec 19, 2007

Best and worst audiobooks

I have been listening to many audiobooks lately and here is what i have to report from the limited files I have.

First, The Hitchhiker's Guide to the Galaxy read by Stephen Fry. Is possibly one of the best available.

On the low end of the spectrum. Ivanhoe read by Flo Gibson. This audiobook book was horrid. And I don't think it is the material so much than the voice of this woman. Her voice is so difficult to stomach. Ivanhoe is not easy as it is. There is so much environment settings and character setting that it can get tedious, but Flo's voice is much more miserable.

All the harry Potter books are great including the first book and the last one (Deathly Hollows) are great.

The Terror is fairly good if you have never had exposure to harrowing artic tales before.

Frank

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: , , , , , , ,

Dec 13, 2007

On the subject of recursion or parent child relationships in data sources. It is a difficult concept if you are non-traditional programmer like myself. I think the hardest part is calling the function over and over and then move out of the loop and back up the tree. I did a lot of searching, especially for a Coldfusion based example. Here is what i found:

EasyCFM example

CuttersCrossing example

Doug Boude example

Kinky Solution : Ben Nadel example

I asked Ben to create a recursive solution for me because the other three were too difficult for me. The Doug Boude example shows a sudo-solution that I actually incorporated but then felt guilty about, but Ben really simplified the concepts and the example is extensible that you can actually use the code in your own solution.

Labels: , , ,