Convert date format UDF

Today, I've created simple UDF for convert date format "dd/mm/yyyy" to "mm/dd/yyy". Because our clients want to use "dd/mm/yyyy" format as our national format into our projects. You may all know that it's very easy to convert but it's dedicated to those who start learning Coldfusion UDF.

[More]

Simple UDF for LIKE searching type

I bet, every programmer know "LIKE" searching type in all RDBMS. It's so simple and can help us to search by all types of character. Unfortunately, we need to avoid using LIKE command if we really don't need it because it can make query performance to getting slow and sometimes, RDBMS can be crush. Today, I need to use create simple UDF in Coldfusion for LIKE command. Because, we need consistency for LIKE command in our searching query.

That's why I wrote following UDF for LIKE.

view plain print about
1<cffunction name="UdfForLIKE" returntype="string" output="No">
2    <cfargument name="SearchParam" type="string" default="">
3    <!--- If search method doesn't declare, STARTING is default. --->
4    <cfargument name="SearchMethod" type="string" default="STARTING">
5    <cfset ReturnResult = "">
6    <cfswitch expression="#SearchMethod#">
7        <cfcase value="STARTING">
8            <cfset ReturnResult = SearchParam & "%">
9        </cfcase>
10        <cfcase value="ENDING">
11            <cfset ReturnResult = "%" & SearchParam>
12        </cfcase>
13        <cfcase value="WITHIN">
14            <cfset ReturnResult = "%" & SearchParam & "%">
15        </cfcase>
16        <cfcase value="EXACT">
17            <cfset ReturnResult = SearchParam>
18        </cfcase>
19        <cfdefaultcase>
20            <cfset ReturnResult = SearchParam & "%">
21        </cfdefaultcase>
22    </cfswitch>
23    <cfreturn ReturnResult>
24</cffunction>

[More]

REMatch in Coldfusion save my time

As we all know, there are a lot of functions in Coldfusion and I can't know all of them when I need to use. That's why I print out all of Coldfusion functions lists and put it on my wall. Even I did it already, I missed to use some core functions to put my projects. REMatch is one of them.

The usage of REMatch is to compare two strings and return the match string both of them as array. Adobe recommend that when we're gonna use REMatch, try it with regular expression and which will be powerful.

Before I didn't know REMatch function, I've created ListGetDifferent function by myself for searching the same value and the different value between two lists.

http://www.ppshein.net/index.cfm/2010/9/9/compare-list-in-coldfusion

Once I know REMatch, I feel I don't need to use my own UDF anymore when I need to compare two string but NOT list.

[More]

Internationalized in CFMX with Cffunction

It's, creating internationalize in CFMX with cffunction.

common.cfm

view plain print about
1<cffunction name="udf_Translate" output="Yes" returntype="string" hint="reads and parses UTF-8 resource bundle per locale">
2    <cfargument name="Name" required="Yes" type="string" />
3    <cfargument name="Filename" required="Yes" type="string" />
4    <cfargument name="Section" required="No" type="string" />
5    <cfset currentPath = getCurrentTemplatePath() />
6    <cfset currentDirectory = getDirectoryFromPath(currentPath) />
7    <cfset local_file = currentDirectory &amp; Filename />
8    <cfset flag = 0 /> <cfset i = 0 />
9    <cfif FileExists(local_file)>
10        <cffile action="read" file="#local_file#" variable="resourceBundleFile" charset="utf-8">
11        <cfset Transalated = ArrayNew(2) />
12            <cfloop index="rbIndx" list="#resourceBundleFile#" delimiters="#chr(10)#">
13                <cfset i = i + 1 />
14                <cfset Orginal_word = listFirst(rbIndx,"=") />
15                <cfset Transalted_word = listRest(rbIndx,"=") />
16                <cfset Transalated[i][1] = Orginal_word />
17                <cfset Transalated[i][2] = Transalted_word />
18                <cfif ASC(Transalted_word) NEQ 13>
19     <cfset Transalated[i][2] = Transalted_word />
20                <cfelse>
21                    <cfset Transalated[i][2] = Transalated[i][1] />
22                </cfif>
23                <cfif Transalated[i][1] EQ Name>
24                    <cfset flag = 1 />
25                    <cfreturn Transalated[i][2] />
26                </cfif>
27            </cfloop>
28    </cfif>
29    <cfif flag EQ 0>
30        <cfreturn Name />
31    </cfif>
32</cffunction>

index.cfm

view plain print about
1<cfoutput>#udf_Translate("Sex", "INPATIENT_EL.properties", "")#</cfoutput>

Compare list in Coldfusion

I'm trying to compare 2 lists and output the different value and same value of these 2 lists. Normally, CF doesn't have complete function of compare 2 lists. That's why I gotta create my own UDF for comparing 2 lists. How does it work :

  • ListGetDifferent(List1, List2, "Same/Differ")
  • If you defined "Same", then will return Same value of main list after comparing with second list.
  • If you defined "Different", then will return different value of main list after comparing with second list as well.

Function UDF

view plain print about
1<cffunction name="ListGetDifferent" output="yes">
2    <cfargument name="mainList" required="Yes" />
3    <cfargument name="compareList" required="Yes" />
4    <cfargument name="CompareType" required="Yes" default="Same" />
5    <cfset ReturnList = "" />
6    <cfif CompareType EQ "Same">
7        <cfloop list="#mainList#" index="i">
8            <cfif ListFindNoCase(compareList,i)>
9                <cfset ReturnList = ListAppend(ReturnList,i) />
10            </cfif>
11        </cfloop>
12    <cfelse>
13        <cfloop list="#mainList#" index="i">
14            <cfif NOT ListFindNoCase(compareList,i)>
15                <cfset ReturnList = ListAppend(ReturnList,i) />
16            </cfif>
17        </cfloop>
18    </cfif>
19    <cfreturn ReturnList />
20</cffunction>
Get from Same Value
view plain print about
1#ListGetDifferent("1,2,3,4,5", "1,3,5", "Same")# Return : 1,3,5

Get from Different Value

view plain print about
1#ListGetDifferent("1,2,3,4,5", "1,3,5", Differ")#
2Return : 2,4

Convert to Ordinal Format

Today, I need to develop one simple CFML UDF, which convert numeric format to Ordinal format. It's just simple UDF but useful for future my projects.

Coding

view plain print about
1<cffunction name="convertOrdinal" returntype="string" output="false">
2    <cfargument name="inputInt" required="yes" type="numeric" />
3    <cfparam name="OrdFormat" default="">
4    <cfsilent>
5        <cfif inputInt MOD 10 EQ 1 AND inputInt MOD 100 NEQ 11>
6            <cfset OrdFormat = "st" />
7        <cfelseif inputInt MOD 10 EQ 2 AND inputInt MOD 100 NEQ 12>
8            <cfset OrdFormat = "nd" />
9        <cfelseif inputInt MOD 10 EQ 3 AND inputInt MOD 100 NEQ 13>
10            <cfset OrdFormat = "rd" />
11        <cfelse>
12            <cfset OrdFormat = "th" />
13        </cfif>
14        <cfreturn TRIM(inputInt & OrdFormat) />
15    </cfsilent>
16</cffunction>

Usage

view plain print about
1<cfoutput>#convertOrdinal(1)#</cfoutput>

UDF (User Defined Function)

In these days, I feel like myself to create UDF, user defined function in cfml. Because, it's reusable and flexible to change when needed. When we want to develop Webservices (SOAP), UDF are the most powerful and compatible for every Web Development Language. That's why I decided I'll always create UDF whenever I develop any programs.

Webservices

Well, I'm interesting to create webservices (WSDL) in CF in these days. Because, I wanna test how to integrate two different language by webservices. Developing webservices is very simple. Ok, let's go.

helloWorld.cfc

view plain print about
1<cfcomponent>
2<cffunction name="HelloWorldMsg" access="remote" returntype="string" output="no">
3<cfargument name="name" type="string" required="yes">
4<cfreturn "Hello World : " &amp; arguments.name>
5</cffunction>
6</cfcomponent>

caller.cfm

view plain print about
1<cfinvoke webservice="http://helloWorld.cfc?wsdl" method="HelloWorldMsg" returnvariable="msg">
2<cfinvokeargument name="name" value="PPSHEIN"/>
3</cfinvoke>
4<cfoutput>#msg#</cfoutput>

Javascript UDF

Honestly, I'm so far to develop javascript UDF because I feel like myself to develop Coldfusion UDF instead of Javascript. Yesterday, I need to develop javascript UDF for part of my project. It's for return checkbox value whether this checkbox is being checked or not. It's just simple UDF.

view plain print about
1<form name="frmUDF">
2    <input type="checkbox" name="chkMyCheck">
3    <input type="button" name="btnCheck" onclick="beingCheck(document.frmUDF.chkMyCheck)">
4</form>

JS coding

view plain print about
1function beingCheck(obj) {
2     if (obj.checked == true)
3         var returnCheck = 1
4    else
5        var returnCheck = 1
6        return returnCheck
7    }

Top of Page