Connecting Tech Pros Worldwide Help | Site Map

Looping through list and comparing to database field problem

Newbie
 
Join Date: Feb 2008
Posts: 8
#1: Feb 26 '08
My situation:
I have a dynamic form with checkboxes. The checkboxes are submitted and added to a database. They are a list.

I want to compare the list with what is currently listed in the database. If there is anything new or removed, I want to know with a variable that says changed.

Basically:
Checkboxes = 1,2,3
Database rows and fields are:
row | field
1 | 2
2| 3

I want to know that 1 is new. OR if 2 is removed, I want to know that.

I've tried using listfind and listcontains, but I still cannot get this to work right.

I've even tried an array:
[HTML]
<cfoutput>#old.ID#</cfoutput>
<cfset chkChanged=ArrayNew(1)>
<cfoutput query="old">
<cfloop list="#form.chk#" index="chk">
<cfif #ID# IS NOT #chk# >
<cfset chkChanged[CurrentRow]= id>
</cfif>
</cfloop>
</cfoutput>
[/HTML]
Newbie
 
Join Date: Feb 2008
Posts: 8
#2: Feb 26 '08

re: Looping through list and comparing to database field problem


I guess I really want to know if there is a way to compare a list to a list, or a list to an array. Looping over just gives me the last value of the field. All I want to know is if anything has changed. Not exactly what has changed.
Newbie
 
Join Date: Feb 2008
Posts: 8
#3: Feb 26 '08

re: Looping through list and comparing to database field problem


As a temporary fix, I turned the array into a list. I then am checking the list length to see if there are any changes. The only problem is if they select and then de-select the same amount.

Expand|Select|Wrap|Line Numbers
  1. <cfif IsDefined('form.chkChanged')>    
  2.    <cfset ID=quotedvalueList(qry.ID)>
  3.   <cfset chkChanged=#form.chkChanged#>
  4.   <cfif #ListLen(ID)# NEQ #ListLen(chkChanged)#>
  5.         changed
  6.   </cfif>
  7. <cfelseif not IsDefined('form.chkChanged') AND #ListLen(ID)# IS NOT 0>
  8.     changed
  9. </cfif>
  10.  
*Not my exact code so there may be some errors throughout.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Feb 27 '08

re: Looping through list and comparing to database field problem


Try this:
Expand|Select|Wrap|Line Numbers
  1. <cfquery name="old" datasource="***">
  2.   SELECT field
  3.   FROM tableName
  4. </cfquery>
  5. <cfset temp = ValueList(old.field)>
  6. <cfset changed = false>
  7. <cfloop From="1" To="#ListLen(temp)#" index="field">
  8.   <cfif ListGetAt(temp, field) NEQ ListGetAt(form.chk,field)>
  9.     <cfset changed = true>
  10.   </cfif>
  11. </cfloop>
You may want to add a length check first.
Newbie
 
Join Date: Feb 2008
Posts: 8
#5: Feb 27 '08

re: Looping through list and comparing to database field problem


Thank you. I found that the below works great for my purpose.

Expand|Select|Wrap|Line Numbers
  1. <cfif #ID# NEQ #chkChanged#>
  2.         Changed
  3. <cfelse>
  4.         Unchanged    
  5. </cfif>
  6.  
With most of everything else above. So I can basically compare to see if the lists are the exact same using "valuelist" to turn the query into a comma separated list without a loop.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Feb 28 '08

re: Looping through list and comparing to database field problem


Glad to hear that you got it working.
Reply