SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    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

  2. #2
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    -SSJ

  3. #3
    SitePoint Evangelist
    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!

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2005
    Posts
    456
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    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

  5. #5
    SitePoint Wizard
    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;
    	}
    }
    OK now that we have a way of telling if an item is within an array, what you are trying to do is easy:
    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]);
    	}
    }

  6. #6
    SitePoint Guru SSJ's Avatar
    Join Date
    Jan 2007
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 ++
    }

  7. #7
    SitePoint Evangelist
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •