Wednesday, October 20, 2010

I lost DB.php, Where do I find it?

I was helping a colleague get a website back online after a server meltdown.  The database was not connecting and the page was complaining about a missing file:  DB.php.  I checked the backup and the files were not there.

Of course the first thing that came to mind is how did this website ever work if it needs that file...?

After some Googling and testing I found that  I needed to install the DB model from the PEAR Repo.

pear install DB

I ran that as root and I was in business.

Sunday, October 10, 2010

Coldfusion Form Validation for the Lazy Programmer

I love to program but I don't like to type.  I constantly look for ways to type less but accomplish more.  When I am not using CFWheels I end up having to type a whole lot more.

The other day I came up with this function:

<cfset not_required="billingform,address_2">
<cfloop collection="#form#" item="key">
<cfif not listFindNoCase(not_required,key) and trim(form[key]) is "">
 <cfset variables.e = variables.e & "<li>#ucase(replace(key,'_',' ','all'))# is a required field.</li>">
</cfif>
</cfloop>



The only thing I am not happy about is it doesn't show the error messages in any kind of order, which makes longer form errors look kind of weird, but I don't consider that to be too big of an issue because most of the time a user is only gonna be shown 1 or 2 error messages.

So what do you think?  How could this function be improved upon?

Friday, October 8, 2010

Caps First Title -- Capitalize the first letter of each item in a list

A while ago I needed to capitalize the first letter of every item in a list.  I created a handy dandy UDF in Coldfusion.

<!---
Capitalize the first letter of each item in a list.

@param str      List to parse. (Required)
@param delimiter      List delimiter. Defaults to a comma. (Optional)
@return Returns a string.
@author Randy Johnson (randy@cfconcepts.com)
@version 1, May 30, 2010
--->

<cffunction name="capFirstList" returntype="string" output="false">
<cfargument name="str" type="string" required="true" />
<cfargument name="delimiter" default="," required="false">

<cfset var newstr = "" />
<cfset var word = "" />
<cfset var separator = "" />

<cfloop index="word" list="#arguments.str#" delimiters="#arguments.delimiter#">
<cfset newstr = newstr & separator & UCase(left(word,1)) />
<cfif len(word) gt 1>
<cfset newstr = newstr & right(word,len(word)-1) />
</cfif>
<cfset separator = arguments.delimiter />
</cfloop>

<cfreturn newstr />


So what do you think?  How can I improve on this function?