First off, my code usses in loops, so I'm not enabling TGML
The
I know the datex dosen't go anywhere right now, but it will later. The "executeSQL(xxx)" function works fine elsewhere. Below is the code for the "GetSitesByDate(date1,date2,"all"
" function, just in case you want to look at it. I can confirm that it works elsewhere also. The GetSitesByDate(date1,date2,"all"
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:
-- The code above (up to the next
oh, the variable conn is "var conn = makeConnection();" 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("Dec 10 2003"
;
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("Dec 10 2003"
;
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 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 = "Availible" } else
if (sitesz[i]==2) { sitex.status = "Occupied" } else
if (sitesz[i]==3) { sitex.status = "notBuilt" } else { sitex.status = "Error" }
// checking and getting the NextEvent for each site
visitSites.MoveFirst();
visitSites.Filter="SiteID=" + i ;
while (!visitSites.eof) { // loops thru each visit of site 'i' to get the next event
erm.startDate = new String(visitSites("startDate"));
erm.endDate = new String(visitSites("endDate"));
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("SiteID"));
sitex.phone = new String(aSet("phone"));
sitex.cable = new String(aSet("cable"));
sitex.internet = new String(aSet("internet"));
sitex.electricity = new String(aSet("electricity"));
sitex.sewer = new String(aSet("sewer"));
sitex.padLength = new String(aSet("padLength"));
sitex.doneConstruction = new String(aSet("doneConstruction"));
sitex.phase = new String(aSet("phase"));
sitex.discription = new String(aSet("discription"));
sitex.baseRate = new String(aSet("baseRate"));
sitex.projectedDate = new String(aSet("projectedDate"));
// 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
}
Code:
function GetSitesByDate(StartDate,EndDate,type) {
//type legend :"all" = all sites in database, "built" = 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 = "SELECT SiteID,doneConstruction FROM site"
if (type=="built") { countSQL = countSQL + " WHERE doneConstruction=1"; } else // add the doneConstruction clause
if (type=="all") { countSQL = countSQL; } // Add nothing to the query
var exstingSites = executeSQL(countSQL + " ORDER BY SiteID");
//Selection from Visit table
var conn = makeConnection();
var sites = new Array();
var selSQL = "Select SiteID,StartDate,EndDate from Visit";
var ss = executeSQL(selSQL);
var ss2 = new Object();
var ri=0, i=0;
if (type=="all") {
//set them all to 'not built' and then overwrite the filled or vacant ones later
while(!exstingSites.eof) {
sites[exstingSites("SiteID")] = 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("SiteID")] = 1;
exstingSites.MoveNext();
}
} else {
ss.MoveFirst();
exstingSites.MoveFirst();
//loop through existing/installed sites;
while(!exstingSites.eof) { //A1
ri = exstingSites("SiteID");
//query for all results that deal with site 'ri';
ss.Filter="SiteID='" + ri + "'";
if (type=="all" && exstingSites("doneConstruction")==true) {
sites[ri] = 1;
}
if (type=="all" && exstingSites("doneConstruction")==false) {
sites[ri] = 3;
}
//set the current site to 1 (availble), if it is not in the visit table;
if (ss.eof && type!="all" ) { sites[ri] = 1 }
//loop through all 'visits' from site 'ri'
while(!ss.eof){ // --A2
ss2.SiteID = new String(ss("SiteID"));
ss2.StartDate = new Date(ss("StartDate"));
ss2.EndDate = new Date(ss("EndDate"));
// 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(" ----< " + ss2.StartDate.getMonth() + " >---- ");
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;
}
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="SELECT * from Site WHERE SiteID IN (";
sql2 = "";
for ( i=1; i<=SitesArray.length; i++ ) {
if ( SitesArray[i] > 0 ) { sql2 += i + "," }
}
finSQL = servSQL + sql2 + ") ORDER BY SiteID;";
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("SiteID"));
sitex.phone = new String(siteSet("phone"));
sitex.cable = new String(siteSet("cable"));
sitex.internet = new String(siteSet("internet"));
sitex.electricity = new String(siteSet("electricity"));
sitex.sewer = new String(siteSet("sewer"));
sitex.padLength = new String(siteSet("padLength"));
sitex.doneConstruction = new String(siteSet("doneConstruction"));
sitex.phase = new String(siteSet("phase"));
sitex.discription = new String(siteSet("discription"));
sitex.baseRate = new String(siteSet("baseRate"));
sitex.projectedDate = new String(siteSet("projectedDate"));
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
}
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("Dec 10 2003"
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("Dec 10 2003"
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 "//testing for tek-tips post" 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.