473,488 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Looping through list and comparing to database field problem

8 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. <cfoutput>#old.ID#</cfoutput>
  2. <cfset chkChanged=ArrayNew(1)>
  3. <cfoutput query="old">
  4.     <cfloop list="#form.chk#" index="chk">
  5.         <cfif #ID# IS NOT #chk# >
  6.             <cfset chkChanged[CurrentRow]= id>
  7.         </cfif>
  8.     </cfloop>
  9. </cfoutput>
  10.  
Feb 26 '08 #1
5 3467
Esmeralda
8 New Member
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.
Feb 26 '08 #2
Esmeralda
8 New Member
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.
Feb 26 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
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.
Feb 27 '08 #4
Esmeralda
8 New Member
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.
Feb 27 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
Glad to hear that you got it working.
Feb 28 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
11707
by: Matt | last post by:
how to get the last record from database without looping? Whenever the user need to insert a new record to the database, it just increment the id field by one from the last record. I tried...
4
5928
by: Emmett Power | last post by:
Hi, I have a small online survey in two parts designed to allow users to rank a few organisations that they have dealings with from a large list of organisations. I want the users to be able to...
27
2579
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
3
2793
by: BlackFireNova | last post by:
I am working on an Access 2002 Database. I created a new table and imported some notes data into it from an existing table. Now, when I try to create a List Box, and it displays the list of...
4
872
by: Danny | last post by:
I set this up so the users can select more than one record so they can update records a bunch at a time. only one field is modified. How can I edit this table easily instead of scrolling through...
1
4463
by: Nicole | last post by:
Hello! I hope there is someone out there who can shed some light on this for me. I have a module that is supposed to look at an access table, pull out each bid record, link to another table to...
1
1963
by: u473 | last post by:
I am still a beginner with VBA and I need the VBA Code for the following problem. I have a list of activities with their Scheduled Start date, Duration, and Working hours in Table1. I need to...
0
2370
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
2
4707
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.