Click to See Complete Forum and Search --> : Looped Recordset


kwilliams
03-28-2005, 05:33 PM
I have the following looped Recordset:
<%
for (i = 0; i <10; i++) {//Begin preccounter loop
var PrecCount = new Array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
//Declare incremented variable
var arrPrec = PrecCount[i];
//Response.Write("No: " + arrPrec + "<br>");
%>
<%
var rsCount = Server.CreateObject("ADODB.Recordset");
rsCount.ActiveConnection = MM_strConn_STRING;
rsCount.Source = "SELECT COUNT(P" + arrPrec + ") AS PrecCount FROM dbo.TABLENAME WHERE (ISNULL(P" + arrPrec + ", -1) > 0)";
rsCount.CursorType = 0;
rsCount.CursorLocation = 2;
rsCount.LockType = 1;
rsCount.Open();
var rsCount_numRows = 0;
%>
<%
var rsPrecinct = rsCount.Fields.Item("PrecCount").Value;
var PrecCounter = rsPrecinct;
if (PrecCounter != 0) {//Then
var PrecCounter = PrecCounter/PrecCounter
}//End if
Response.Write(PrecCounter++);
}//End loop
%>
<%
//Individual vote counter
if (!rsCount.BOF || !rsCount.EOF) {//Then
var PrecCount1 = PrecCounter;
var PrecCount = PrecCounter++;
Response.Write("<strong>" + PrecCount + "</strong>");//test
}//End if
%>

...which results in the correct 10 values being displayed:
1,1,0,0,0,0,0,0,0,0

So I need to assign those resulting values to different variable names, and then add those values together to come up with one main value. Ofcourse, this would result in the value "2" because of the first two values being "1". I'm somewhat of a newbie at using looped arrays to pull dynamic data, so I'm kind of lost on what the next step is. If anyone can point me in the right direction, or even help me to solve this one, that would be greatly appreciated. Thanks for any & all help.

kwilliams
03-28-2005, 05:47 PM
...by the way, I also tried this scenario:
<%
for (i = 0; i <10; i++) {//Begin preccounter loop
var PrecCount = new Array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
//Declare incremented variable
var arrPrec = PrecCount[i];
%>
<%
var rsCount = Server.CreateObject("ADODB.Recordset");
rsCount.ActiveConnection = MM_strConn_STRING;
rsCount.Source = "SELECT COUNT(P" + arrPrec + ") AS PrecCount" + arrPrec + " FROM dbo.TABLENAME WHERE (ISNULL(P" + arrPrec + ", -1) > 0)";
rsCount.CursorType = 0;
rsCount.CursorLocation = 2;
rsCount.LockType = 1;
rsCount.Open();
var rsCount_numRows = 0;
//Response.Write(rsCount.Source + "<br/>");//test
}//End loop
%>
<%
if (!rsCount.BOF || !rsCount.EOF) {//Then
var PrecCount1 = rsCount.Fields.Item("PrecCount1").Value;//<--LINE 24
}//End if
%>
...but I get the following error on the page:
ADODB.Fields error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
/clerk/electionresults_office_newest.asp, line 24

I'll keep messing around with it until I hopefully hear from a helpful and wonderful individual:) Thanks.

phpnovice
03-29-2005, 09:41 AM
Well, I'm not exactly sure what you're after, but how about this?

<%
var rsCount = Server.CreateObject("ADODB.Recordset");
rsCount.ActiveConnection = MM_strConn_STRING;
rsCount.CursorType = 0;
rsCount.CursorLocation = 2;
rsCount.LockType = 1;
//
var PrecCount = new Array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
var PrecCounter = 0;
var PrecAccum = 0;
//
for (i = 0; i <10; i++) {//Begin preccounter loop
rsCount.Source = "SELECT COUNT(P" + PrecCount[i] + ") AS PrecCount " +
"FROM dbo.TABLENAME " +
"WHERE (ISNULL(P" + PrecCount[i] + ", -1) > 0)";
rsCount.Open();
PrecCounter = rsCount.Fields.Item("PrecCount").Value;
if (PrecCounter != 0) { //Then
PrecCounter = PrecCounter / PrecCounter;
} //End if
PrecAccum = PrecAccum + PrecCounter;
rsCount.Close();
} //End loop
rsCount = null;
Response.Write("<strong>" + PrecAccum + "</strong>");
%>

kwilliams
03-30-2005, 10:26 AM
phpnovice wrote: Well, I'm not exactly sure what you're after, but how about this?...

I've actually made some progress in what I need by creating a looped array of values from 1 to 67, and then looping through the Recordset and script to assign values from the RS to a variable for manipulation & display. This is what I have:
<%
for (i=0;i<67;i++) {//Begin loop
var PrecNo = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,3 5,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67)
var arrPrec = PrecNo[i]
%>
<%
var rsCount = Server.CreateObject("ADODB.Recordset");
rsCount.ActiveConnection = MM_strConn_STRING;
rsCount.Source = "SELECT COUNT(P" + arrPrec + ") AS PrecCount" + arrPrec + " FROM dbo.TABLENAME WHERE (ISNULL(P" + arrPrec + ", -1) > 0)";
rsCount.CursorType = 0;
rsCount.CursorLocation = 2;
rsCount.LockType = 1;
rsCount.Open();
var rsCount_numRows = 0;
//Response.Write(rsCount.Source);//test
%>
<%
//Set variables
if (!rsCount.BOF || !rsCount.EOF) {//Then
var rsPrecCount = rsCount.Fields.Item("PrecCount" + arrPrec).Value;
//Response.Write(rsPrecCount)
if (rsPrecCount != 0) {//Then
var rsPrecCount = rsPrecCount/rsPrecCount;
}//End if
}
else {
var rsPrecCount = 0;
}//End if
Response.Write(arrPrec + ": " + rsPrecCount + "<br>");//test
//Response.Write(arrPrec);//test
}//End loop
%>
This script works great at what I need, and I can actually see the resulting values in a list provided by my Response.Write test:
1: 1
2: 1
3: 1
4: 0
5: 0...

...but my problem is in the assignment of the RS value to a variable (var rsPrecCount = rsCount.Fields.Item("PrecCount" + arrPrec).Value). Since this variable doesn't have a unique name, I can't add the values from the loop together into one main value. This is what I want to do:
var PrecCount = rsPrecCount1 + rsPrecCount2 + ...
but I can't figure out a way to assign the looped value to the variable name.

So if you or anyone else sees a way that I can make the variable name unique within a looped array, please let me know. Any & all help is greatly appreciated. Thanks for responding phpnovice!

phpnovice
03-30-2005, 11:00 AM
You don't need to do this:

var rsPrecCount = rsCount.Fields.Item("PrecCount" + arrPrec).Value;

That is what I included the following for in my original post:

PrecAccum = PrecAccum + PrecCounter;

The above statement could also be this:

PrecAccum += PrecCounter;

kwilliams
03-30-2005, 11:20 AM
Ok, I get what you're saying. I used your included code like this:
<%
var rsCount = Server.CreateObject("ADODB.Recordset");
rsCount.ActiveConnection = MM_strConn_STRING;
rsCount.Source = "SELECT COUNT(P" + arrPrec + ") AS PrecCount FROM dbo.TABLENAME WHERE (ISNULL(P" + arrPrec + ", -1) > 0)";
rsCount.CursorType = 0;
rsCount.CursorLocation = 2;
rsCount.LockType = 1;
rsCount.Open();
var rsCount_numRows = 0;
//Response.Write(rsCount.Source);//test
var PrecCounter = rsCount.Fields.Item("PrecCount").Value;
var PrecAccum = 0;
if (PrecCounter != 0) { //Then
var PrecCounter = PrecCounter / PrecCounter;
} //End if
var PrecAccum = PrecAccum + PrecCounter;
rsCount.Close();
} //End loop
rsCount = null;
Response.Write("<strong>" + PrecAccum + "</strong>");
%>

...but it resulted in 0. I think that it's because the last value from the RS is a 0, so it's using that value. I also don't exactly see how this code adds all of the looped values from PrecCounter, especially since PrecCounter is not a unique variable name for the RS. Any suggestions?

phpnovice
03-30-2005, 12:35 PM
Why don't you take the exact (entire) code I posted and run with that. For example... Your code seems to be missing the beginning of the loop.

kwilliams
03-30-2005, 12:56 PM
Ok, I copied & pasted your code into my page, and it worked great! I'm sorry to have not done that in the first place. I should rename myself ASPNovice. It can be wuite confusing for me when working with looped arrays and dynamic data together, but you've helped to clear my head. Anyway, thanks so much for your help. I really appreciate you helping me out so much!!! :)

phpnovice
03-30-2005, 03:13 PM
Ok, I copied & pasted your code into my page, and it worked great!
Cheers.