Archive

Archive for the ‘Free Stuff’ Category

Extending Coldfusion with UDFs

December 3rd, 2008

I cobbled together a few functions you may find useful this morning. As you can see they are simple, albeit useful, and so I wanted to share them here. Enjoy, and keep on coding!

/*
FUNCTION:   ArrayFindString
PURPOSE:    Searches inside the specified array (array_obj)
              for a string matching string (str)
PARAMETERS: 1. array_obj: the array containing the strings you
                 want to try to match to str
            2. str: the string you will be looking for in array_obj
RETURNS:    boolean
NOTE:       not case sensitive
*/
function ArrayFindString(array_obj, str){
var i = 1;

for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (array_obj[i] eq str) return true;

return false;
}

/*
FUNCTION: 	ArrayContainsString
PURPOSE: 	Searches inside the specified array (array_obj) for a
                  string that contains the string (str)
PARAMETERS: 	1. array_obj: the array containing the strings you want
                     to try to match to str
                2. str: the string you will be looking for in array_obj
RETURNS: 	boolean
NOTE: 		not case sensitive
*/
function ArrayContainsString(array_obj, str){
var i = 1;

for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (findNoCase(str, array_obj[i])) return true;

return false;
}

/*
FUNCTION: 	StringSearchArray
PURPOSE: 	Searches inside the specified string (str) for any
string matches contained in the array (array_obj)
PARAMETERS: 	1. str: the string you will be searching inside of
                2. array_obj: the array containing the strings you want
                     to look for in str
RETURNS: 	boolean
NOTE: 		not case sensitive
*/
function StringSearchArray(str, array_obj){
var i = 1;

for (i = 1; i lte arrayLen(array_obj); i = i + 1)
if (findNoCase(array_obj[i], str)) return true;

return false;
}

If your curious, I use StringSearchArray to control which areas of my application require user login:

...
foldersNoLogin = arrayNew(1);
foldersNoLogin[1] = "/login";
foldersNoLogin[2] = "/registration";
</cfscript>
<cfif not isDefined("session.loggedin") or session.loggedin eq false>
<cfif not StringSearchArray(cgi.path_info, foldersNoLogin)>
	<cflocation addtoken="no" url="/login/">
</cfif>
</cfif>

Free Stuff , , ,

FREE CODE: Paging record list.

November 13th, 2008

When listing records from a database you will often need to break those records up into multiple pages, so that your users do not see hundreds of rows at once. What follows is a custom tag which does exactly that.

Parameters

  1. pageScale - (integer) Default 0. This will limit the number of pages returned depending on the current page. 0 means no scaling will occur.
    Example: pageScale=5, startRow=24, pageSize=8, records=64. Given these parameters this tag will return an array of 5 structures. The first page will start with 2 the last page will be 6. There are 8 total pages.
  2. startRow - (integer) Default 1. The first record of the current page.
  3. pageSize - (integer) Default 20. How many records to list per page.
  4. records - (integer) Default 0. How many records to page through.
  5. variable - (string) Default “pages”. The name of the array to create in the caller scope.

Source Code

Enjoy, it’s free. Until next time, dream in digital.

Free Stuff, web development ,