Check two arguments in ColdFusion? - if-statement

I have two different arguments and if they are holding specific values I shouldn't execute the block of the code inside of my statement. Here is example:
<cfarguments name="myArg1" type="string" required="yes">
<cfarguments name="myArg2" type="string" required="yes">
<cfif myArg1 NEQ 'MMT' OR myArg2 NEQ 'newMMT'>
Execute the code
</cfif>
Arguments that I have passed above are myArg1 = 'MMT' and myArg2 = 'newCCS'. My cfif was indicating to true and code inside of the cfif was executed. So if myArg1 is equal to 'MMT' or myArg2 equal to 'newMMT' code inside of the if should not be executed. If I put AND instead of OR in that case I would be looking when both arguments are equal to the required values and that's not what I want. If you hav eany idea how I can get this to work please let me know. Thank you.

You need to use an AND statement. Also, don't forget to scope your variables.
<cfif arguments.myArg1 NEQ 'MMT' AND arguments.myArg2 NEQ 'newMMT'>

Related

Using ColdFusion, how do I replicate a compare statement with listFind?

I'm setting up something to allow me to masquerade as another user in my system. But, I want to use listFind instead of a compare() method.
<cfif compare(session.userName, "userOne") EQ 0>
<cfset #session.userName# = "userThree">
</cfif>
In the above statement, I'm trying to conform it to a listFind, where if userOne is currently logged in, then set the session.userName to userThree. But, I'm having trouble.
What I have thus far...
<cfif #ListFind("userOne, UserTwo")#>
<cfset #session.userName# = "userThree">
</cfif>
You will need to provide two independent arguments to ListFind. At the moment you are providing one string. The list is also the first arguments and string search for is the second.
<cfif ListFind("userOne", session.userName)>
<cfset session.userName = "userThree">
</cfif>
On a side note, the hashes are only necessary for string interpolation. In other words, only when you want a variable/expression to be evaluated and inserted into a string.

ColdFusion Multi Check isDefined(session) and is not blank

I am trying to say if this isDefined and is not blank show this..
<cfif (isDefined("session.checkout.info.Certificate_2")) is not "">
I also tried:
<cfif (isDefined("session.checkout.info.Certificate_2")) neq "">
Will someone please tell me why this still shows when the string is empty?
Consider using structKeyExists (it has lower overhead than isDefined) but here you go:
<cfif
isDefined("session.checkout.info.Certificate_2")
AND session.checkout.info.Certificate_2 NEQ "">
If it exists it will then check to see if it's empty. If it doesn't exist, it won't check the value.

ColdFusion checking empty form fields

I am very new to ColdFusion and was curious if someone could tell me how to check to see if a form field is empty or not.
For example let's say we set it up like this:
<cfinput
type="text"
name="firstName"
id="firstName"
value="#form.firstName#"
>
How do I call this later to use it in another form? I tried many things but I am missing something somewhere.
<cfif (form.firstName) EQ 0>
You can check if the length of the field is 0, using trim would remove any leading or trailing spaces.
<cfif len(trim(form.firstName)) EQ 0>
I have always use a two fold check. IsDefined evaluates a string value to determine whether the variable named in it exists.
<CFIF NOT IsDefined("FORM.firstname") OR
FORM.firstname EQ "">
Reference: http://help.adobe.com/livedocs/coldfusion/8/htmldocs/help.html?content=functions_in-k_14.html
The most straightforward way is:
<cfif form.firstName IS "">
It simply checks to see if the specified form field is an empty string ("").
Another way of writing the same thing would be:
<cfif len(form.firstName) EQ 0>
This checks to see if the length of the form field value is 0 (empty string).
This second method can be shortened a little bit?
<cfif len(form.firstName)>
Assume that form.firstName is empty. This would then become . In boolean evaluation, 0 is false. Assuming the value was not empty, it would become . A non-zero number evaluates to true.
Some developers prefer checking for emptiness by checking comparing against an empty string. See len(x) better or x NEQ "" better in CFML?
<cfif trim(form.firstName) NEQ "">
<cfscript> is also an option
<cfscript>
if (trim(form.firstName) != "") {
...
Yoda conditions work too
<cfscript>
if ( "" != trim(form.firstName)) {

ColdFusion isDefined

I am trying to check to see if data exist in my form If data does not exist I want to assign it to O. How can I do this.
<cfif not isDefined("FORM.Age")>
cfset FORM.Age = "0"
<cfif>
Generally the best practice is considered to be to avoid isDefined. This is because isDefined will search all scopes until it finds a matching variable. So it's more efficient to use structKeyExists, eg:
<cfif NOT structKeyExists(form, "age")>
<cfset form.age = 0>
</cfif>
Also, another way to achieve this is to use cfparam, and specify 0 as the default:
<cfparam name="form.age" default="0">
You're almost there:
<cfif not isDefined("FORM.Age")>
<cfset Form.Age = 0>
</cfif>
Technically what you have is fine once you enclose the cfset in tags < and >. Assuming that omission is just a typo, could it be you are trying to use it with a text field?
Text fields always exist on submission. The value may be an empty string, but the field itself still exists, so IsDefined will always return true. If that is the case, you need to examine the field length or value instead. Then do something if it is empty according to your criteria. For example:
<!--- value is an empty string --->
<cfif NOT len(FORM.age)>
do something
</cfif>
... OR
<!--- value is an empty string or white space only --->
<cfif NOT len(trim(FORM.age))>
do something
</cfif>
... OR
<!--- convert non-numeric values to zero (0) --->
<cfset FORM.Age = val(FORM.Age)>
There are actually two things you want to to ensure. First, make sure this page was arrived at by submitting the proper form. Next, ensure you have a numeric value for the form.age variable. Here is an example of how you might want to code this:
<cfif StructKeyExists(form, "age") and cgi.http_referrer is what it should be>
<cfif IsNumeric(form.age) and form.age gt 0>
<cfset AgeSubmitted = int(form.age)>
<cfelse>
<cfset AgeSubmitted = 0>
</cfif>
...more code to process form
<cfelse>
...code for when page was not arrived at properly
</cfif>

Having some trouble understanding loops

I created this for some reason neither one of the queries are being updated
<cfloop index="i" from="1" to="#ArrayLen(location)#">
<cfif location[i] NEQ "" AND #locationID# EQ "" >
<cfquery Name="UpdateAddActivity" DATASOURCE="#DS#">
INSERT INTO tblProjectLocations
(
projectID,
locationID
)
VALUES
(
#ProjectName#,
#location[i]#
)
</cfquery>
</cfif>
<cfif location[i] EQ "" AND #locationID# NEQ "" >
<cfquery Name="UpdateAddActivity" DATASOURCE="#DS#">
DELETE FROM tblProjectLocations
WHERE locationID = #locationID# AND projectID = #ProjectName#
</cfquery>
</cfif>
</cfloop>
Am I looping correctly? It doesn't seem like to me that the accumulator is ever going to be updated but loops are done this way every place that I've looked.
Your cfloop tag is fine - you only need index/from/to attributes for a basic loop.
The index variable is incremented (and the loop re-processed) at the position of the closing tag. Or to put it another way, the body code is executed once for each index value between from and to (inclusive).
For information, you can change the default increment (of 1) by specifying the step attribute (though that obviously doesn't make sense for an array loop).
When your code isn't performing as expected, you can debug it with the dump tag:
<cfloop ... >
...
<cfdump var=#locationID# abort />
...
</cfloop>
The abort attribute will stop processing - the loop will not iterate and the current page content will be returned (it's shorthand for specifying cfabort tag separately.
You can use multiple dumps, and the label attribute to help identify which is which, but obviously if using abort attribute make sure only the last one has it.
As has been mentioned locationID isn't defined in the snippet you've provided, so may be the issue.
Sometimes spaces can cause issues - you may want to use the trim function to ensure you're dealing with empty strings (though blindly wrapping trim functions everywhere is ugly - always try if possible avoid introducing spaces).
Shortcut Array Looping
The from/to loop you've got there is only one type of cfloop - there are others.
Specifically, when you don't need the numeric index, there is a shorthand array loop:
<cfloop index="CurLocation" array=#Location# >
...
</cfloop>
Which is equivalent to:
<cfloop index="i" from=1 to=#ArrayLen(Location)# >
<cfset CurLocation = Location[i] />
...
</cfloop>
But without the unused i variable. (If you need the i variable, stick to from/to.)
Note that inside a function you should almost always write index="local.i" and index="local.CurLocation" to ensure the variables are appropriately scoped. This isn't unique to loops - it applies to any tags that create variables. You can also do <cfset var i = 0 /> prior to the loop to do the same thing.
Unrelated Issues
There are a couple of other issues with your code.
Most importantly, the code you're showing is potentially at risk of SQL injection. You should almost never write SQL with bare hashes in, and instead parameterise your queries - using cfqueryparam tag - to solve this. (In the situations where you can't use parameters (e.g. within ORDER BY) make sure you have appropriately sanitized any dynamic text.
Less important -- it doesn't change how the code works, but does betray a lack of experience and understanding -- are the superfluous hashes around locationID. A simplified explanation is that you generally only need #s inside strings (i.e. where the contents would otherwise be treated as text, rather than being a variable's value.)
When in doubt, look at your data.
<cfoutput>
<cfloop index="i" from="1" to="#ArrayLen(location)#">
i is #i# <br>
<cfif location[i] NEQ "" AND locationID EQ "" >
true location is #location[i]# <br>
<cfelse>
false location [i] is is #location[i]# and
locationid is #locationID# <br>
</cfif>
<cfif location[i] EQ "" AND locationID NEQ "" >
same as above
</cfif>
</cfloop>
</cfoutput>
Then you'll know why you are not getting the results you expect.
As long as there are items in your location array, the loop will run and the CFLoop tag will take care of incrementing i.
What I'd guess is happening is that you are checking two conditions within the loop and if neither match, no code will run. You're handling these:
location[i] NEQ "" AND #locationID# EQ ""
location[i] EQ "" AND #locationID# NEQ ""
but not these:
location[i] EQ "" AND #locationID# EQ ""
location[i] NEQ "" AND #locationID# NEQ ""
Could that be it?