Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange Array behavior

Status
Not open for further replies.

Suthern

Technical User
Dec 5, 2000
122
US
First off, my code usses in loops, so I'm not enabling TGML :( The
Code:
 tags are just for looks

I'm been working on this project (Not this particular piece of coding) for over 6 months on and off, and just recently I've stumbled across some odd behavior of an ARRAY which is causing me to loose hair.
All of this is in ASP mind you, so don't worry, I'm not letting someone 'view source' and see my SQL statements.
Below are the sections of code that are appropiete. Inbetween the code sections are comments, and below it all are more comments.
>>>>>>>>
[code]
function getCurrStatOfAll(datex){
	// returns an array of objects which have all the properties for each site including the next event at each site
	
	// Variables
	var MAXSITES = 210;
	var date1 = new Date("December 1 2003");
	var date2 = new Date("December 1 2003");
	//Tomorrow = Tomorrow.setDate(TodayDate.getDate()+1);
	diff = 0;
	olddiff = date1.getTime();
	var sitex = new Object();
	var Services = new Array();
	erm = new Object();
	
	// query database - sites[] Legend: 1=Availible, 2=DateConflict, 3=NotBuilt;
	sitesz = GetSitesByDate(date1,date2,"all");	
	
	// SQL to get all sites from SITE ordered by SiteID
	var servSQL = "SELECT * from SITE"
	finSQL = servSQL + " ORDER BY SiteID;";
	aSet = executeSQL(finSQL);
	
	//SQL to get all Sites from the Visit Table
	ssqqll = "SELECT SiteID,startDate,endDate FROM Visit ORDER BY SiteID";
	visitSites = executeSQL(ssqqll);

	aSet.MoveFirst()
	visitSites.MoveFirst()

	for ( i=1; i<sitesz.length; i++ ) {			// cycle through each site from the SITE table

		if (sitesz[i]>0) {		// is this site in the database?
			// do a switch for the .status
			if (sitesz[i]==1) { sitex.status = &quot;Availible&quot; } else
			if (sitesz[i]==2) { sitex.status = &quot;Occupied&quot; } else
			if (sitesz[i]==3) { sitex.status = &quot;notBuilt&quot; } else { sitex.status = &quot;Error&quot; }
		
			// checking and getting the NextEvent for each site
			visitSites.MoveFirst();		
			visitSites.Filter=&quot;SiteID=&quot; + i ;
			while (!visitSites.eof) {		//  loops thru each visit of site 'i' to get the next event
				erm.startDate = new String(visitSites(&quot;startDate&quot;));
				erm.endDate = new String(visitSites(&quot;endDate&quot;));
				startTime = new Date(erm.startDate);	startTime = startTime.getTime(); // convert to TIME
				endTime = new Date(erm.endDate);  endTime = endTime.getTime();
				if (date1.getTime() > startTime && date1.getTime() < endTime && sitesz[i]==2 ) {	// if occupied, and if this is the visit that is current.
					sitex.nextEvent = new Date(endTime);				// nextEvent = closest end time
				}
				if (date1.getTime() < startTime && sitesz[i]==1 ) {		// if availble, and in the furture
					diff = startTime - date1.getTime();
					if (diff <= olddiff ) {									// if the date is closer to today
						sitex.nextEvent = new Date(startTime)	;			// the nextEvent is the closest startTime
					}
					olddiff = diff;
				}
				visitSites.MoveNext();
			} // end while
			visitSites.Filter = 0;		// resets the filter to include all the sites
			
			// get services for each site
			sitex.SiteID = new String(aSet(&quot;SiteID&quot;));
			sitex.phone = new String(aSet(&quot;phone&quot;));
			sitex.cable = new String(aSet(&quot;cable&quot;));
			sitex.internet = new String(aSet(&quot;internet&quot;));
			sitex.electricity = new String(aSet(&quot;electricity&quot;));
			sitex.sewer = new String(aSet(&quot;sewer&quot;));
			sitex.padLength = new String(aSet(&quot;padLength&quot;));
			sitex.doneConstruction = new String(aSet(&quot;doneConstruction&quot;));
			sitex.phase = new String(aSet(&quot;phase&quot;));
			sitex.discription = new String(aSet(&quot;discription&quot;));
			sitex.baseRate = new String(aSet(&quot;baseRate&quot;));
			sitex.projectedDate = new String(aSet(&quot;projectedDate&quot;));
//			sitex.xxxx = sitesz[i];
			Services[i] = sitex;		// assigns object to one spot in the aray
			// moves to next site in SITE table
			aSet.MoveNext();
		} // end if <site is in database>
		
	} // end for <sites in SITE table>

	//return finSQL;    //return the string FOR TESTING
	visitSites.close();
	aSet.close();
	Services[5].SiteID = 5;		// testing for tek-tips post.
	return Services; //return an array, not an object
}
I know the datex dosen't go anywhere right now, but it will later. The &quot;executeSQL(xxx)&quot; function works fine elsewhere. Below is the code for the &quot;GetSitesByDate(date1,date2,&quot;all&quot;)&quot; function, just in case you want to look at it. I can confirm that it works elsewhere also. The GetSitesByDate(date1,date2,&quot;all&quot;) funtion returns an ARRAY like this[1,2,1,3,1,2,1,1,1,2] where the position in the array is the siteID, and the number is the status. 1=Availible, 2=Site is occupied/or a date conflict, 3=site is in database but has the 'doneConstruction' set to false.Um, here is the function code:
Code:
function GetSitesByDate(StartDate,EndDate,type) {
	//type legend    :&quot;all&quot; = all sites in database, &quot;built&quot; = just the sites which can be stayed at
	//Legend: 1=Availible, 2=DateConflict, 3=SiteNotMade
	//Find out sites that are installed, and get thier SiteID's;
      var countSQL = &quot;SELECT SiteID,doneConstruction FROM site&quot;
      if (type==&quot;built&quot;) { countSQL = countSQL + &quot; WHERE doneConstruction=1&quot;; }	else			// add the doneConstruction clause
      if (type==&quot;all&quot;) { countSQL = countSQL;  }			// Add nothing to the query
      var exstingSites = executeSQL(countSQL + &quot; ORDER BY SiteID&quot;);
      
      //Selection from Visit table
	var conn = makeConnection();
	var sites = new Array();
	var selSQL = &quot;Select SiteID,StartDate,EndDate from Visit&quot;;
	var ss = executeSQL(selSQL);
	var ss2 = new Object();
	
	var ri=0, i=0;
	
	if (type==&quot;all&quot;) {
		//set them all to 'not built' and then overwrite the filled or vacant ones later
		while(!exstingSites.eof) {
			sites[exstingSites(&quot;SiteID&quot;)] = 3;
			exstingSites.MoveNext();
		}
	}		// end of 'change all the not built'
	
	//make sure we are at the beginning of each recordset;
	exstingSites.MoveFirst();
	if (ss.RecordCount == 0) { 				// pick all sites if the visit table is empty
		while(!exstingSites.eof) {	
			sites[exstingSites(&quot;SiteID&quot;)] = 1;
			exstingSites.MoveNext();
		}
	 } else { 
	ss.MoveFirst();
	exstingSites.MoveFirst();
	
	//loop through existing/installed sites;
	while(!exstingSites.eof) {		//A1
	
		ri = exstingSites(&quot;SiteID&quot;);
		
		//query for all results that deal with site 'ri';
		ss.Filter=&quot;SiteID='&quot; + ri + &quot;'&quot;;
		
		if (type==&quot;all&quot; && exstingSites(&quot;doneConstruction&quot;)==true) {
			sites[ri] = 1;
		}
		if (type==&quot;all&quot; && exstingSites(&quot;doneConstruction&quot;)==false) {
			sites[ri] = 3;
		}
		
		//set the current site to 1 (availble), if it is not in the visit table;
		if (ss.eof && type!=&quot;all&quot; ) { sites[ri] = 1 }
		
		//loop through all 'visits' from site 'ri'
		while(!ss.eof){			// --A2
			
			ss2.SiteID = new String(ss(&quot;SiteID&quot;));
			ss2.StartDate = new Date(ss(&quot;StartDate&quot;));
			ss2.EndDate = new Date(ss(&quot;EndDate&quot;));
		
		
			// add a month from database values to display and calc properly 
			ss2.StartDate.setMonth(ss2.StartDate.getMonth()+1);
			ss2.EndDate.setMonth(ss2.EndDate.getMonth()+1);
			//Response.write(&quot; ----< &quot; + ss2.StartDate.getMonth() + &quot; >---- &quot;);
		
			PlandStart = new Date(StartDate);
			PlandEnd = new Date(EndDate);
		
			var PlndStrt = PlandStart.getTime();
			var PlndEnd = PlandEnd.getTime();
			var ExstStrt = ss2.StartDate.getTime();
			var ExstEnd = ss2.EndDate.getTime();
		
			//Response.write(printDate2(ExstStrt)); //sensless babbling
			
			// Does the planned Start, planned end, or are both inside the existing;
			if (PlndStrt>=ExstStrt && PlndStrt<=ExstEnd) {
				sites[ss(0)] = 2;
			} else {
				// is the end of the planned inside the existing;
				if (PlndEnd>=ExstStrt && PlndEnd<=ExstEnd) {
					sites[ss(0)] = 2;
				} else {

					// does the planned totally cover the existing;
					if (PlndStrt<=ExstStrt && PlndEnd>=ExstEnd) {
						sites[ss(0)] = 2;
					} else {
						sites[ss(0)] = 1;
					}
				}
				

				// does the planned totally cover the existing again;
				if (PlndStrt<=ExstStrt && PlndEnd>=ExstEnd) {
					sites[ss(0)] = 2; }
			  //  }
	
			}		
		
		//move to next 'visit' from same site 'ri' from visit table;
		ss.movenext();
		}		// Closes A2
		
		
		// Move on to next number from site table
		exstingSites.MoveNext();
		
		//reset SiteID filter over visit table;
		ss.Filter=0;
	}	// Closes A1
	} // closes check for empty visits 
	
	//sites = [0,1,1,0,1,0,1,0,1,2,1,1,0,1,1,1,0,0,3,1,0,1]; //early testing array
	exstingSites.close();
	ss.close();
	conn.Close();
	return sites;
}
-- The code above (up to the next
Code:
) works fine. Meaning this function works fine.
Below is code which does something much simpler then what the first section does, and works.
[code]
function getServicesMSites(conn,SitesArray) {
	var servSQL=&quot;SELECT * from Site WHERE SiteID IN (&quot;;
	sql2 = &quot;&quot;;
	for ( i=1; i<=SitesArray.length; i++ ) {  
		if ( SitesArray[i] > 0 ) { sql2 += i + &quot;,&quot; } 
	}
	finSQL = servSQL + sql2 + &quot;) ORDER BY SiteID;&quot;;
	siteSet = executeSQL(finSQL);
	
	// check to see if any row was returned
	var Services = new Array();
//	if (siteSet.eof) { // no site with this SiteID
//		return Services; // return empty Array
//	}
	siteSet.MoveFirst();
	for ( i=1; i<SitesArray.length; i++ ) {
		if ( SitesArray[i]>0) {
			var sitex = new Object();
			sitex.SiteID = new String(siteSet(&quot;SiteID&quot;));
			sitex.phone = new String(siteSet(&quot;phone&quot;));
			sitex.cable = new String(siteSet(&quot;cable&quot;));
			sitex.internet = new String(siteSet(&quot;internet&quot;));
			sitex.electricity = new String(siteSet(&quot;electricity&quot;));
			sitex.sewer = new String(siteSet(&quot;sewer&quot;));
			sitex.padLength = new String(siteSet(&quot;padLength&quot;));
			sitex.doneConstruction = new String(siteSet(&quot;doneConstruction&quot;));
			sitex.phase = new String(siteSet(&quot;phase&quot;));
			sitex.discription = new String(siteSet(&quot;discription&quot;));
			sitex.baseRate = new String(siteSet(&quot;baseRate&quot;));
			sitex.projectedDate = new String(siteSet(&quot;projectedDate&quot;));
			siteSet.MoveNext();
			Services[i] = sitex;
		}
	} 
	siteSet.close();
	//return finSQL;		//return the string FOR TESTING
	Services[3].padLength = 300;	// testing
	return Services; //return an array of objects
}
oh, the variable conn is &quot;var conn = makeConnection();&quot; and the makeConnection has the connection strings. Don't worry about them as they work fine. :)
Ok, now here is what it all is supposed to do. LOL..... when I call the function getCurrStatOfAll(SomeDate), I need an array to be returned, An array of objects. Example below.
SiteArray = getCurrStatOfAll(&quot;Dec 10 2003&quot;);
SiteArray[1].SiteID should now be = to 1, and
SiteArray[44].SiteID should be = to 44...ect.

However, what I get for the .SiteID is the value of the LAST input for .SiteID.. Example;
SiteArray = getCurrStatOfAll(&quot;Dec 10 2003&quot;);
SiteArray[1].SiteID = 44 and
SiteArray[3].SiteID = 44
SiteArray[44].SiteID = 44. GRRRRR. This is happaning with ALL the values (SiteID, phone, cable, padLength, phase....ect);

Ok. Section/function 3 of the code (the 3rd coding between the
Code:
 tags) works fine. And you can see my //testing right before the object is returned, and it correctly modifys ONLY the object in 'slot?' 3 of the array.

PROBLEM: See the &quot;//testing for tek-tips post&quot; line of code? when I call that function, in the returned array ALL the array[x].SiteID's are 5!!! I repeat, not just array[5].SiteID is 5, but also array[1].SiteID and array[44].SiteID.
Now thats what has my head going in loops... Almost IDENTICAL coding, and completely differn't results returned.

If you have read this far, Congrats!!! And thanks for taking the time!!

If there are any functions which are unclear, I'll explain them if someone asks. Oh, and I know I'm not the most efficiant coder, but I welcome tips :(

Does anyone tell me how to remedy the 3rd section of code to return what it needs too? (not just the LAST result)

-Suthern

P.S. When I call the GetsitesBydate, I'm using the 'all' type. Just to make things a little clearer.
 
One more thing I forgot the mention: When I call the getServicesMSites(conn,SitesArray) function, I do so like this:
Code:
conn = makeConnection();
SitesArray = GetsitesBydate(date1,date2,&quot;all&quot;)
Hoped that help make things more clear.
-Suthern
 
Well thanks everyone for contributing to the post. If you were as puzzled as I was, this should make things much clearer. (haha). The code now works!!! You know what I did? I moved the
Code:
var sitex = new Object();
into the for loop. And hey, presto, it works!!

umm, can anyone tell me WHY that move made the difference? I'm still puzzled by that one..

-Suthern
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top