SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: while loop within a loop?
-
Jan 16, 2007, 04:48 #1
- Join Date
- Jul 2005
- Posts
- 456
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
while loop within a loop?
hi all,
im writing some script to add records to my db. but if a value thats about to be added exists, then dont add that record.
for example: if i want to add 7 records to my db with the values 1 - 7, but the db already has 3, 5 and 7, then it should only add 4 new records (1, 2, 4 and 6). i have a record returning all records form my db, and an array with the values i want to add. i want to loop through my recordset of all existing entries, then for each loop, start another loop to see if the value exists, and if it doesnt , then add a new record. im running into troubles here, has anyone any ideas?
hope that makes sense.
cheers
- G
-
Jan 16, 2007, 22:24 #2
You can use the Following code snippet.
Code:while not rsAdd.Eof 'Loop of db records those needs to be added. for I=0 to Ubound(arrRecords) 'Loop Of Already Existing Records if rsAdd(recordid)=arrRecords(i) then found = 1 'Flag set to 1 if found else found = 0 'Flag set to 0 if not found end if Next If found = 0 then 'If found set to 0 then add that record 'Add that Record End If rsAdd.MoveNext Wend
-
Jan 17, 2007, 06:44 #3
- Join Date
- Jul 2005
- Posts
- 456
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks SSJ, ill try that later. i keep running into more things that dont work! aarrgggh!
-
Jan 19, 2007, 11:11 #4
- Join Date
- Jul 2005
- Posts
- 456
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
hi SSJ,
im trying to get this code to work. ive hashed your code a bit and now have:
while(z < 7){
for(i = 0; i < 7; i ++){
if (addArray[i] == existArray[z]){
found = 1
Response.write("dates to add: " + i + " " + addArray[i] + " -- existing: " + existArray[z] + " FOUND!<br/>")
}else{
found = 0
Response.write("dates to add: " + i + " <b>" + addArray[i] + "</b> -- existing: " + existArray[z] + " ADD RECORD<br/>")
}
}
z ++
}
its writing out:
dates to add: 0 02/01/2007 -- existing: 02/01/2007 FOUND!
dates to add: 1 03/01/2007 -- existing: 02/01/2007 ADD RECORD
dates to add: 2 04/01/2007 -- existing: 02/01/2007 ADD RECORD
dates to add: 3 05/01/2007 -- existing: 02/01/2007 ADD RECORD
dates to add: 4 06/01/2007 -- existing: 02/01/2007 ADD RECORD
dates to add: 5 07/01/2007 -- existing: 02/01/2007 ADD RECORD
dates to add: 6 08/01/2007 -- existing: 02/01/2007 ADD RECORD
dates to add: 0 02/01/2007 -- existing: 04/01/2007 ADD RECORD
dates to add: 1 03/01/2007 -- existing: 04/01/2007 ADD RECORD
dates to add: 2 04/01/2007 -- existing: 04/01/2007 FOUND!
dates to add: 3 05/01/2007 -- existing: 04/01/2007 ADD RECORD
dates to add: 4 06/01/2007 -- existing: 04/01/2007 ADD RECORD
dates to add: 5 07/01/2007 -- existing: 04/01/2007 ADD RECORD
dates to add: 6 08/01/2007 -- existing: 04/01/2007 ADD RECORD
dates to add: 0 02/01/2007 -- existing: 05/01/2007 ADD RECORD
dates to add: 1 03/01/2007 -- existing: 05/01/2007 ADD RECORD
dates to add: 2 04/01/2007 -- existing: 05/01/2007 ADD RECORD
dates to add: 3 05/01/2007 -- existing: 05/01/2007 FOUND!
dates to add: 4 06/01/2007 -- existing: 05/01/2007 ADD RECORD
dates to add: 5 07/01/2007 -- existing: 05/01/2007 ADD RECORD
dates to add: 6 08/01/2007 -- existing: 05/01/2007 ADD RECORD
dates to add: 0 02/01/2007 -- existing: 08/01/2007 ADD RECORD
dates to add: 1 03/01/2007 -- existing: 08/01/2007 ADD RECORD
dates to add: 2 04/01/2007 -- existing: 08/01/2007 ADD RECORD
dates to add: 3 05/01/2007 -- existing: 08/01/2007 ADD RECORD
dates to add: 4 06/01/2007 -- existing: 08/01/2007 ADD RECORD
dates to add: 5 07/01/2007 -- existing: 08/01/2007 ADD RECORD
dates to add: 6 08/01/2007 -- existing: 08/01/2007 FOUND!
dates to add: 0 02/01/2007 -- existing: 09/01/2007 ADD RECORD
dates to add: 1 03/01/2007 -- existing: 09/01/2007 ADD RECORD
dates to add: 2 04/01/2007 -- existing: 09/01/2007 ADD RECORD
i need to find out add the records 3, 6 and 7. NOT 2, 4, 5 and 8. any ideas?
many thanks for your help.
- G
-
Jan 19, 2007, 12:25 #5
- Join Date
- Nov 2004
- Location
- Nelson BC
- Posts
- 2,310
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here's how I'd do it.
First, create a nice reusable exists method of the array object. This will tell me true or false if an element exists within an array:
Code:Array.prototype.exists = function (item) { if (!Array.indexOf) { Array.prototype.indexOf = function (thing) { for (var i=0; i < this.length; i++) { if (this[i] === thing) { return i; } } return -1; } } if (this.indexOf(item) > -1) { return true; } else { return false; } }
Code:existArray = ["02/01/2007","04/01/2007","05/01/2007","08/01/2007"]; addArray = ["02/01/2007","03/01/2007","04/01/2007","05/01/2007","06/01/2007","07/01/2007","08/01/2007"]; for (var i=0; i < addArray.length; i++) { if (!existArray.exists(addArray[i])) { // add to database // maybe append addArray[i] to existArray? // existArray.push(addArray[i]); } }
-
Jan 19, 2007, 22:32 #6
Use the Following code:
while(z < 7){
for(i = 0; i < 7; i ++){
if (addArray[i] == existArray[z]){
found = 0
Response.write("dates to add: " + i + " " + addArray[i] + " -- existing: " + existArray[z] + " Add Record!<br/>")
}else{
found = 1
Response.write("dates to add: " + i + " <b>" + addArray[i] + "</b> -- existing: " + existArray[z] + " FOUND<br/>")
}
}
z ++
}
-
Jan 22, 2007, 03:34 #7
- Join Date
- Jul 2005
- Posts
- 456
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thanks jim, that works great. neat solution. thankyou both for your hard work.
- G
Bookmarks