Easy 500 points....code explaination

Come some one please explain the code below, would really appreciate if it could be done in a little more detail then just one line of comment......thanks in advance.


<%@LANGUAGE="VBSCRIPT"%>
<%

Dim objConn

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "dsn=Oracle;uid=00411343;pwd=090180"

If Request.Form("submit") <> "Update Stock" Then

Dim strSQL1, strSQL2, objRs1, objRs2, lngVotesCast

strSQL1="select * from ec_tunes order by artist_name, tune_name"
strSQL2="SELECT sum(ec_tunes.tune_copies) AS totalincrease FROM ec_tunes"

Set objRs1 = objConn.Execute(strSQL1)
Set objRs2 = objConn.Execute(strSQL2)

If (objRs2.BOF) Then
     lngVotesCast = 0
Else
     lngVotesCast=CLng(objRs2("totalincrease"))
End If

totalTunes = 0

Do While Not objRs1.EOF

     totalTunes = totalTunes + 1

     tuneOutput = tuneOutput & "<tr>"
       tuneOutput = tuneOutput & "<td>" & objRs1("tune_id") & "</td>"
     tuneOutput = tuneOutput & "<td>" & objRs1("artist_name") & "</td>"
     tuneOutput = tuneOutput & "<td>" & objRs1("tune_name") & "</td>"
     tuneOutput = tuneOutput & "<td><input type='text' name='tune" & objRs1("tune_id") & "' value='" & objRs1("tune_copies") & "' /></td>"
     tuneOutput = tuneOutput & "<td align='center'><a href='updateStock.asp?id=" & objRs1("tune_id") & "'>Edit</a></td>"
     tuneOutput = tuneOutput & "<td align='center'><input type='checkbox' name='deleteTune" & objRs1("tune_id") & "' value='ON' /></td>"
     tuneOutput = tuneOutput & "<tr>"
     
tuneIDs = tuneIDs & objRs1("tune_id") & ","
     
     

objRs1.MoveNext
Loop

tuneIDs = Mid(tuneIDs, 1, Len(tuneIDs) - 1)

objRs1.Close
Set objRs1=Nothing

objRs2.Close
Set objRs2=Nothing

objConn.Close
Set objConn=Nothing

%>

<HTML>
<HEAD>
<TITLE>Stock update</TITLE>
<style type="text/css">

.style1 {color: #CCCCCC}

</style>
</HEAD>
<BODY bgcolor="#CCCCCC">
<TABLE BORDER="0">
<TR>
<TD ALIGN=LEFT><H1>Administrator Stock Check</H1></TD>
<TD ALIGN=RIGHT>Changes made: <%=lngVotesCast%><BR>Last update: <%=now()%></TD>

</TR>
</TABLE>

<FORM ACTION="<%= Request.ServerVariables("URL") %>" method="POST" name="frmVotes">
<input type="hidden" name="totalTunes" value="<%= totalTunes %>">
<input type="hidden" name="tunesIDs" value="<%= tuneIDs %>">
<table width="600" border="2" bgcolor="#FFFFFF">
  <tr>
    <td bgcolor="#FFFFFF">Tune ID</td>
    <td bgcolor="#FFFFFF">Artist</td>
    <td bgcolor="#FFFFFF">Tune</td>
    <td bgcolor="#FFFFFF">Quantity</td>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td align="center" bgcolor="#FFFFFF">Delete Tune?</td>
  </tr>
  <%= tuneOutput %>
  <tr>
    <td align="CENTER" colspan="5"><input type="submit" name="submit" value="Update Stock" /></td>
  </tr>
</table>
<span class="style1"></span>
</FORM>
</BODY>
</HTML>
<%
Else
noTunesToUpdate = Request.Form("totalTunes")

tunesIDs = split(Request.Form("tunesIDs"), ",")
     
For i = 0 to Ubound(tunesIDs)
     
currTuneID = tunesIDs(i)
     
currTuneValue = Request.Form("tune" & tunesIDs(i))
deleteCurrTune = Request.Form("deleteTune" & tunesIDs(i))
     
If deleteCurrTune = "ON" Then
     
updateTuneStock = "DELETE FROM ec_tunes WHERE tune_id = " & currTuneID    
     
Else
     
updateTuneStock = "UPDATE ec_tunes SET tune_copies = " & currTuneValue & " WHERE tune_id = " & currTuneID
     
End If
     
objConn.Execute updateTuneStock
   
Next

objConn.Close
Set objConn=Nothing

End If
%>
sid1980Asked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
It a simple loop.  i is just a variable that is being used as a counter, tunesIDs is the array containing all of the possible tune_id values. The UBound part tells the loop how many elements are in the array so it knows when to stop looping.

So, each time through the loop, "i" will hold the index position of the current array element. You can then access the value of the current array element by using:

    tunesIDs(i)       '// tunesIDs is the array, and "i" is the index


Hope this helps.
0
 
ZvonkoSystems architectCommented:
Look for this line:
<input type="submit" name="submit" value="Update Stock" />

That form input element decides which brunch in your ASP script is taken.
On initial page load is that input elemt empty, and therefore is the first path of the page executed:
If Request.Form("submit") <> "Update Stock" Then

By the way, the submit button name "submit" is poore choice because it conflicts with JavaScript submit() method of the form on the browser side if ever needed in the future. Beter choice would be "Submit"

Ok, now from the beginning:
First two statements open the database connection to the Oracle database DSN (DataSourceName): Oracle

Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "dsn=Oracle;uid=00411343;pwd=090180"

If the page was opened without form submit, then all records are fetched by the first query string. The second query string is for a total sumarization of all quantitie values for all records in the  ec_tunes column tune_copies:

strSQL1="select * from ec_tunes order by artist_name, tune_name"
strSQL2="SELECT sum(ec_tunes.tune_copies) AS totalincrease FROM ec_tunes"

Set objRs1 = objConn.Execute(strSQL1)
Set objRs2 = objConn.Execute(strSQL2)


That was the first part of the explanation. Tell me now if I should go on in that way or you would like to ask questions to special topics.

0
 
sid1980Author Commented:
i understand the top bit of the code i want to understand the rest of the code which is below....thanks

Do While Not objRs1.EOF

     totalTunes = totalTunes + 1

     tuneOutput = tuneOutput & "<tr>"
      tuneOutput = tuneOutput & "<td>" & objRs1("tune_id") & "</td>"
     tuneOutput = tuneOutput & "<td>" & objRs1("artist_name") & "</td>"
     tuneOutput = tuneOutput & "<td>" & objRs1("tune_name") & "</td>"
     tuneOutput = tuneOutput & "<td><input type='text' name='tune" & objRs1("tune_id") & "' value='" & objRs1("tune_copies") & "' /></td>"
     tuneOutput = tuneOutput & "<td align='center'><a href='updateStock.asp?id=" & objRs1("tune_id") & "'>Edit</a></td>"
     tuneOutput = tuneOutput & "<td align='center'><input type='checkbox' name='deleteTune" & objRs1("tune_id") & "' value='ON' /></td>"
     tuneOutput = tuneOutput & "<tr>"
     
tuneIDs = tuneIDs & objRs1("tune_id") & ","
     
     

objRs1.MoveNext
Loop

tuneIDs = Mid(tuneIDs, 1, Len(tuneIDs) - 1)

objRs1.Close
Set objRs1=Nothing

objRs2.Close
Set objRs2=Nothing

objConn.Close
Set objConn=Nothing

%>

<HTML>
<HEAD>
<TITLE>Stock update</TITLE>
<style type="text/css">

.style1 {color: #CCCCCC}

</style>
</HEAD>
<BODY bgcolor="#CCCCCC">
<TABLE BORDER="0">
<TR>
<TD ALIGN=LEFT><H1>Administrator Stock Check</H1></TD>
<TD ALIGN=RIGHT>Changes made: <%=lngVotesCast%><BR>Last update: <%=now()%></TD>

</TR>
</TABLE>

<FORM ACTION="<%= Request.ServerVariables("URL") %>" method="POST" name="frmVotes">
<input type="hidden" name="totalTunes" value="<%= totalTunes %>">
<input type="hidden" name="tunesIDs" value="<%= tuneIDs %>">
<table width="600" border="2" bgcolor="#FFFFFF">
  <tr>
    <td bgcolor="#FFFFFF">Tune ID</td>
    <td bgcolor="#FFFFFF">Artist</td>
    <td bgcolor="#FFFFFF">Tune</td>
    <td bgcolor="#FFFFFF">Quantity</td>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td align="center" bgcolor="#FFFFFF">Delete Tune?</td>
  </tr>
  <%= tuneOutput %>
  <tr>
    <td align="CENTER" colspan="5"><input type="submit" name="submit" value="Update Stock" /></td>
  </tr>
</table>
<span class="style1"></span>
</FORM>
</BODY>
</HTML>
<%
Else
noTunesToUpdate = Request.Form("totalTunes")

tunesIDs = split(Request.Form("tunesIDs"), ",")
     
For i = 0 to Ubound(tunesIDs)
     
currTuneID = tunesIDs(i)
     
currTuneValue = Request.Form("tune" & tunesIDs(i))
deleteCurrTune = Request.Form("deleteTune" & tunesIDs(i))
     
If deleteCurrTune = "ON" Then
     
updateTuneStock = "DELETE FROM ec_tunes WHERE tune_id = " & currTuneID    
     
Else
     
updateTuneStock = "UPDATE ec_tunes SET tune_copies = " & currTuneValue & " WHERE tune_id = " & currTuneID
     
End If
     
objConn.Execute updateTuneStock
   
Next

objConn.Close
Set objConn=Nothing

End If
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sid1980Author Commented:
parts of the code is trying to work out the lenght of charactors in a string to be able to delete more then one record from the page at a single time using check boxes, that is the parts that needs to be explained but the update parts of the code need to be explained separately. so i can understand which part of the code is doing what......P.s i don't need a explaination on the display out put.
0
 
Carl TawnSystems and Integration DeveloperCommented:
The code is actually doing more work than it needs to.

I'll try and explain the main parts of the code, anything thats fairly irrelevant or obvious i'll skip.

The <form> in this page actually submits to itself, so the statement:

    If Request.Form("submit") <> "Update Stock" Then

Is simply checking to see if the form has been submitted. If it hasn't been submitted then the page simply gathers a bunch of records from the database and creates a HTML string that will be used as the body of a HTML table. Each row in the table gets a checkbox named "deleteTune" plus the "tune_id" of the current record.

So, if the "tune_id" of the current record was 5, then the checkbox would be named "deleteTune5". This section of code is also building a comma-seperated list of all of the "tune_id" values in the line - tuneIDs = tuneIDs & objRs1("tune_id") & ",". This comma-seperated list is then held in a hidden field in the <form>.

If the form has been submitted then the page simply deletes the selected records from the database. To explain this i'll simply add comments to the code in the "Else" part of the main "If" statement.

    noTunesToUpdate = Request.Form("totalTunes")         '// This line seems redundant because noTunesToUpdate doesn't appear to be used anywhere

    '// This line create an array of tune_id values. This is the comma seperated list of tune_id values created earlier.
    tunesIDs = split(Request.Form("tunesIDs"), ",")
     
    '// The code is now going to loop through each tune_id in the array we just created
    For i = 0 to Ubound(tunesIDs)
     
        currTuneID = tunesIDs(i)       '// Hold the current array element in a variable
     
        currTuneValue = Request.Form("tune" & tunesIDs(i))                      '// Gets the value from the text field associated with this tune_id
        deleteCurrTune = Request.Form("deleteTune" & tunesIDs(i))           '// Gets the value of the checkbox associated with this tune_id

        '// The code is now going to setup a DELETE or UPDATE SQL statement based on whether the checkbox was checked or not.  If it was, the record will be
        '// deleted, otherwise it will be updated.    

        If deleteCurrTune = "ON" Then
            updateTuneStock = "DELETE FROM ec_tunes WHERE tune_id = " & currTuneID    
        Else  
            updateTuneStock = "UPDATE ec_tunes SET tune_copies = " & currTuneValue & " WHERE tune_id = " & currTuneID
        End If
     
        '// Now we execute the SQL statement
        objConn.Execute updateTuneStock
   
    Next

Like I said at the start, this code seems a little bit overkill and could be simplified a little.

Hope this helps.
0
 
sid1980Author Commented:
still dont really understand what this means

 For i = 0 to Ubound(tunesIDs)

especially the  For i =
0
 
ZvonkoSystems architectCommented:
This two statements work together:

    tunesIDs = split(Request.Form("tunesIDs"), ",")
    For i = 0 to Ubound(tunesIDs)


...
    Next

The first line splits the value sent from the hidden input field on the form with the name: tunesIDs
That hidden field value was composed by last  page composion.
The value is a series of id separated by comas.
So does that split statement split this long single string value to an array of id values.
The id values are returned as elements of an array into variable with the name tunesIDs
The For loop steps trough all elements of that array of ids.
It steps with loop variable i initialized to 1 and runs to the upper bound of the array size. The method to lookup the upper boundery of an array is Ubound() and returns the count of elements of an array.

0
 
ZvonkoSystems architectCommented:
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.