www.webdeveloper.com
Results 1 to 9 of 9

Thread: Server Side Problem!

  1. #1
    Join Date
    Feb 2006
    Posts
    9

    Server Side Problem!

    Hi,

    I need help on my simple problem urgently! How do you assign a value from a database to a variable in javascript? I ask this as I am having trouble with my short algorithm, which is supposed to go trhough all rows in a table and check the value of the order ID. The variable trackOrderNo is supposed to be assigned the first value of orderID (the first row in the database) before entering any loops.

    Then a check is made on the next row in the database, and if the value of orderID is the same as the current value of trackOrderNo then assign this orderID value to the variable trackOrderNo and move to the next row. However if this value of orderID is not the same as trackOrderNo then assign this value to trackOrderNo and add one to the variable total. THis process is repeated until the end of the table is reached.

    Code:
    rs.MoveFirst();
    var trackOrderNo = rs("orderID");
    var total = 1;
    Response.Write(trackOrderNo + "<br>");
    rs.MoveNext();
    
    while(!rs.EOF)
    {
    	if (trackOrderNo == rs("orderID"))
    	{
    		//total = total;
    		trackOrderNo = rs("orderID")
    		Response.Write("total = total" + "<br>");
    		Response.Write("Recordset value is: " + rs("orderID") + "<br>")
    		Response.Write("Track No is: " + trackOrderNo + "<br>" + "<br>")
    	}
    	else
    	{
    		total++;
    		trackOrderNo = rs("orderID")
    		Response.Write("total++" + "<br>");
    		Response.Write("Recordset value is: " + rs("orderID") + "<br>")
    		Response.Write("Track No is: " + trackOrderNo + "<br>" + "<br>")
    			
    		//Response.Write(total + "<br>");
    	}
    	rs.MoveNext();
    }
    I have just found out that by assigning something like trackOrderNo = rs("orderID") sets the variable to that value of database. Then if the value of rs("orderID") changes, trackOrderNo changes automatically to the same value as the new rs("orderID")!! Why is this doing this? Is there any way to stop this automatic value assigning?

    Thanks in advance,
    Harry

  2. #2
    Join Date
    Feb 2006
    Location
    Los Angeles, CA
    Posts
    105
    Create a start value, which would be your first trackOrderNo. Then for your counters, use mytrackOrderNo. I'm not entirely sure what you want to do with this though, and it would help if you clarified.
    Justice Erolin
    http://www.erolindesigns.com
    I can stimulate your <HEAD> and <BODY>.

  3. #3
    Join Date
    Feb 2006
    Posts
    9
    Hi, thanks ever so much for the reply.

    Bascially I want to assign the first value in the database for the given field (rs("orderID") into the variable trackOrderNo.

    So if orderID = 1 in the database this is obviously assigned to trackOrderNo. Then I want to go to the next row in the database and check to see whether this value is the same as the value in trackOrderNo. If they are the same, then trackOrderNo is assigned this second value in the database and the next row is accessed.

    If the two are not equal I want to increment the counter total and then set the value of trackOrderNo to this second database value.

    This whole process is repeated until the recordset end is reached.

    But the problem i'm having is when I assign the first value of the database to the varible trackOrderNo, I then do the rs.MoveNext which does the correct thing in going to the next row in the database but also changes the variable trackOrderNo to this new row value in the database.

    Code:
    var total = 1;
    var trackOrderNo;
    rs.MoveFirst();
    trackOrderNo = rs("orderID");
    var trackOrderNo2 = trackOrderNo;
    	
    Response.Write("Recordset Before enter loop value is: " + rs("orderID") + "<br>")
    Response.Write("Initial Value of trackOrderNo is the start value in the db table: " +trackOrderNo + "<br>");
    Response.Write("trackOrderNo Before enter loop value is: " + trackOrderNo + "<br>");
    	
    rs.MoveNext();
    	
    Response.Write("Recordset Before enter loop value after movenext: " + rs("orderID") + "<br>")
    Response.Write("Initial Value of trackOrderNo after movenext: " +trackOrderNo + "<br>");
    Response.Write("trackOrderNo Before enter loop after movenext: " + trackOrderNo + "<br>");
    I get the following output where the first value in the database is 2 for orderID and the second value for orderID is 4:

    Code:
    Recordset Before enter loop value is: 2
    Initial Value of trackOrderNo is the start value in the db table: 2
    trackOrderNo Before enter loop value is: 2
    
    Recordset Before enter loop value after movenext: 4
    Initial Value of trackOrderNo after movenext: 4
    trackOrderNo Before enter loop after movenext: 4

  4. #4
    Join Date
    Feb 2006
    Location
    Los Angeles, CA
    Posts
    105
    Is this function only accessed once, or also during a loop? I would assume that you would have more than set of outputs for each time you use rs.MoveNext()

    Why are you using JavaScript? In ASP, you can use a Do While loop. Not sure how that applies to JavaScript.
    Justice Erolin
    http://www.erolindesigns.com
    I can stimulate your <HEAD> and <BODY>.

  5. #5
    Join Date
    Feb 2006
    Posts
    9
    Its accessed only once, the loop is ONLY entered once trackOrderNo has been assigned the value of the first database value. Someone has suggested I use ASP instead (VBScript) but when I do rs.MoveNext in this, will the same problem occur that I am having now?

    Code:
    rs.MoveFirst
    Dim trackOrderNo
    trackOrderNo = rs("orderID")
    Dim total
    total = 1
    Response.Write(trackOrderNo & "<br>")
    rs.MoveNext
    while Not rs.EOF
    	if trackOrderNo <> rs("orderID") 'total = total;
    		trackOrderNo = rs("orderID")
    		Response.Write("total = total<br>")
    		Response.Write("Recordset value is: " & rs("orderID") & "<br>")
    		Response.Write("Track No is: " & trackOrderNo & "<br><br>")
    	else
    	total = total + 1
    		trackOrderNo = rs("orderID")
    		Response.Write(total + 1 & "<br>")
    		Response.Write("Recordset value is: " & rs("orderID") & "<br>")
    		Response.Write("Track No is: " & trackOrderNo & "<br><br>")
    			
    		'Response.Write(total + "<br>")
            end if
    	rs.MoveNext
    Wend

  6. #6
    Join Date
    Feb 2006
    Location
    Los Angeles, CA
    Posts
    105
    Code:
    rs.MoveFirst
    Dim trackOrderNo, myTrackOrder
    trackOrderNo = rs("orderID")
    Dim total
    total = 1
    Response.Write(trackOrderNo & "<br>")
    rs.MoveNext
    while Not rs.EOF
    	if trackOrderNo <> rs("orderID") 'total = total;
    		trackOrderNo = rs("orderID")
    		Response.Write("total = total<br>")
    		Response.Write("Recordset value is: " & rs("orderID") & "<br>")
    		Response.Write("Track No is: " & trackOrderNo & "<br><br>")
    	else
    	total = total + 1
    		mytrackOrderNo = rs("orderID")
    		Response.Write(total + 1 & "<br>")
    		Response.Write("Recordset value is: " & rs("orderID") & "<br>")
    		Response.Write("Track No is: " & mytrackOrderNo & "<br><br>")
    			
    		'Response.Write(total + "<br>")
            end if
    	rs.MoveNext
    Wend
    Try this.
    Justice Erolin
    http://www.erolindesigns.com
    I can stimulate your <HEAD> and <BODY>.

  7. #7
    Join Date
    Feb 2006
    Posts
    9
    With this code, when the first statement is reached, if the two are not equal it will never go in this loop. But I want to compare every row so therefore I cant have 2 variables. I dont no how to do vbscript lol!! Have to change all db connections and everything, any help on this would be appreciated!

  8. #8
    Join Date
    Feb 2006
    Location
    Los Angeles, CA
    Posts
    105
    Have you considered doing this server-side with ASP.NET?
    Justice Erolin
    http://www.erolindesigns.com
    I can stimulate your <HEAD> and <BODY>.

  9. #9
    Join Date
    Feb 2006
    Posts
    9
    No to be honest, I am doing this project and have very little time (approx 3 weeks). Is it beneficial for me to pick up, I have very little time though.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles