?
Solved

Easy 500 points....code explaination

Posted on 2005-04-17
8
Medium Priority
?
322 Views
Last Modified: 2012-06-21
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
%>
0
Comment
Question by:sid1980
  • 3
  • 3
  • 2
8 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 13802587
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
 

Author Comment

by:sid1980
ID: 13802596
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
 

Author Comment

by:sid1980
ID: 13802610
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

Expert Comment

by:Carl Tawn
ID: 13802724
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
 

Author Comment

by:sid1980
ID: 13802896
still dont really understand what this means

 For i = 0 to Ubound(tunesIDs)

especially the  For i =
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1200 total points
ID: 13802947
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
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 800 total points
ID: 13802958
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 13805834
Thanks.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question