Skip to content

Instantly share code, notes, and snippets.

@orangexception
Created October 29, 2012 13:17
Show Gist options
  • Save orangexception/3973492 to your computer and use it in GitHub Desktop.
Save orangexception/3973492 to your computer and use it in GitHub Desktop.
A Better Refactor for Mike Henke
<cffunction name= "minorNamesToString"
output= "false"
hint= "I convert a query of minor names to a string.">
<cfargument name= "qMinorNames" />
<cfset var sMinorNames= "" />
<cfset var sPreviousLastName= "" />
<!--- Edge Case: 1 Name in Query --->
<cfif qMinorNames.RecordCount EQ 1>
<cfset sMinorNames= qMinorNames.First & " " & qMinorNames.Last />
<cfreturn sMinorNames />
</cfif>
<!--- Insure Names are in Alphabetical Order --->
<cfquery name= "qMinorNames"
dbtype= "query">
SELECT
qMinorNames.[First] ,
qMinorNames.[Last]
FROM qMinorNames
ORDER BY qMinorNames.[Last] , qMinorNames.[First]
</cfquery>
<cfset sPreviousLastName= TRIM( qMinorNames.Last ) />
<cfloop query= "qMinorNames">
<!--- Add Last Name to Group --->
<cfif Compare( sPreviousLastName , TRIM( qMinorNames.Last ) )>
<cfset sMinorNames&= " " & sPreviousLastName />
</cfif>
<!--- Everything Except The Last Entry --->
<cfif qMinorNames.CurrentRow LT qMinorNames.RecordCount>
<cfset sMinorNames= ListAppend( sMinorNames , qMinorNames.First ) />
</cfif>
<!--- Store Last Name --->
<cfset sPreviousLastName= TRIM( qMinorNames.Last ) />
</cfloop>
<!--- Add Last Entry --->
<cfset sMinorNames&= " and #qMinorNames.First[ qMinorNames.RecordCount ]# #sPreviousLastName#" />
<!--- Add Spaces After Commas --->
<cfset sMinorNames= sMinorNames.ReplaceAll( "," , ", " ) />
<cfreturn sMinorNames />
</cffunction>
<cfset minornames= minorNamesToString( MinorList ) />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment