ColdFusion: Get variable string value from cfloop - coldfusion

The code below outputs weekend dates in the current month.
CODE:
<cfparam name="month" default="#DatePart('m', Now())#">
<cfparam name="year" default="#DatePart('yyyy', Now())#">
<cfset ThisMonthYear=CreateDate(year, month, '1')>
<cfset Days=DaysInMonth(ThisMonthYear)>
<cfset ThisDay = 1>
<cfloop condition="ThisDay LTE Days">
<cfset presentDay = CreateDate(year, month, thisday)>
<cfif DayOfWeek(presentDay) EQ '7'>
<cfoutput>#ThisDay#</cfoutput>
<cfelseif DayOfWeek(presentDay) EQ '1'>
<cfoutput>#ThisDay#</cfoutput>
</cfif>
<cfset ThisDay = ThisDay + 1>
</cfloop>
OUTPUT:
6 7 13 14 20 21 27 28
What I'm trying is to pass value of this cfloop in one variable. The code below only displays the last weekend date value.
CODE:
<cfset ThisDay = 1>
<cfset weekDayOfMonth = "">
<cfloop condition="ThisDay LTE Days">
<cfset presentDay = CreateDate(year, month, thisday)>
<cfif DayOfWeek(presentDay) EQ '7'>
<cfset weekDayOfMonth = ThisDay>
<cfelseif DayOfWeek(presentDay) EQ '1'>
<cfset weekDayOfMonth = ThisDay>
</cfif>
<cfset ThisDay = ThisDay + 1>
</cfloop>
<cfoutput>#weekDayOfMonth#</cfoutput>
OUTPUT
28
Question, what do I need fix in my last cfloop code so I can pass loop values into the jsWeekendDates variable?
Any help will be greatly appreciated.
Thank you.

Just figured on my own. Enjoy.
<cfset ThisDay = 1>
<cfset weekDay = "">
<cfloop condition='ThisDay LTE Days'>
<cfset presentDay = CreateDate(year, month, thisday)>
<cfif DayOfWeek(presentDay) EQ '1' OR DayOfWeek(presentDay) EQ '7'>
<cfset weekDay = weekDay & " " & ThisDay">
</cfif>
<cfset ThisDay = ThisDay + 1>
</cfloop>
<cfoutput>#weekDay#</cfoutput>

Related

coldfusion 11 loop in rightful way

i see this code in one of the pages, seems annoying, it can be shorted, if i am on Coldfusion 11
https://trycf.com/gist/701515844d5e41549a7b6e61dfafeaa0/lucee5?theme=monokai
Here is the code how its looks like
<cfloop list="#mqry.columnList#" index="col">
<cfset ColRow ="#mqry.columnList#," >
</cfloop>
<cfset x = replace(ColRow,"CONDITION1","CONDITION") & chr(13) & chr(10)>
<cfloop query="mqry">
<cfloop list="#mqry.columnList#" index="colN">
<cfset f = Replace("#mqry[colN][currentRow]#" ,"""","","All")>
<cfset f = REReplaceNoCase("#mqry[colN][currentRow]#" , "<[^>]*(?:>|$)", "", "ALL")>
<cfset x = x & """#_f#"",">
</cfloop>
<cfset x = x & chr(13) & chr(10)>
</cfloop>
the first value i think i can do as:
mquery.columnlist - no need to loop over
There's nothing wrong with the nested loop. You iterate over all rows and then iterate over each cell per row using the column name. I rewrote your code to be easier understandable:
<cfset NEWLINE = (chr(13) & chr(10))>
<cfset DELIM = ",">
<cfset QUOTE = '"'>
<!--- column headers --->
<cfset x = mqry.columnList>
<!--- replace column names as desired --->
<cfset x = replace(x, "CONDITION1", "CONDITION")>
<!--- next line --->
<cfset x &= NEWLINE>
<cfloop query="mqry">
<cfset row = []>
<cfloop list="#mqry.columnList#" index="colN">
<cfset cellValue = mqry[colN][mqry.currentRow]>
<!--- remove all quotes --->
<cfset cellValue = replace(cellValue, QUOTE, "", "ALL")>
<!--- remove tags --->
<cfset cellValue = reReplaceNoCase(cellValue, "<[^>]*(?:>|$)", "", "ALL")>
<!--- prefix with underscore --->
<cfset cellValue = ("_" & cellValue)>
<!--- wrap in quotes --->
<cfset cellValue = (QUOTE & cellValue & QUOTE)>
<cfset row.add(cellValue)>
</cfloop>
<!--- combine cell values into a row --->
<cfset x &= arrayToList(row, DELIM)>
<!--- next line --->
<cfset x &= NEWLINE>
</cfloop>
<cfoutput>#x#</cfoutput>

using List to delete elements from middle, first position and last position in sequence

trying to handle the list deletion but stuck on ts deletion process: here is my small gist which i am working on, any help appreciated
https://trycf.com/gist/aa3871e76db36ad446b770b9bbbb4cec/lucee5?theme=monokai
<cfset lstFirst = "1,2,3,4,5">
<cfset lstMiddle = "6,7,8,9,10">
<cfset lstLast = "11,12,13,14,15">
From the lstFirst I should be able to delete from reverse like 5,4,3,2,1 but i should not be able to
delete from middle of the very next item which is 2
From the lstMiddle if the middle one is 8, i should be able to delete 7,6 and so on or 9,10 but i should not be able to
delete 6,10 without deleting 7 and 9
From the lstLast which is complete reverse of the lstFirst where the last element is selected and i should be able to delete
from 14,13,12 onward but cannot delete from middle or from 11 onward, it must start its deletion from 14 or 15 backwards this is the way i am trying to make it work
**<cfoutput>
<cfset lstFirst = "1,2,3,4,5">
<cfset lstMiddle = "6,7,8,9,10">
<cfset lstLast = "11,12,13,14,15">
<!--- It's work all dynamic values for lstFirst like 1,2,3,4,5 OR 1,2,3,4,5,6,7,8,9,10 OR 1,2,3,4,5,6,7,8,9,10,11,12 etc.... --->
======= Delete all number from reverse and except very next item to middle ====
<cfset convertToArray = listtoarray(lstFirst)>
<cfset getRoundValue =round(arraylen(convertToArray)/2)>
<cfset findMidNextEle = ArraySlice(convertToArray,1,getRoundValue)>
<cfset LfirstMiddle = listgetat(lstFirst,listfind(lstFirst,listlast(arraytolist(findMidNextEle)))-1)>
<cfloop from="#listlen(lstFirst)#" to="1" index="i" step="-1">
<cfset listgetVal = listgetat(lstFirst,i)>
<cfif LfirstMiddle NEQ listgetVal >
<cfset lstFirst =listdeleteat(lstFirst,i)>
</cfif>
<br/><cfdump var="#lstFirst#" /><br/>
</cfloop>
=========== Delete 7,6 and so on or 9,10 and delete 6,10 without deleting 7 and 9 ================================= <br/>
<cfset lstMiddleMiddle = listfind(lstMiddle,listgetat(lstMiddle,listfind(lstMiddle,listlast(mid(lstMiddle,1,listlen(lstMiddle)))))) >
<cfloop from="1" to="#listlen(lstMiddle)#" index="j">
<cfset userList = listlen(lstMiddle)>
<cfif userList GT 3 >
<cfset lstMiddle1 = listdeleteat(lstMiddle,lstMiddleMiddle-1)>
<cfset lstMiddle2 = listdeleteat(lstMiddle1,lstMiddleMiddle)>
<cfset lstMiddleMiddle = listfind(lstMiddle2,listgetat(lstMiddle2,listfind(lstMiddle2,listlast(mid(lstMiddle2,1,listlen(lstMiddle2))))))>
<cfset lstMiddle = lstMiddle2>
<br/><cfdump var="#lstMiddle2#" /></br>
<cfelse>
<cfset lstMiddle3 = listdeleteat(lstMiddle2,1)>
<cfset lstMiddle4 = listdeleteat(lstMiddle3,2)>
<cfbreak/>
</cfif>
</cfloop>
enter code here
<cfdump var="#lstMiddle4#" /> <br/>
=============== Delete from reverse except last number ==========
<!--- It's will work for all dynamic values --->
<cfset LastEle = listlast(lstLast)>
<cfloop from="#listlen(lstLast)#" to="1" step="-1" index="k">
<cfset getCurrentValue = listgetat(lstLast,k)>
<cfif LastEle NEQ getCurrentValue>
<cfset lstLast =listdeleteat(lstLast,k)>
</cfif>
</br><cfdump var="#lstLast#" /><br/>
</cfloop>
</cfoutput>**

How to output the values of a query depending on the value of row?

If I have a query result like this
type_name | count
-----------------
test1 | 5
test2 | 4
test4 | 7
How to output the count base on the value of the column type_name?
From the table the row value 'test3' doesn't exist, but it doesn't mean it wont exist later after a refresh.
With the below code I will only get the value but looping 3 times since test3 value doesn't exist.
<cfoutput name="query">
<table>
<tr><td><cfif query.type_name eq 'test1'>#query.count#</cfif></td></tr>
<tr><td><cfif query.type_name eq 'test2'>#query.count#</cfif></td></tr>
<tr><td><cfif query.type_name eq 'test3'>#query.count#</cfif></td></tr>
<tr><td><cfif query.type_name eq 'test4'>#query.count#</cfif></td></tr>
</cfoutput>
<cfset testResults = [0,0,0,0]>
<cfloop query="query">
<cfset testResults[right(query.type_name, 1)] = query.count>
</cfloop>
<table>
<cfloop array="#testResults#" index="count">
<td>#count#</td>
</cfloop>
</table>
You should see
5
4
0
7
If you need a more dynamic approach, you can transform your full query result (all columns) to an array of structs:
<!---
<!--- test data --->
<cfset query = queryNew("type_name,count", "VARCHAR,INTEGER")>
<cfset queryAddRow(query, 3)>
<cfset querySetCell(query, "type_name", "test1", 1)>
<cfset querySetCell(query, "count", "5", 1)>
<cfset querySetCell(query, "type_name", "test2", 2)>
<cfset querySetCell(query, "count", "4", 2)>
<cfset querySetCell(query, "type_name", "test4", 3)>
<cfset querySetCell(query, "count", "7", 3)>
--->
<!--- number of tests to list --->
<cfset expectedNumberOfRows = 4>
<!--- remember all columns while transforming query to array of structs --->
<cfset queryColumns = listToArray(query.columnList)>
<!--- initialize array of structs --->
<cfset testResults = arrayNew(1)>
<cfloop from="1" to="#expectedNumberOfRows#" index="i">
<cfset blankResult = structNew()>
<!--- initialize all columns for each row --->
<cfloop array="#queryColumns#" index="columnName">
<cfset blankResult[columnName] = "">
</cfloop>
<!--- default values for specific columns --->
<cfset blankResult["type_name"] = "test#i#">
<cfset blankResult["count"] = "0">
<cfset testResults.add(blankResult)>
</cfloop>
<!--- transfer cell values from each query row to array of structs --->
<cfset queryRowIndex = 1>
<cfloop query="query">
<!--- extract possible row index --->
<cfset testNumber = trim( reReplace(query.type_name, "[^0-9]*([0-9]+)$", "\1") )>
<!--- transfer cells --->
<cfif reFind("^[0-9]+$", testNumber)>
<cfloop array="#queryColumns#" index="columnName">
<cfset testResults[int(testNumber)][columnName] = query[columnName][queryRowIndex]>
</cfloop>
</cfif>
<cfset queryRowIndex++>
</cfloop>
<cfoutput>
<table>
<cfloop array="#testResults#" index="testResult">
<tr>
<td>#testResult.type_name#</td>
<td>#testResult.count#</td>
<!--- add additional columns if desired --->
</tr>
</cfloop>
</table>
</cfoutput>

How to Add a Calculated Value to Export to Excel Function

I have a block of code that sets an "Aging" value for an invoice, as part of an export to excel script. I have all the other options in the export function working, except the "Aging" value.
Here is the SQL code for how "Aging" works:
<cfif Aging neq "">
<cfswitch expression="#Aging#">
<cfcase value=29>
AND I.DueDate BETWEEN dateadd(day, datediff(day, 0 ,getdate())-29, 0) AND dateadd(day, datediff(day, 0 ,getdate()), 0)
AND I.OutstandingAmount > 0
</cfcase>
<cfcase value=59>
AND I.DueDate BETWEEN dateadd(day, datediff(day, 0 ,getdate())-59, 0) AND dateadd(day, datediff(day, 0 ,getdate())-30, 0)
AND I.OutstandingAmount > 0
</cfcase>
<cfcase value=89>
AND I.DueDate BETWEEN dateadd(day, datediff(day, 0 ,getdate())-89, 0) AND dateadd(day, datediff(day, 0 ,getdate())-60, 0)
AND I.OutstandingAmount > 0
</cfcase>
<cfcase value=90>
AND I.DueDate < dateadd(day, datediff(day, 0 ,getdate())-90, 0)
AND I.OutstandingAmount > 0
</cfcase>
</cfswitch>
</cfif>
I'm new to ColdFusion and I just cannot figure out how to write the code for the Excel part. Here is the export function, with line breaks added for readability only.
<!--- ------------------------------------------ --->
<cfelseif ExType eq "xls5">
<!--- ------------------------------------------ --->
<cfset tabchar = Chr(9)>
<cfset NewLine = Chr(13) & Chr(10)>
<cfcontent type="text/msexcel">
<cfheader name="Content-Disposition" value="Attachment; filename=Late/Aging.xls">
<cfoutput>DATE INVOICED#TabChar#INVOICE#TabChar#CLIENT#TabChar#TOTAL#TabChar#AMOUNT DUE#TabChar#AGE#NewLine#</cfoutput>
<cfoutput query="viewinvoices">
<cfif curr.currencytype eq "Euro">
<cfset TotalC = #LSEuroCurrencyFormat(TotalCost, "local")#>
<cfelse>
<CFSET oldlocale = SetLocale(#curr.currencytype#)>
<cfset TotalC = #LSCurrencyFormat(TotalCost, "local")#>
</cfif>
<cfif curr.currencytype eq "Euro">
<cfset OutAmt = #LSEuroCurrencyFormat(OutstandingAmount, "local")#>
<cfelse>
<CFSET oldlocale = SetLocale(#curr.currencytype#)>
<cfset OutAmt = #LSCurrencyFormat(OutstandingAmount, "local")#>
</cfif>
#DateFormat(InvoiceDate, 'mm/dd/yyyy')##TabChar#
<cfif curr.CustomInvoices eq "0">
#Invoice#
<cfelse>
#NumberFormat(InvoiceCustom, '0000')#
</cfif>
#TabChar##Client##TabChar##TotalC##TabChar##OutAmt##TabChar##Aging##Newline#
</cfoutput>
I have this in my export statement but does not seem to work:
<cfif Aging eq "29">
<cfset Aging = 'Year(s)'>
<cfelseif Aging eq "59">
<cfset Aging = 'Day(s)'>
<cfelseif Aging eq "89">
<cfset Aging = 'Month(s)'>
<cfelseif Aging eq "90">
<cfset Aging = 'Quarter(s)'>
<cfelse>
<cfset Aging = 'n/a'>
</cfif>
The text years, etc is just for testing.

How to change DayOfWeek() to start from own day

I want to change the DayOfWeek() function so that I can get dates according to my own desired first day of the week. So what I am doing in this code is that I am setting a startDate, endDate and selectDays that I want to check. Here is my code:
<cfscript>
function Mydayofweek(date, day_1)
{
return (((DayOfWeek(date) + (7 -day_1)) MOD 7) +1);
}
</cfscript>
<cfset startDate = '07/01/2013'>
<cfset endDate = '07/25/2013'>
<Cfset mydates = ''>
<cfset selectDays = '2,6'>
<cfset MyWeekFirstDay = 6> <!---I selected Friday = 6 --->
<cfset new = ''>
<cfoutput>
<cfloop list="#selectDays#" delimiters="," index="d">
<cfset new &= '#Mydayofweek(d, MyWeekFirstDay)#,' >
</cfloop>
<cfif new NEQ ''>
<cfset ScheduleDate = left(new, (len(new)-1) )>
</cfif>
<cfdump var="#ScheduleDate#"><br />
</cfoutput>
<cfset AppendToMyDates = false>
<cfloop from="#startDate#" to="#endDate#" index="day">
<cfif AppendToMyDates is false and DayOfWeek(day) is ListFirst(selectDays)>
<cfset AppendToMyDates = true>
</cfif>
<cfif listfind(ScheduleDate, DayOfWeek(day), ',') NEQ 0 and AppendToMyDates is true>
<cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">
</cfif>
</cfloop><cfoutput>#mydates#</cfoutput>
This is written in ColdFusion. That code generates this output:
4,1
Jul, 03, 2013 Wednesday,
Jul, 07, 2013 Sunday,
Jul, 10, 2013 Wednesday,
Jul, 14, 2013 Sunday,
Jul, 17, 2013 Wednesday,
Jul, 21, 2013 Sunday,
Jul, 24, 2013 Wednesday,
The Output Should be like this because i select Friday = 1 to Thursday = 7 so the above days selectDays = '2,6' should now point to selectDays = '7,4' with respect to my first days 2,6
7,4
Jul, 06, 2013 Saturday,
Jul, 10, 2013 Wednesday,
Jul, 13, 2013 Saturday,
Jul, 17, 2013 Wednesday,
Jul, 20, 2013 Saturday,
Jul, 24, 2013 Wednesday,
I have set my selectDays = '2,6' it means I want to get dates of Saturday and Wednesday because I have set 6 as my week first day and it start from Friday (by default it was sunday). My days start from Sunday Sunday = 1 , Monday = 2 , Tuesday = 3 , Wednesday = 4 , Thursday = 5 , Friday = 6 , Saturday = 7 and now I changed my date start from Friday = 1 , Saturday = 2 ,Sunday = 3 , Monday = 4 , Tuesday = 5 , Wednesday = 6 , Thursday = 7 in script function. Actually I think there is error in my script function that i don't understand. Please help me out to find the problem and it's solution thanks
UPDATED
You just need to call your script into your loop. No need to make any function just change the code like this one.
Copy this code hope this will solve your problem.
<cfscript>
function Mydayofweek(date, day_1)
{
return (((DayOfWeek(date) + (7 -day_1)) MOD 7) +1);
}
</cfscript>
<cfset startDate = '07/01/2013'>
<cfset endDate = '07/25/2013'>
<Cfset mydates = ''>
<cfset selectDays = '2,6'>
<cfset MyWeekFirstDay = 6><!---I selected Friday = 6 --->
<cfloop from="#startDate#" to="#endDate#" index="day">
<cfif listfind(selectDays, Mydayofweek(day,MyWeekFirstDay), ',') NEQ 0 >
<cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">
</cfif>
</cfloop>
<cfoutput>#mydates#</cfoutput>
With the new information that selectDays might not always be the same, I'd do something like this:
<cfset AppendToMyDates = false>
<cfloop from="#startDate#" to="#endDate#" index="day">
<cfif AppendToMyDates is false and DayOfWeek(day) is ListFirst(SelectDays)>
<cfset AppendToMyDates = true>
</cfif>
<cfif listfind(selectDays, DayOfWeek(day), ',') NEQ 0 and AppendToMyDates is true>
<cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">
</cfif>
</cfloop>
Edit starts here
If you want the start of the week to be a variable, you want to write your own version of DayOfWeek() with a different name. The structure would be something like this:
<cffunction name="DayOfWeekModified returntype="numeric">
<cfargument name="WeekStartsOn" type="numeric" required="yes">
<cfscript>
var DayNumber = 0;
code to generate it based on arguments.WeekStartsOn
return DayNumber;
<cfscript>
<cffunction>
You then call this function instead of DayOfWeek() in your loop.
The issue you are having is with your cfif condition. You are looping over the dates and then checking if the given date is a Wednesday or Friday and the results you are getting are correct. Since you want to start with a Friday (ignoring the first Wednesday) you need to add that to your code. This may work for you:
<cfset startDate = '06/11/2013'>
<cfset endDate = '06/25/2013'>
<cfset mydates = ''>
<cfset selectDays = '6,4'>
<cfloop from="#startDate#" to="#endDate#" index="day">
<cfif listfind(selectDays, DayOfWeek(day), ',') NEQ 0>
<cfif mydates NEQ "" OR DayOfWeek(day) EQ "6">
<cfset mydates &= "#dateformat(day, 'mmm, dd, yyyy dddd')#,<br />">
</cfif>
</cfif>
</cfloop>
<cfoutput>#mydates#</cfoutput>
I added an additional cfif condition around the setting of mydates. This code mydates NEQ "" is checking to see if mydates is not empty, meaning we have already satisified the next condition. If mydates is empty then also check to see if the given date is a Friday with DayOfWeek(day) EQ "6". This should guarantee that the first date entered in mydates is a Friday.
Not sure how I feel about this code but it seemed to work for me with your example.