We help IT Professionals succeed at work.

cfloop not working in conditional statement

kbtudor
kbtudor asked
on
238 Views
Last Modified: 2013-12-24
I'm having trouble with looping within a conditional statement.  I have a table where there are yes/no toggles (liveApp).  Each row has a checkbox.  If the checkbox is checked I need to:

- If liveApp is currently Yes (1) - change it to No (0)
- If liveApp is currently No (0) - change it to Yes (1)

Here's my code in part: - this does nothing.
<cfif isDefined("form.liveApp") AND form.liveApp EQ '0'>
<cfloop list="#form.ID#" index="i" delimiters=",">
      <cfquery datasource="#application.dbdsn#">
      UPDATE tblBoards SET
      liveApp = '1'
      WHERE ID = '#i#'
      </cfquery>
</cfloop>
<cfelseif form.liveApp EQ '1'>
      <cfquery datasource="#application.dbdsn#">
      UPDATE tblBoards SET
      liveApp = '0'
      WHERE ID = '#i#'
      </cfquery>
</cfif>

FORM
<cfoutput>
<cfloop query="listBoards">
<tr>
<td><input type="checkbox" name="ID" id="ID" value="#listBoards.ID#">
<input type="hidden" name="liveApp" id="liveApp" value="#listBoards.liveApp#" /></td>
<td><cfif listBoards.liveApp EQ '0'>No<cfelseYes</cfif></td>
</tr>
</cfloop>
</cfoutput>

Thanks for any advice.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2015
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
Another option is to just use the the id's

    <cfquery datasource="#application.dbdsn#">
      UPDATE tblBoards
      SET   liveApp = '1'
      WHERE ID IN (
 '#i#'
      </cfquery>
CERTIFIED EXPERT
Most Valuable Expert 2015

Commented:
> Another option is to just use the the id's...

Ignore the last post.  I hit submit accidentally.

Another option would be to use a CASE statement to toggle the values.  I don't know what database you're using but here is an example using ms sql syntax.

Note - I'm assuming your columns are varchar since you used single quotes around the values (though a numeric type would seem more appropriate here)

<cfif IsDefined("form.id") AND listLen(form.id) gt 0>
  <cfquery datasource="#application.dbdsn#">
      UPDATE tblBoards
      SET   liveApp = CASE WHEN liveApp = '1' THEN '0' ELSE '1' END
      WHERE ID IN
          (
          <cfqueryparam value="#form.id#" cfsqltype="cf_sql_varchar" list="true">
          )
  </cfquery>
</cfif>

Author

Commented:
Excellent!  Thanks so much - this is exactly what I was after.

k.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.