Invalid list index 2 - coldfusion

I' am getting Invalid list index 2 which means that in the index 2 is not defined. How can check if index 2 is defined in ColdFusion?
<cfif
ListGetAt('/pages.cfm/about-us/' 1 , "/") eq 'news' AND
ListGetAt('/pages.cfm/about-us/', 2 , "/") eq 'press' AND
ListGetAt('/pages.cfm/about-us/', 3 , "/") eq '2016'AND
ListGetAt('/pages.cfm/about-us/', 4 , "/") neq ''>
<cfoutput>It Works!</cfoutput>
</cfif>

If the goal is to verify the text starts with a particular pattern, ie "/news/press/2016/{moreChars}/", it would be simpler to use a regular expression:
<cfif REFindNoCase("^/news/press/2016/[^/]+/", theStringToSearch)>
found
</cfif>

It is working now.
<cfif
ListLen('/pages.cfm/about-us/', "/") neq '' AND
ListGetAt('/pages.cfm/about-us/', 1 , "/") eq 'news' AND
ListLen('/pages.cfm/about-us/', "/") neq '' AND
ListGetAt('/pages.cfm/about-us/', 2 , "/") eq 'press' AND
ListLen('/pages.cfm/about-us/', "/") neq '' AND
ListGetAt('/pages.cfm/about-us/', 3 , "/") eq '2016'AND
ListLen('/pages.cfm/about-us/', "/") neq '' AND
ListGetAt('/pages.cfm/about-us/', 4 , "/") neq ''>
<cfoutput>It Works!</cfoutput>
</cfif>

Related

making the listcolumns search dynamic

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>

How would I skip the specific time slot in ColdFusion

I am creating a reservation system for the cars with instructors and its basically working correctly. I want to implement the rule where I can say want to block some additional sessions.
For example the instructor just finish session from 6:00Am to 8AM. the next available slot should be 8AM if they decide back to back scheduling and 8:15AM (if they want a break). The other sessions withing 105 minutes span should be hidden so the next available session will at either 10AM
<cfloop index="ii" from="#startHour#" to="#endHour#">
<cfloop index="jj" from="0" to="#60-locbtwMinute#" step="#locbtwMinute#">
<cfset showtime="y">
<cfset time = createtime(ii, jj, 0)>
<cfloop query="qAssignedSessions">
<cfset preSessionStart = dateadd("n",-locBtwSpan-buffertime,sessionstart)>
<cfset postSessionend = dateadd("n",bufferTime,sessionend)>
<cfif (hour(time) gt hour(preSessionStart) or (hour(time) eq hour(preSessionStart) and minute(time) gt minute(preSessionStart)))
and (hour(time) lt hour(postSessionend) or (hour(time) eq hour(postSessionend) and minute(time) lt minute(postSessionend)))>
<cfset showtime="n">
</cfif>
</cfloop>
<cfif ((datecompare(arguments.startDt,latestSessionDate) eq 1 and ((hour(time) eq 20 and minute(time) eq 00) or hour(time) lt 20))
or (datecompare(arguments.startDt,latestSessionDate) eq 0 and ((hour(time) eq 20 and minute(time) eq 00) or (hour(time) lt 20 and hour(time) gt hour(now()))))
)
and showtime eq "y">
<option value="<cfoutput>#timeformat(time, 'HH:mm')#</cfoutput>"><cfoutput>#timeformat(time, "hh:mm tt")#</cfoutput></option>
</cfif>
</cfloop>
</cfloop>
I figure out I just create 2 array one for time slots that are available and second for the one that are already taken and eliminate the one I do not need

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>

Trouble with If Statements to fully loop and not fully loop

What I am trying to do is if pcount is = 0
<cfif (isDefined("session.checkout.quantity.pcount")) eq 0>
then loop this but skip the last loop
<cfif BAdd NEQ session.checkout.quantity.bcount>
<cfinclude template="../../../ddl/bandor.cfm">
and if pcount is not equal to 0
(zero is the number 0 in a drop down menu not just blank)
<cfif (isDefined("session.checkout.quantity.pcount")) neq 0>
then loop this everytime
<cfinclude template="../../../ddl/bandor.cfm">
This is the full code if anyone can please tell me what I am doing wrong?
<cfif (isDefined("session.checkout.quantity.pcount")) eq 0>
<cfif BAdd NEQ session.checkout.quantity.bcount>
<cfinclude template="../../../ddl/bandor.cfm">
</cfif>
</cfif>
<cfif (isDefined("session.checkout.quantity.pcount")) neq 0>
<cfinclude template="../../../ddl/bandor.cfm">
</cfif>
This is a formatted comment to help you understand what went wrong with your code. This,
<cfif (isDefined("session.checkout.quantity.pcount")) eq 0>
does not check the value of pcount. It actually means,
if variable session.checkout.quantity.pcount does not exist
That's because function isDefined returns a boolean, true or false. However, the way ColdFusion works, the number 0, as well as the strings, "false", "no", and "0" are interpeted as false.
<cfif structKeyExists(session.checkout.quantity, "pcount") AND session.checkout.quantity.pcount eq 0>
<cfif BAdd NEQ session.checkout.quantity.bcount>
<cfinclude template="../../../ddl/bandor.cfm">
</cfif>
<cfelse>
<cfinclude template="../../../ddl/bandor.cfm">
</cfif>

How to check value in an Array using Coldfusion?

I have the below code:
<cfset abcList = "*,B,b,A,C,a">
<cfset abcList=ListToArray(abcList,',')>
When I output 'abcList' then it is giving me a value but when I use the 'abcList' in <cfif> it's not working. Here is the code which is creating the problem:
<cfoutput>
#abcList[1]# <!---This is giving '*' as Correct o/p--->
<cfif #abcList[1]# eq '*'> <!---Here its going in else--->
list has * at first place
<cfelse>
* is not first
</cfif>
</cfoutput>
Any suggestions on what's wrong in my code?
You don't necessarily need to convert the list to an array. If you are starting from a list variable, you may use Coldfusion list functions to do the same thing without specifying the array conversion.
<cfset abcList = "*,B,b,A,C,a">
<cfif Compare(listGetAt(abcList, 1), '*') EQ 0>
Match
<cfelse>
No Match
</cfif>
Note that most of Coldfusion's string comparisons are not case sensitive. So if you need to test that 'B' is not the same as 'b', you will need to use the compare() function or else use one of the Regular Expression string functions. In this case, compare() returns 0 if string 1 equals string 2. If you do not need case sensitivity, then you may simplify further:
<cfset abcList = "*,B,b,A,C,a">
<cfif listGetAt(abcList, 1) EQ '*'>
Match
<cfelse>
No Match
</cfif>
It also works fine for me. Perhaps you have some extra spaces in the list values? That would skew the results:
<cfset abcList = "#chr(32)#*,B,b,A,C,a">
<cfset abcList=ListToArray(abcList,',')>
<cfoutput>
The value of abcList[1] = #abcList[1]# <br/>
<cfif abcList[1] eq '*'>
list has * at first place
<cfelse>
The else condition was hit because abcList[1] is "(space)*" and not just "*"
</cfif>
</cfoutput>
Try trimming the value first. Also, the # signs around the value are not needed.
<cfif trim(abcList[1]) eq '*'>
....
</cfif>
If that does not work, display the ascii values of both characters. Perhaps they are different than you are thinking.
<cfoutput>
ASCII abcList[1] = #asc(abcList[1])# <br/>
ASCII "*" = #asc("*")# <br/>
</cfoutput>
<cfset abcList = "*,B,b,A,C,a">
<cfset abc=ListToArray(abcList)>
<cfif #abc[1]# eq "*">OK<cfelse>FAIL</cfif>
<cfif abc[1] eq "*">OK<cfelse>FAIL</cfif>
Prints "OK OK" for me. Can you re-confirm it prints something else for you?