making the listcolumns search dynamic - coldfusion

I am following upn on a tute on datatables and found a code which basically iterates over the columns for a order clause but in the case, i do not have a list of order columns hardcoded, its a dynamic list
how can I change this
<cfif form["order[0][column]"] gt 0>
ORDER BY
<cfif form["order[0][column]"] eq '1'>
empname <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>
<cfif form["order[0][column]"] eq '2'>
empno <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>
<cfif form["order[0][column]"] eq '3'>
ic <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>
</cfif>
to be dynamic as my list variable is a comma separated as id,name,email and lots more

The direction I always would go will be to create a Coldfusion array or structure with this fieldNames, like the following.
<cfset orderColumnReference = ['empname', 'empno', 'ic', 'id', 'name', 'email', ...]>
<cfif form["order[0][column]"] GTE ArrayLen(orderColumnReference)>
<cfset orderByColumn = orderColumnReference[form["order[0][column]"]]>
ORDER BY
#orderByColumn# <cfif form["order[0][dir]"] eq 'desc'>desc</cfif>
</cfif>

Related

coldfusion if - how to look for several items in an IF

I have an if structure as follows:
<cfif #user.personnel_no# is 'xxxxx' or #user.personnel_no# is 'xxxxx' or #user.personnel_no# is 'xxxxx'>
---data
</cfif>
how can I do something like:
<cfif #user.personnel_no# in ('xxxxx','yyyyy','zzzzz')>
---data
</cfif>
to look among all values in if?
or declaring a list and do something like
list = 'xxxxx','yyyyy','zzzzz'
<cfif #user.personnel_no# in list>
---data
</cfif>
Thank you.
<cfscript>
user.personnel_no = 'yyyyy'
asArray = ['xxxxx','yyyyy','zzzzz']
writeOutput(asArray.find(user.personnel_no)) // 2
asList = 'xxxxx,yyyyy,zzzzz'
writeOutput(asList.listfind(user.personnel_no)) // 2
</cfscript>
https://trycf.com/gist/f737ef6d010d4ce37936f1d53d021a62/lucee5?theme=monokai
https://cfdocs.org/listfind
https://cfdocs.org/arrayfind

Adding AND statement to ColdFusion query

I am trying to add AND ActiveFlag = 'Y' (value in same table as CommitteeRoleCode), but all my attempts either brake it or are ignored. Would anyone be kind enough to assist?
Thanks for your time!
<cfparam name="attributes.Address" default="">
<cfparam name="attributes.CommitteeID" default="0">
<cfparam name="attributes.AddressListName" default="">
<cfif Len(Trim(attributes.CommitteeID)) EQ 0>
<cfset attributes.CommitteeID = 0>
</cfif>
<cfif Len(Trim(attributes.Address))>
<cfset result = "">
<cfloop list="#attributes.Address#" index="i">
<cfif Refind("\[{1}.+\]{1}", i)>
<!---looking for the 'ALL' member type to grab all categories--->
<cfif i EQ "[ALL]">
<cfset l_where = "a.CommitteeRoleCode IS NOT NULL AND c.EmailAddress IS NOT NULL">
<cfelse>
<cfset l_where = "a.CommitteeRoleCode=#Chr(39)##ReReplace(i, "\[|\]", "", "ALL")##Chr(39)# AND c.EmailAddress IS NOT NULL">
</cfif>
<cfquery datasource="#application.datasource#" name="MemberTypeEmails">
Select c.EmailAddress
from (Committee_Role_Ref as a Inner Join Committee_Member as b on a.CommitteeRoleCode=b.CommitteeRoleCode)
Inner Join Contact as c on b.ContactID=c.ContactID
Where #ReReplace(l_where, "\'\'", "'", "ALL")#
AND b.CommitteeID=#attributes.CommitteeID#
</cfquery>
<cfloop query="MemberTypeEmails">
<cfif ReFind("[^#chr(13)##chr(10)##chr(9)##chr(32)#]#{1}.+\..+", EmailAddress)>
<cfif Len(Trim(result))>
<cfset result = result & "," & EmailAddress>
<cfelse>
<cfset result = EmailAddress>
</cfif>
</cfif>
</cfloop>
<cfelseif ReFind(".+#{1}.+\..+", i)>
<cfif Len(Trim(result))>
<cfset result = result & "," & i>
<cfelse>
<cfset result = i>
</cfif>
</cfif>
</cfloop>
<cfset "caller.#attributes.AddressListName#" = result>
</cfif>
You say you are trying to add AND ActiveFlag = 'Y' (value in same table as CommitteeRoleCode). However, the value CommitteeRoleCode appears in two tables and I know this because of this part of the query
a.CommitteeRoleCode = b.CommitteeRoleCode
My suggestion would be to find out which table the ActiveFlag is really in and then prefix it accordingly in the query with either the a. or b. ie
AND a.ActiveFlag = 'Y'
or
AND b.ActiveFlag = 'Y'
Then place your AND clause where indicated below. But if you want more help you should also post what error message you are receiving.
<cfquery datasource="#application.datasource#" name="MemberTypeEmails">
Select c.EmailAddress
from (Committee_Role_Ref as a Inner Join Committee_Member as b on a.CommitteeRoleCode=b.CommitteeRoleCode)
Inner Join Contact as c on b.ContactID=c.ContactID
Where #ReReplace(l_where, "\'\'", "'", "ALL")#
AND b.CommitteeID=#attributes.CommitteeID#
-- AND a. or b. ActiveFlag goes here
AND a.ActiveFlag = 'Y'
</cfquery>

Split and Sort an Array by date, getting blank array places

I am just confusing myself with this one. I have a data log that is saved on the database as something like this:
Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge).
<!--- all other tracking info --->
I am trying to pull from this log only the discharges and sort/filter them by date. This log is saved in the database as one column varchar(MAX). I need to sort this data and only pull the discharge dates / reason using regex and then apply a filter. The end product should end up with me putting that array in a table with PATIENT | DISCHARGE DATE | DISCHARGE REASON My code right now is giving me some errors as it is leaving me with some empty array values. My question is that I need to remove those blank values and sort by the date, but I am unsure where to begin.
<cfquery name="getDischarge" datasource="#this.dsn#">
select PatientsName, LogData from Patient
</cfquery>
<cfoutput query="getDischarge" group="PatientsName">
<cfif LogData neq "">
<cfsavecontent variable="str">
#LogData#
</cfsavecontent>
<cfset possibilities = reMatch("Discharge Date?:(\d\d?/\d\d?/\d{4})\s*Reason for Discharge:\d* - ((?:(?!Log-Date:).)*)", str)>
<cfset dateArray = ArrayNew(2)>
<cfloop index="w"
array="#possibilities#">
<cfif w NEQ "">
<!--- create 1 dimensional temp array to hold DATE | REASON --->
<cfset tempArray = ArrayNew(1)>
<!--- trim our regex to have only the date & then DateFormat --->
<cfset theDate = #Mid(w, 16, 11)#>
<cfset formatDate = #dateformat('#thedate#','mm-dd-yyyy')#>
<!--- use our regex to find the reason for discharge --->
<cfset theReason = reMatch("Reason for Discharge:\d* - ((?:(?!Log-Date:).)*)", str)>
<!--- append our DATE | REASON to 1d temp array --->
<cfset ArrayAppend(tempArray, '#formatDate#')>
<cfset ArrayAppend(tempArray, '#theReason#')>
<!--- append our 1d array to our 2d array to output matching DATE | REASON --->
<cfset #ArrayAppend(dateArray, '#tempArray#')#>
</cfif>
</cfloop>
<cfdump var="#dateArray#">
</cfif> <!--- logdata neq "" --->
</cfoutput>
To me, logically, this should work and omit blank values, but this is what I get when I dump this data:
Looks like you are struggling with the Jakarta ORO regular expression engine that is used in ColdFusion. reMatch/reMatchNoCase are terrible when it comes to capturing. Java on the other hand offers the POSIX regular expression engine.
<cfset data = [
"Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge).
More stuff...",
"Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/16/2012
Reason for Discharge:lorem ipsum."
]>
<cfset result = queryNew("DISCHARGE_DATE,DISCHARGE_REASON", "VARCHAR,VARCHAR")>
<cfloop array="#data#" index="line">
<cfset dischargeDate = reMatchGroupNoCase("Discharge Date:([0-9]{2}/[0-9]{2}/[0-9]{4})", line)>
<cfset dischargeReason = reMatchGroupNoCase("Reason for Discharge:([^\n]*)", line)>
<cfset hasDate = (arrayLen(dischargeDate) eq 1)>
<cfset hasReason = (arrayLen(dischargeReason) eq 1)>
<cfif hasDate or hasReason>
<cfset rowIndex = queryAddRow(result, 1)>
<cfif hasDate and isDate(dischargeDate[1])>
<cfset querySetCell(result, "DISCHARGE_DATE", dateFormat(dischargeDate[1], "yyyy-mm-dd"), rowIndex)>
</cfif>
<cfif hasReason>
<cfset querySetCell(result, "DISCHARGE_REASON", dischargeReason[1], rowIndex)>
</cfif>
</cfif>
</cfloop>
<cfquery name="orderedResult" dbType="query">
SELECT
*
FROM
[result]
ORDER BY
[DISCHARGE_DATE] ASC
</cfquery>
<cfdump var="#orderedResult#">
And here is the function you need:
<cffunction name="reMatchGroupNoCase" access="public" output="false" returnType="array">
<cfargument name="regex" type="string" required="true">
<cfargument name="value" type="string" required="true">
<cfset LOCAL.result = []>
<cfset LOCAL.Pattern = createObject("java", "java.util.regex.Pattern")>
<cfset ARGUMENTS.regex = LOCAL.Pattern.compile(ARGUMENTS.regex, bitOr(LOCAL.Pattern["CASE_INSENSITIVE"], LOCAL.Pattern["UNICODE_CASE"]))>
<cfset LOCAL.buffer = ARGUMENTS.regex.matcher(toString(ARGUMENTS.value))>
<cfset LOCAL.length = LOCAL.buffer.groupCount()>
<cfloop condition="LOCAL.buffer.find()">
<cfloop from="1" to="#LOCAL.length#" index="LOCAL.i">
<cfset LOCAL.value = LOCAL.buffer.group(
javaCast("int", LOCAL.i)
)>
<cfif isNull(LOCAL.value)>
<cfcontinue>
</cfif>
<cfset LOCAL.result.add(LOCAL.value)>
</cfloop>
</cfloop>
<cfreturn LOCAL.result>
</cffunction>
I recommend this approach:
with q1 as (select 'Log-Date: 10/26/2012. Record created. Admission Date: 08/01/2012
Log-Date: 06/20/2013 Discharged. Discharge Date:10/15/2012
Reason for Discharge:01 - (routine discharge). ' logData
)
select substring(logData, patindex('%Admission Date: %', logdata) + 16
, 10) admitDate
from q1
where logData like '%Discharge Date:%'
That returns 08/01/2012. You may have some complications for a variety of reasons, but the general idea should work.

Coldfusion 10 cfloop errors

I am getting an error after upgrade from coldfusionOX to coldfusion 10.
Error Occurred While Processing Request Complex object types cannot be
converted to simple values.
The expression has requested a variable or an intermediate expression
result as a simple value. However, the result cannot be converted to a
simple value. Simple values are strings, numbers, boolean values, and
date/time values. Queries, arrays, and COM objects are examples of
complex values. The most likely cause of the error is that you tried
to use a complex value as a simple one. For example, you tried to use
a query variable in a cfif tag.
It occurs at line " cfloop index="local.thisRight" list="#rights#" ". It seems like ColdFusion does not like the "rights" here.
Anyone can give me some help? Thanks so much.
<cfif local.profile.rights.profile.self is not "">
<cfquery name="local.getAffiliations" datasource="#Request.readerDSN#">
SELECT tblPersonsToAffiliations.affiliationID, tblPersonsToAffiliations.rights, tblAffiliations.affiliationType, tblPersonsToAffiliations.relationshipType
FROM tblPersonsToAffiliations INNER JOIN tblAffiliations
ON tblPersonsToAffiliations.affiliationID = tblAffiliations.affiliationID
WHERE tblPersonsToAffiliations.personID IN (#local.profile.rights.profile.self#)
AND (tblPersonsToAffiliations.archived IS NULL
OR tblPersonsToAffiliations.archived = '')
</cfquery>
<cfif local.getAffiliations.recordCount is not 0>
<cfloop query="local.getAffiliations">
<cfif local.getAffiliations.relationshipType is "interested">
<cfset local.thisRelationshipType = "provisionalMember">
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRelationshipType#")>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = ListAppend(local.profile.rights[affiliationType][local.thisRelationshipType], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][thisRelationshipType] = affiliationID>
</cfif>
<cfelse>
<cfset local.thisRelationshipType = "fullMember">
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRelationshipType#")>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = ListAppend(local.profile.rights[affiliationType][local.thisRelationshipType], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][local.thisRelationshipType] = affiliationID>
</cfif>
<cfif isNull(rights)>
<cfelse>
<cfloop index="local.thisRight" list="#rights#" >
<cfif IsDefined("local.profile.rights.#affiliationType#.#local.thisRight#")>
<cfset local.profile.rights[affiliationType][local.thisRight] = ListAppend(local.profile.rights[affiliationType][local.thisRight], affiliationID)>
<cfelse>
<cfset local.profile.rights[affiliationType][local.thisRight] = affiliationID>
</cfif>
</cfloop>
</cfif>
</cfif>
</cfloop>
</cfif>
</cfif>
A bit earlier in your code you do this:
<cfif local.getAffiliations.relationshipType is "interested">
I think you need the same query name prefix in front of "rights" that is used when evaluating "relationshipType".
Try this:
#local.getAffiliations.rights#
HTH!
I am betting it is failing on this line:
<cfloop index="local.thisRight" list="rights" >
You are attempting to use the string "rights" as a list. My first reaction would be that you need to make that:
<cfloop index="local.thisRight" list="#rights#" >

Conditional Cfloop code to place AND at first loop

Using the following query, I am trying to do a simple case here when the first record it encounters it should basically add "AND" and for the remaining conditions, I want to add OR
Here is my try
<cfif isDefined('age') and len(trim(age)) and age neq '-1'>
<cfset age = trim(htmlEditFormat(lcase(age)))>
<cfloop list="#age#" index="k">
or age between #ListFirst(k,'-')# and #ListLast(k,'-')#
</cfloop>
</cfif>
trying to make it work like this
and (age between 18 and 20
or age between 20 and 25
or age between 25 and 30)
I am not getting where I should add a condition to add parenthesis and the AND operator.
You could do something as similar as adding a false statement and then looping through everything else as needed
<cfif isDefined('age') and len(trim(age)) and age neq '-1'>
<cfset age = trim(htmlEditFormat(lcase(age)))>
AND (1 = 2 --always returns false
<cfloop list="#age#" index="k">
OR age between #ListFirst(k,'-')# and #ListLast(k,'-')#
</cfloop>
)
</cfif>
This is what you were trying to do
<cfif isDefined('age') and len(trim(age)) and age neq '-1'>
<cfset age = trim(htmlEditFormat(lcase(age)))>
AND (
<cfloop list="#age#" index="k">
<cfif listFirst(age) NEQ k> OR </cfif> --if it's not the first iteration, add the OR
age between #ListFirst(k,'-')# and #ListLast(k,'-')#
</cfloop>
)
</cfif>
An alternative that doesn't require an if block and would work for any type of loop:
<cfif isDefined('age') and len(trim(age)) and age neq '-1'>
<cfset age = trim(htmlEditFormat(lcase(age)))>
<cfset expressionSeparator = "">
AND (
<cfloop list="#age#" index="k">
#expressionSeparator#
age between #ListFirst(k,'-')# and #ListLast(k,'-')#
<cfset expressionSeparator = " or ">
</cfloop>
)
</cfif>