This is pretty straight forward
<cfset strTest = "#form.variable#">
<cfset strTarget = " ">
<cfset newTest=Chr(1) & strTest & Chr(1)>
<cfset intCount=DecrementValue(ArrayLen(newTest.split(strTarget.replaceAll("(\W)","\\$1"))))>
<cfoutput>#intCount#</cfoutput>
This will count the number of spaces in the variable.
If the intCount = 0, you are good to go.
You can use the code to test for any character.
Enjoy!
Thursday, January 29, 2015
Thursday, December 18, 2014
How to dynamically define quarterly dates in Coldfusion
You know those reports that upper management request from time to time? The ones that give you nightmares? Yeah those. Well sometimes they will request quarterly reports so I wanted to share an easy way to define quarterly dates.
So we start with our default variables:
<cfset quarter = 1>
<cfset year = "#year(now())#">
<cfset startmonth = 1>
And here are the functions that create the dates:
<cffunction name="QuarterFirstDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset firstDate = DateAdd("m",startmonth-1,CreateDate(yr, ((quarternumber-1)*3)+1, "1"))>
<cfreturn firstDate>
</cffunction>
<cffunction name="QuarterLastDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset lastDate = DateAdd("m",startmonth-1,CreateDate(yr, quarternumber*3, DaysInMonth(CreateDate(yr, quarternumber*3, "1"))))>
<cfreturn lastDate>
</cffunction>
And here is how I define my returned variables to be used in my code:
<cfset startDate = #DateFormat(QuarterFirstDate(quarter, year, startmonth))#>
<cfset endDate = #DateFormat(QuarterLastDate(quarter, year, startmonth))#>
To change the quarters, simply change the default quarter variable define at the top of the page:
<cfset quarter = 1> = 01-Jan-14 - 31-Mar-14
<cfset quarter = 2> = 01-Apr-14 - 30-Jun-14
<cfset quarter = 3> = 01-Jul-14 - 30-Sep-14
<cfset quarter = 4> 01-Oct-14 - 31-Dec-14
nJoy!
So we start with our default variables:
<cfset quarter = 1>
<cfset year = "#year(now())#">
<cfset startmonth = 1>
And here are the functions that create the dates:
<cffunction name="QuarterFirstDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset firstDate = DateAdd("m",startmonth-1,CreateDate(yr, ((quarternumber-1)*3)+1, "1"))>
<cfreturn firstDate>
</cffunction>
<cffunction name="QuarterLastDate" returnType="date">
<cfargument name="quarternumber" required="yes" type="numeric">
<cfargument name="yr" type="numeric" default="#year(now())#">
<cfargument name="startmonth" type="numeric" default="1">
<cfset lastDate = DateAdd("m",startmonth-1,CreateDate(yr, quarternumber*3, DaysInMonth(CreateDate(yr, quarternumber*3, "1"))))>
<cfreturn lastDate>
</cffunction>
And here is how I define my returned variables to be used in my code:
<cfset startDate = #DateFormat(QuarterFirstDate(quarter, year, startmonth))#>
<cfset endDate = #DateFormat(QuarterLastDate(quarter, year, startmonth))#>
To change the quarters, simply change the default quarter variable define at the top of the page:
<cfset quarter = 1> = 01-Jan-14 - 31-Mar-14
<cfset quarter = 2> = 01-Apr-14 - 30-Jun-14
<cfset quarter = 3> = 01-Jul-14 - 30-Sep-14
<cfset quarter = 4> 01-Oct-14 - 31-Dec-14
nJoy!
Wednesday, July 9, 2014
How to return a set number of rows in SQL using a variable
You're gonna love me for this one :)
I'll make it very simple too.
So I am sure you've use the TOP clause to retreive a certain number of rows from a query, right?
Here is an example of how you can set the value of TOP as a variable based on, let's say...user input.
CREATE PROCEDURE [dbo].[testRowCount]
(
@total int=0 input variable. This can be based on anything like user input, etc.
)
AS
DECLARE @top INT what you will use as the TOP variable
SET @top = @total here you set the value for TOP based on the input
And here is how you use in in the select statement
SELECT TOP(@top) *
FROM table
WHERE....
You're welcome :)
I'll make it very simple too.
So I am sure you've use the TOP clause to retreive a certain number of rows from a query, right?
Here is an example of how you can set the value of TOP as a variable based on, let's say...user input.
CREATE PROCEDURE [dbo].[testRowCount]
(
@total int=0 input variable. This can be based on anything like user input, etc.
)
AS
DECLARE @top INT what you will use as the TOP variable
SET @top = @total here you set the value for TOP based on the input
And here is how you use in in the select statement
SELECT TOP(@top) *
FROM table
WHERE....
You're welcome :)
Tuesday, June 10, 2014
Stored Proc Clean Up - Getting rid of procs you don't use
If you are like me, then you HATE clutter. I am a minimalist - yep even when coding. I hate to see stored procs laying around that I:
1. Don't know what they do OR
2. Don't know if they are even in use
Of course, you don't want to start deleting procs randomly (that's a no-no)
So here are a few snippets to help you with your SQL house keeping:
This one tells you all of the procs that have run since the last restart.
SELECT p.name, last_execution_time, s.type_desc, create_date, modify_date
FROM sys.dm_exec_procedure_stats AS s
INNER JOIN sys.procedures AS p
ON s.[object_id] = p.[object_id]
ORDER BY p.name;
And this one shows procs that have NOT been run since last restart:
SELECT p.*
FROM sys.procedures AS p
LEFT JOIN sys.dm_exec_procedure_stats AS s
ON s.[object_id] = p.[object_id]
WHERE s.object_id IS null
order by name
Happy Cleaning!
1. Don't know what they do OR
2. Don't know if they are even in use
Of course, you don't want to start deleting procs randomly (that's a no-no)
So here are a few snippets to help you with your SQL house keeping:
This one tells you all of the procs that have run since the last restart.
SELECT p.name, last_execution_time, s.type_desc, create_date, modify_date
FROM sys.dm_exec_procedure_stats AS s
INNER JOIN sys.procedures AS p
ON s.[object_id] = p.[object_id]
ORDER BY p.name;
And this one shows procs that have NOT been run since last restart:
SELECT p.*
FROM sys.procedures AS p
LEFT JOIN sys.dm_exec_procedure_stats AS s
ON s.[object_id] = p.[object_id]
WHERE s.object_id IS null
order by name
Happy Cleaning!
Thursday, October 31, 2013
Ajax with Double Bind
Ok this was pretty tricky but I figured it out :-)
Here are the requirements I needed to fulfill:
1. Display a select box of categories
2. Based on the category selected, do an autosuggest of institutions
3. Populate a hidden field with the institutionID based on the institution selected in #2.
Ok so here is my first SELECT:
<cfselect name="categoryID"
bind="cfc:ajaxlookup.getInstitutionCategories()"
bindonload="true" />
It called the getInstitutionCategories function in the ajaxlookup CFC.
Here is the method:
<cffunction name="getInstitutionCategories" access="remote" returnType="array">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<cfquery name="data" datasource="#request.dsn#">
SELECT strInstitutionCategory, intInstitutionCateogryNumber
FROM tblInstitutionCategories
where intInstitutionCateogryNumber IN (2,3,1,6,7,8,9)
ORDER BY strInstitutionCategory
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[i][1]=data.intInstitutionCateogryNumber[i]>
<cfset result[i][2]=data.strInstitutionCategory[i]>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
When the user selects the category, I take the category ID and search for institutions in that category using a call to the getInstitutions method in ajaxlookup CFC.
<cfinput type="text" name="institution"
autosuggest="cfc:ajaxlookup.getInstitutions({cfautosuggestvalue},{categoryID})" value="#form.institution#" size="60" showAutoSuggestLoadingIcon="false">
As you can see, two values are passed to the function: the value typed, and the category ID that the user selected in the first drop down.
Here is the getInstitutions function:
<cffunction access="remote" name="getInstitutions" output="false" returntype="array">
<cfargument name="search" type="any" required="true" default="">
<cfargument name="catID" type="numeric" required="true">
<!--- Define variables --->
<cfset var result=ArrayNew(1)>
<!--- Do search --->
<cfquery datasource="#request.dsn#" name="data">
SELECT strInstitutionName
FROM tblInstitutions
WHERE intInstitutionCategoryID = #catID#
AND strInstitutionName LIKE '#search#%'
ORDER BY strInstitutionName
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, strInstitutionName)>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
And for step 3, I want to populate a hidden field with the institutionID of the institution the user selected in step 2:
<cfinput type="hidden" name="institutionID" id="institutionID" bind="cfc:ajaxlookup.getInstitutionIDs({institution})">
It simply passes the institution name to the getInstitutionIDs function with a bind.
Here is the getInstitutionIDs function:
<cffunction access="remote" name="getInstitutionIDs" output="false" returntype="array">
<cfargument name="institution" type="string" required="true">
<!--- Define variables --->
<cfset var result=ArrayNew(1)>
<!--- Do search --->
<cfquery datasource="#request.dsn#" name="data">
SELECT intInstitutionID
FROM tblInstitutions
WHERE strInstitutionName = <cfqueryparam value="#institution#" cfsqltype="CF_SQL_VARCHAR">
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, intInstitutionID)>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
Enjoy!
Here are the requirements I needed to fulfill:
1. Display a select box of categories
2. Based on the category selected, do an autosuggest of institutions
3. Populate a hidden field with the institutionID based on the institution selected in #2.
Ok so here is my first SELECT:
<cfselect name="categoryID"
bind="cfc:ajaxlookup.getInstitutionCategories()"
bindonload="true" />
It called the getInstitutionCategories function in the ajaxlookup CFC.
Here is the method:
<cffunction name="getInstitutionCategories" access="remote" returnType="array">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(2)>
<cfset var i=0>
<!--- Get data --->
<cfquery name="data" datasource="#request.dsn#">
SELECT strInstitutionCategory, intInstitutionCateogryNumber
FROM tblInstitutionCategories
where intInstitutionCateogryNumber IN (2,3,1,6,7,8,9)
ORDER BY strInstitutionCategory
</cfquery>
<!--- Convert results to array --->
<cfloop index="i" from="1" to="#data.RecordCount#">
<cfset result[i][1]=data.intInstitutionCateogryNumber[i]>
<cfset result[i][2]=data.strInstitutionCategory[i]>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
When the user selects the category, I take the category ID and search for institutions in that category using a call to the getInstitutions method in ajaxlookup CFC.
<cfinput type="text" name="institution"
autosuggest="cfc:ajaxlookup.getInstitutions({cfautosuggestvalue},{categoryID})" value="#form.institution#" size="60" showAutoSuggestLoadingIcon="false">
As you can see, two values are passed to the function: the value typed, and the category ID that the user selected in the first drop down.
Here is the getInstitutions function:
<cffunction access="remote" name="getInstitutions" output="false" returntype="array">
<cfargument name="search" type="any" required="true" default="">
<cfargument name="catID" type="numeric" required="true">
<!--- Define variables --->
<cfset var result=ArrayNew(1)>
<!--- Do search --->
<cfquery datasource="#request.dsn#" name="data">
SELECT strInstitutionName
FROM tblInstitutions
WHERE intInstitutionCategoryID = #catID#
AND strInstitutionName LIKE '#search#%'
ORDER BY strInstitutionName
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, strInstitutionName)>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
And for step 3, I want to populate a hidden field with the institutionID of the institution the user selected in step 2:
<cfinput type="hidden" name="institutionID" id="institutionID" bind="cfc:ajaxlookup.getInstitutionIDs({institution})">
It simply passes the institution name to the getInstitutionIDs function with a bind.
Here is the getInstitutionIDs function:
<cffunction access="remote" name="getInstitutionIDs" output="false" returntype="array">
<cfargument name="institution" type="string" required="true">
<!--- Define variables --->
<cfset var result=ArrayNew(1)>
<!--- Do search --->
<cfquery datasource="#request.dsn#" name="data">
SELECT intInstitutionID
FROM tblInstitutions
WHERE strInstitutionName = <cfqueryparam value="#institution#" cfsqltype="CF_SQL_VARCHAR">
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, intInstitutionID)>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
Enjoy!
Bind data to an input field via autosuggest using data from another input field.
here is my first input:
<cfinput type="text"
name="country" value="#form.country#" validateat="onSubmit" size="60" showAutoSuggestLoadingIcon="false"
autosuggest="cfc:ajaxlookup.getCountries({cfautosuggestvalue})">
This is a simple CF autosuggest. It calls the getCountries function in the ajaxlookup cfc. The CFC is in the same directory as my file. I don't know if you have to do this but it just works better for me. :)
This call will suggest countries as the person types the name of the country.
(showAutoSuggestLoadingIcon="false" means that little loading icon won't show.)
Here is the getCountries function:
<!--- Country Lookup used for auto suggest --->
<cffunction access="remote" name="getCountries" output="yes" returntype="array">
<cfargument name="search" type="any" required="false" default="">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(1)>
<!--- Do search --->
<cfquery datasource="#request.dsn#" name="data">
SELECT strCountry
FROM tblCountries
WHERE strCountry LIKE '#arguments.search#%'
ORDER BY strCountry
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, strCountry)>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
So I want to send the country ID to the database, not the country name. So I am going to retrieve the country ID based on the country that the user selected in the above input.
Here is my 2nd input:
<cfinput type="hidden" name="countryID" id="countryID" bind="cfc:ajaxlookup.getCountryID({country})">
It uses the bind attribute to call the getCountry method while passing in the value of the country input.
Enjoy!
<cfinput type="text"
name="country" value="#form.country#" validateat="onSubmit" size="60" showAutoSuggestLoadingIcon="false"
autosuggest="cfc:ajaxlookup.getCountries({cfautosuggestvalue})">
This is a simple CF autosuggest. It calls the getCountries function in the ajaxlookup cfc. The CFC is in the same directory as my file. I don't know if you have to do this but it just works better for me. :)
This call will suggest countries as the person types the name of the country.
(showAutoSuggestLoadingIcon="false" means that little loading icon won't show.)
Here is the getCountries function:
<!--- Country Lookup used for auto suggest --->
<cffunction access="remote" name="getCountries" output="yes" returntype="array">
<cfargument name="search" type="any" required="false" default="">
<!--- Define variables --->
<cfset var data="">
<cfset var result=ArrayNew(1)>
<!--- Do search --->
<cfquery datasource="#request.dsn#" name="data">
SELECT strCountry
FROM tblCountries
WHERE strCountry LIKE '#arguments.search#%'
ORDER BY strCountry
</cfquery>
<!--- Build result array --->
<cfloop query="data">
<cfset ArrayAppend(result, strCountry)>
</cfloop>
<!--- And return it --->
<cfreturn result>
</cffunction>
So I want to send the country ID to the database, not the country name. So I am going to retrieve the country ID based on the country that the user selected in the above input.
Here is my 2nd input:
<cfinput type="hidden" name="countryID" id="countryID" bind="cfc:ajaxlookup.getCountryID({country})">
It uses the bind attribute to call the getCountry method while passing in the value of the country input.
Enjoy!
Wednesday, October 17, 2012
Customized CKEditor ToolBars Based on User Roles
So let's say you've built a really cool content management system (CMS) using ckeditor which has you feeling all "rock star-like" when you suddenly realize that you can't have everyone hitting the SOURCE button on the toolbar (things can get pretty ugly...)
Here's how to display a different toolbar set based on user roles.
So I have two roles within my CMS - a super editor and a basic editor. The super editor sees the advance toolbar and the basic, sees the basic.
In my config.js files, I define my toolbars:
Here is my basic tool bar (Basic Editor Access):
config.toolbar_Basic =
[
['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']
];
};
Here is my FULL tool bar (Super Editor Access):
config.toolbar_Full =
[
{ name: 'document', items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
//{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton',
// 'HiddenField' ] },
'/',
{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv',
'-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
//{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },
'/',
{ name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
{ name: 'colors', items : [ 'TextColor','BGColor' ] },
{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
];
Ok so here is my editor reference on the CFM page:
My textarea field
<textarea id="englishText" class="ckeditor" name="englishText">#englishText#</textarea>
Code to show the different toolbars:
<cfif session.user.roleID EQ 1>
<script type="text/javascript"> // <![CDATA[
CKEDITOR.replace( 'englishText', { toolbar : 'Full' });
// ]]>
</script>
<cfelse>
<script type="text/javascript"> // <![CDATA[
CKEDITOR.replace( 'englishText', { toolbar : 'Basic' });
// ]]>
</script>
</cfif>
I want to make a few notes about this code:
1. Notice I did not add the toolbar attribute to the textarea field
2. Notice I added the code UNDER the textarea field. For some reason that I don't know about and had to learn the HARD WAY (GRRRR....) the code has to go after the textarea.
Enjoy! :)
Here's how to display a different toolbar set based on user roles.
So I have two roles within my CMS - a super editor and a basic editor. The super editor sees the advance toolbar and the basic, sees the basic.
In my config.js files, I define my toolbars:
Here is my basic tool bar (Basic Editor Access):
config.toolbar_Basic =
[
['Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','About']
];
};
Here is my FULL tool bar (Super Editor Access):
config.toolbar_Full =
[
{ name: 'document', items : [ 'Source','-','Save','NewPage','DocProps','Preview','Print','-','Templates' ] },
{ name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
{ name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','SpellChecker', 'Scayt' ] },
//{ name: 'forms', items : [ 'Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton',
// 'HiddenField' ] },
'/',
{ name: 'basicstyles', items : [ 'Bold','Italic','Underline','Strike','Subscript','Superscript','-','RemoveFormat' ] },
{ name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote','CreateDiv',
'-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock','-','BidiLtr','BidiRtl' ] },
{ name: 'links', items : [ 'Link','Unlink','Anchor' ] },
//{ name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak','Iframe' ] },
'/',
{ name: 'styles', items : [ 'Styles','Format','Font','FontSize' ] },
{ name: 'colors', items : [ 'TextColor','BGColor' ] },
{ name: 'tools', items : [ 'Maximize', 'ShowBlocks','-','About' ] }
];
Ok so here is my editor reference on the CFM page:
My textarea field
<textarea id="englishText" class="ckeditor" name="englishText">#englishText#</textarea>
Code to show the different toolbars:
<cfif session.user.roleID EQ 1>
<script type="text/javascript"> // <![CDATA[
CKEDITOR.replace( 'englishText', { toolbar : 'Full' });
// ]]>
</script>
<cfelse>
<script type="text/javascript"> // <![CDATA[
CKEDITOR.replace( 'englishText', { toolbar : 'Basic' });
// ]]>
</script>
</cfif>
I want to make a few notes about this code:
1. Notice I did not add the toolbar attribute to the textarea field
2. Notice I added the code UNDER the textarea field. For some reason that I don't know about and had to learn the HARD WAY (GRRRR....) the code has to go after the textarea.
Enjoy! :)
Subscribe to:
Posts (Atom)