Skip to main content
Known Participant
February 17, 2010
Answered

<CFIF> Structure not working? What am I doing wrong?

  • February 17, 2010
  • 3 replies
  • 1266 views

Back at it. Hope I can figure this out today. Thanks again. There is one persnickety problem though. I have the DB set to a yes/no on certain items in a <cfif> structure. However, when the structure is processed it does not recognize the structure and continues to return the <cfelse> instead. I have tried changing the 1 to '1' and "1". I have also attempted using the isDefined('doubles'), but that did not work.

The previous page sends the URL.tName to this page.

<a href="" onclick='NewWindow("/forms/T-Register.cfm?tName=#getTournaments.tName#&tDate=#getTournaments.tDate#", "register",750,900,"yes");return false;'><img src="/images/regButton.jpg" width="100" height="22" /></a>

As I said the DB fields for these below are set to yes/no, which is either 1 or 0. Obviously I am doing something wrong, but what is it? The <cfif> structure is not being ignored because it returns the last item <cfelse>. The statements are either true or false. At present it is as though the statements are all being read as false and the only thing that is true is the <cfelse>. Should I be scoping the <cfif> with the query? Any ideas out there. I am not a rocket scientist programmer, but know enough to get by.

<cfquery name="getTournaments" datasource="RBDS">

SELECT *

FROM tournamentFeatures

WHERE tName = '#URL.tName#'

</cfquery>

<cfif tName EQ #URL.tname# AND 'doubles' EQ 1>

<!--- #URL.tname# must have '#" signs around it because it is comparing the database "tname" to the URL.tname.

Aslo, the 'doubles' variable needs to be defined with tick marks because you are calling the valur from the

database.--->

<div class="redTextNORM">This is a Doubles Tournament. Please fill in the required information for the other team member.

<!---Tool Tip Begin--->

<a href="bowlingPartners.cfm" onClick="return show_hide_box(this,200, 425,'2px dotted','scroll')">More Info</a>

<!---Tool Tip End--->

</div>

<br />

<div class="g"><cfinput type="text" name="BowlerTwoName" value="Name of Bowler 2" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 2'" onfocus="if(this.value =='Name of Bowler 2' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerTwoBook" value="Bowler 2 Average" required="no" onblur="if(this.value=='') this.value='Bowler 2 Average'" onfocus="if(this.value =='Bowler 2 Average' ) this.value=''"  /></div>

<cfelseif tName EQ #URL.tname# AND 'threePerTeam' EQ '1'>

<div class="redTextNORM">This tournament consists of a three person team. Please fill in the required information for the other team members.

<!---Tool Tip Begin--->

<a href="bowlingPartners.cfm" onClick="return show_hide_box(this,200, 425,'2px dotted','scroll')">More Info</a>

<!---Tool Tip End--->

</div>

<br />

<div class="g"><cfinput type="text" name="BowlerTwoName" value="Name of Bowler 2:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 2:'" onfocus="if(this.value =='Name of Bowler 2:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerTwoBook" value="Bowler 2 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 2 Average:'" onfocus="if(this.value =='Bowler 2 Average:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerThreeName" value="Name of Bowler 3:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 3:'" onfocus="if(this.value =='Name of Bowler 3:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerThreeBook" value="Bowler 3 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 3 Average:'" onfocus="if(this.value =='Bowler 3 Average:' ) this.value=''"  /></div>

<cfelseif tName EQ #URL.tname# AND 'fourPerTeam' IS 'yes'>

<div class="redTextNORM">This tournament consists of a four person team. Please fill in the required information for the other team members.

<!---Tool Tip Begin--->

<a href="bowlingPartners.cfm" onClick="return show_hide_box(this,200, 425,'2px dotted','scroll')">More Info</a>

<!---Tool Tip End--->

</div>

<br />

<div class="g"><cfinput type="text" name="BowlerTwoName" value="Name of Bowler 2:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 2:'" onfocus="if(this.value =='Name of Bowler 2:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerTwoBook" value="Bowler 2 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 2 Average:'" onfocus="if(this.value =='Bowler 2 Average:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerThreeName" value="Name of Bowler 3:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 3:'" onfocus="if(this.value =='Name of Bowler 3:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="bowlerThreeBook" value="Bowler 3 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 3 Average:'" onfocus="if(this.value =='Bowler 3 Average:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerFourName" value="Name of Bowler 4:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 4:'" onfocus="if(this.value =='Name of Bowler 4:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerFourBook" value="Bowler 4 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 4 Average:'" onfocus="if(this.value =='Bowler 4 Average:' ) this.value=''"  /></div>

<cfelseif tName EQ #URL.tname# AND 'fivePerTeam' IS 'yes'>

<div class="redTextNORM">This tournament consists of a five person team. Please fill in the required information for the other team members.

<!---Tool Tip Begin--->

<a href="bowlingPartners.cfm" onClick="return show_hide_box(this,200, 425,'2px dotted','scroll')">More Info</a>

<!---Tool Tip End--->

</div>

<br />

<div class="g"><cfinput type="text" name="BowlerTwoName" value="Name of Bowler 2:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 2:'" onfocus="if(this.value =='Name of Bowler 2:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="bowlerTwoBook" value="Bowler 2 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 2 Average:'" onfocus="if(this.value =='Bowler 2 Average:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerThreeName" value="Name of Bowler 3:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 3:'" onfocus="if(this.value =='Name of Bowler 3:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerThreeBook" value="Bowler 3 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 3 Average:'" onfocus="if(this.value =='Bowler 3 Average:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerFourName" value="Name of Bowler 4:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 4:'" onfocus="if(this.value =='Name of Bowler 4:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerFourBook" value="Bowler 4 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 4 Average:'" onfocus="if(this.value =='Bowler 4 Average:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerFiveName" value="Name of Bowler 5:" required="yes" onblur="if(this.value=='') this.value='Name of Bowler 5:'" onfocus="if(this.value =='Name of Bowler 5:' ) this.value=''"  /></div>

<div class="g"><cfinput type="text" name="BowlerFiveBook" value="Bowler 5 Average:" required="no" onblur="if(this.value=='') this.value='Bowler 5 Average:'" onfocus="if(this.value =='Bowler 5 Average:' ) this.value=''"  /></div>

<cfelse>

<div class="redTextNORM">This is a Singles Tournament</div>

<cfinput type="hidden" name="singles" value=1>

</cfif>

When a button is clicked on this page: http://www.renobowls.com/tournaments.cfm (Super No-Tap) which is set as a "doubles" tournament in the DB.
It goes to this template (http://www.renobowls.com/forms/T-Register.cfm?tName=Super%20No-Tap&tDate=12/13/09). The query should recognize it as a 'doubles' tournament, yet the template identifies it as a 'singles' tournament. The process should return two extra fields where the registrant adds the other bowlers name and member card number. Like I said, the process is not recognizing the <cfif> structure and only returns one thing.

When I change the first instance to this:

<cfif tName EQ #URL.tname# AND 'doubles' IS NOT 1>

It works. But it seems backwards. In the database the tournament (Super No-Tap) is set to "1" or yes. The <cfif> rule is is the tournament is not "yes" or "1" then it should bypass it to the next group (cfif). I know its early but I feel a bit bonkers at this point.

    This topic has been closed for replies.
    Correct answer BuddWright

    First and foremost, properly scope your variables!  With so many variables floating around in your code using the same name, ColdFusion is likely to get mixed up and provide unexpected results.  By scoping your variables as a rule of thumb, you eliminate that possibility completely, and make your code a lot more readable.

    Second, when dealing with database bit/boolean (yes/no) fields that cannot be null, ColdFusion will allow you to test them as such, rather than testing them as numeric values or "yes/no" string values... i.e., "<cfif isDoubles>" will evaluate properly, as will "<cfif isDoubles EQ 1>".  I prefer the former, because it eliminates guessing on whether I need to test for "1", "yes", or "true" for a true boolean value.

    So, instead of this:


    <cfif tName EQ #URL.tname# AND 'doubles' EQ 1>

    I would recommend doing this:


    <cfif queryName.tName EQ URL.tName AND queryName.doubles>

    As long as the "doubles" field can't be null in your database, that'll work just fine.

    3 replies

    February 17, 2010

    Hi there,

    I think you are a bit muddled when it comes to when to use hashes and quotes etc.  Have a read over the help doc again.

    In the meantime try your cfif statement as:

    <cfif getTournaments.doubles EQ 1>

    I dont understand why you have this logic "tName EQ #URL.tname#"  you have already established this using your WHERE clause in the SQL statement.  You should get rid of the hashes around url.tname as well.

    Cheers

    Inspiring
    February 17, 2010

    Have you tried changing <cfif tName EQ #URL.tname# AND 'doubles' EQ 1> to <cfif #GetTournaments.tName# EQ #URL.tname# AND #GetTournaments.doubles# EQ 1> - before, it looks like you were checking to see if the exact text "tName" (not the variable by that name) equaled a URL variable, and the exact text "doubles" was 1.  Of course, I could be wrong....

        Dan

    BuddWrightCorrect answer
    Participating Frequently
    February 17, 2010

    First and foremost, properly scope your variables!  With so many variables floating around in your code using the same name, ColdFusion is likely to get mixed up and provide unexpected results.  By scoping your variables as a rule of thumb, you eliminate that possibility completely, and make your code a lot more readable.

    Second, when dealing with database bit/boolean (yes/no) fields that cannot be null, ColdFusion will allow you to test them as such, rather than testing them as numeric values or "yes/no" string values... i.e., "<cfif isDoubles>" will evaluate properly, as will "<cfif isDoubles EQ 1>".  I prefer the former, because it eliminates guessing on whether I need to test for "1", "yes", or "true" for a true boolean value.

    So, instead of this:


    <cfif tName EQ #URL.tname# AND 'doubles' EQ 1>

    I would recommend doing this:


    <cfif queryName.tName EQ URL.tName AND queryName.doubles>

    As long as the "doubles" field can't be null in your database, that'll work just fine.

    USN-WDAuthor
    Known Participant
    February 17, 2010

    Budd Wright YOU ARE AWESOME DUDE!!!! I changed it to what you suggested and works like a swiss watch. THANK YOU!