• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

cfloop not working in conditional statement

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.
0
kbtudor
Asked:
kbtudor
  • 3
1 Solution
 
_agx_Commented:
> Here's my code in part: - this does nothing.
> <cfif isDefined("form.liveApp") AND form.liveApp EQ '0'>

The hidden form fields all have the same name. So form.liveApp will likely contain a list of the values like: 0,1,0,0,1,0.  The code does nothing because form.liveApp would almost never be equal to '0' or '1'.

You could give the hidden fields a unique name.  Like this

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


Action page  (not tested)
====================
<cfparam name="form.ID" default="">
<cfloop list="#form.ID#" index="i" delimiters=",">
   <cfif IsDefined("form.liveApp#i#")>
      <cfset oldValue = form["liveApp"& i]>
      <cfquery name="updateBoards" datasource="#application.dbdsn#">
      UPDATE tblBoards
      SET    liveApp = <cfif oldValue eq 1>'0'<cfelse>'1'</cfif>
      WHERE ID = '#i#'
      </cfquery>
    </cfif>
</cfloop>

0
 
_agx_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>
0
 
_agx_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>
0
 
kbtudorAuthor Commented:
Excellent!  Thanks so much - this is exactly what I was after.

k.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now