Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Multiple Condition If Statement

New Here ,
Sep 07, 2012 Sep 07, 2012

I am currently working on a mobile redirect in which I would like the mobile user to be able view the Full version of only two pages of the site.  The link to the page from the mobile site sends redirect=true in the query string and creates a COOKIE named redirect.  My question is how do i properly write the below conditional statement to achieve the results I need?  I need news.cfm and news-details.cfm to allow the full version and any other link clicked to take them back to the mobile site.  Of course I have left out portions of code but I think I have included enough to understand the issue.  This code resides in the Application.cfm file. Thanks in advance.

<cfif COOKIE.redirect IS "true" AND #CGI.script_name# IS "/news.cfm" OR  #CGI.script_name# IS "/news-details.cfm">

I have tried every way I can think of and it just won't work the way I need it to.

4.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Sep 07, 2012 Sep 07, 2012

Well, the main thing I see wrong is that you are not using parentheses in your if.  When you combine ANDs and ORs you usually need to use parens to make them work the way you want.

In the case above your statement will always eval to TRUE in the CGI.script_name is equal to /news-details.cfm.  I think you want something like this:

<cfif COOKIE.redirect EQ "true" AND (CGI.script_name EQ "/news.cfm" OR  CGI.script_name EQ "/news-details.cfm")>

Note also that you do not need any of the hash tags you w

...
Translate
Advocate ,
Sep 07, 2012 Sep 07, 2012

Well, the main thing I see wrong is that you are not using parentheses in your if.  When you combine ANDs and ORs you usually need to use parens to make them work the way you want.

In the case above your statement will always eval to TRUE in the CGI.script_name is equal to /news-details.cfm.  I think you want something like this:

<cfif COOKIE.redirect EQ "true" AND (CGI.script_name EQ "/news.cfm" OR  CGI.script_name EQ "/news-details.cfm")>

Note also that you do not need any of the hash tags you were using in that statement. You had it correct for your COOKIE reference, but both of your CGI references used extraneous hash tags.  I'm curious if you thought they needed to be accessed differently for some reason.

I changed your IS to EQ simply because I could not bring myself to type IS. It's not wrong to use IS, I just hate it and I am very opinionated.

jason

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2012 Sep 08, 2012

Thank you 12robots,

And yes I did think I needed the hash tags, I am a Coldfusion newbie and coming from php.  It drives me nuts that sometimes you need the hash tags and sometimes not.  I think I tried the statement with the parens the way you showed but I'm not 100%.  I'll have to give it another shot on Monday morning. 

Thank you so much for your quick reply, I see all too often of forums where questions go unanswered for weeks at a time. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 08, 2012 Sep 08, 2012

What I tell my student is: If (it's in quotes inside of a tag or function call) OR (it's in CFOUTPUT and NOT in quotes inside of a tag or function) then use ##

Examples:

<cfset myVar = otherVariables />

<cfset myVar = "String with #otherVariable#" />

<cfset myVar = "String with" & otherVariable />

<cfif myVar EQ other variables>

<cfif myVar EQ "String with #otherVariable#">

<cfset myVar EQ "String with" & otherVariable />

<cfset functionCall(otherVaribales, "String with #otherVariable#">

<cfoutput>

    <cfif myVar EQ other variables>

        #otherVariable#

    </cfif>

</cfoutput>

There are some notable exceptions. For example:

<cfloop condition="var EQ otherVar">

<cfloop query="queryName">

My rule above may not be perfect (already updated it once this morningsince I was going from memory). Eventually you just get the hang of it.

Jason

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 08, 2012 Sep 08, 2012
LATEST

Great, that makes perfect sense!!  There's aways exceptions, what fun would it be without them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources