Karl Blessing
Programmer
I have this code which takes all the data from a Submited form (which was a treeview of documents and options), now that I have it doing the basic task of just creating the new URLs with the filters, I need some tips on how to shorten this code. it involves lots of For Loops, as well as numerous arrays.<br><br><FONT FACE=monospace><br><%@ Language=VBScript %><br><HTML><br><HEAD><br></HEAD><br><BODY><br><br><%<br>Dim FormStr<br>Dim FrmCol<br>Dim Number<br>Dim TripleAry<br>Dim HeadAry<br>Dim ValueAry<br>Dim AryData<br>Dim URLList<br><br>' FormStr has 1 fields, "Variable=Value" from the Form Posting<br>'<!-- Grab Parameters --><br>FormStr = Split(Request.Form, "&"<br>ReDim FrmCol(UBound(FormStr),2)<br><br>'FormCol now has 2 feilds "Variable", "Value"<br>'<!-- Grab Variable and Value --><br>for i = LBound(FrmCol) to UBound(FrmCol)<br> TmpAry = Split(FormStr(i),"="<br> FrmCol(i,0) = TmpAry(0)<br> FrmCol(i,1) = TmpAry(1)<br>next<br><br>'<!-- Grab number of Root Catagories(Heads) --><br>Number = 0<br>for j = LBound(FrmCol) to UBound(FrmCol)<br> if IsNumeric(FrmCol(j,0)) then<br> Number = Number + 1<br> end if<br>next<br><br>ReDim HeadAry(Number)<br>Redim ValueAry((UBound(FrmCol)-Number)-1, 3)<br><br>'HeadAry now has a single field, for the HeadIDs selected<br>'<!-- break down into Head-Variable-Value as well as create a Head Table --><br>Looper = 0<br>Looper2= 0<br>For j = LBound(FrmCol) to UBound(FrmCol)<br> if IsNumeric(FrmCol(j,0)) then<br> HeadAry(Looper) = FrmCol(j,0)<br> Looper = Looper + 1<br> else<br> rtn = Instr(1, FrmCol(j,0), "_"<br> if rtn <> 0 then<br> ValueAry(Looper2, 0) = Left(FrmCol(j,0), Rtn-1)<br> ValueAry(Looper2, 1) = Mid(FrmCol(j,0), rtn+1)<br> ValueAry(Looper2, 2) = FrmCol(j,1)<br> Looper2 = Looper2 + 1<br> end if<br> end if<br>next<br><br>'ValueAry now has 3 fields populated with all the HeadID, Variable, Value from the posting<br>'<!-- Check the number of variables that actually reside under a selected head --><br>'<!-- an option may have been chosen, but the root head hasnt been chosen --><br>Number = 0<br>for j = LBound(ValueAry) to UBound(ValueAry)<br> if checkhead(ValueAry(j,0)) then<br> Number = Number + 1<br> end if<br>next<br><br>Redim TripleAry(Number, 3)<br><br>'TripleAry will contain same format as VauleAry w/o the invalid rows<br>'<!-- Load new array with Variables that match the selected Heads --><br>Looper = 0<br>For j = LBound(ValueAry) to UBound(ValueAry)<br> if checkhead(ValueAry(j,0)) then<br> TripleAry(Looper,0) = ValueAry(j,0)<br> TripleAry(Looper,1) = ValueAry(j,1)<br> TripleAry(Looper,2) = ValueAry(j,2)<br> Looper = Looper + 1<br> end if<br>next<br><br>'<!-- Load URL Table into an Array --><br>Set Conn = Server.CreateObject("ADODB.Connection"<br>Set RS = Server.CreateObject("ADODB.Recordset"<br>Conn.Open "Tree", "sa", ""<br>RS.Open "Select * from HeadURL Order by ID, TOrder",Conn, 0,1<br>If not RS.EOF then<br> RS.MoveFirst<br> AryData = RS.GetRows<br> RS.Close<br> Set RS = nothing<br>end if<br>Conn.Close<br>Set Conn = nothing<br><br>Number = 0<br><br>'AryData now contains 2 fields (Field#, Row#)<br>'<!-- check to see how many URLs match the Head to get a proper count for new array --><br>for j = LBound(AryData,2) to UBound(AryData, 2)<br> for k = LBound(HeadAry) to UBound(HeadAry)<br> if HeadAry(k) = Cstr(AryData(0,j)) then<br> Number = Number + 1<br> end if<br> next<br>next<br><br>ReDim URLList(Number-1, 3)<br>Looper = 0<br><br>'<!-- Load Selected URLS into the New Array --><br>for j = LBound(AryData,2) to UBound(AryData,2)<br> for k = LBound(HeadAry) to UBound(HeadAry)<br> if HeadAry(k) = CStr(AryData(0,j)) then<br> URLList(Looper,0) = AryData(0,j)<br> URLList(Looper,1) = AryData(1,j)<br> URLList(Looper,2) = AryData(2,j)<br> Looper = Looper + 1<br> end if<br> next<br>next<br><br>'URLList now contains the HeadID-URL-Order<br>'<!-- Append the filters onto the selected URLs --><br>For j = LBound(URLList) to UBound(URLList)<br> URLList(j,1) = URLList(j,1) & ReturnTags(URLList(j,0))<br>next<br><br>'URLList now been modifed with a proper URL<br>'<!-- show final table, wit Head #, URL, Order --><br>Response.Write "<TABLE Border=1 align=center>" & vbcrlf<br>For j = Lbound(URLList) to UBound(URLList)<br> Response.Write vbtab & "<TR>" & vbcrlf<br> Response.Write vbtab & vbtab & "<TD>" & URLList(j,0) & "</TD>" & "<TD>" & URLList(j,1) & "</TD>" & "<TD>" & URLList(j,2) & "</TD>" & vbcrlf<br> Response.Write vbtab & "</TR>" & vbcrlf<br>next<br>Response.Write "</TABLE>" & vbcrlf<br><br>'<!-- Function to check to see if Filter has a chosen Head --><br>'<!-- this also helps filter out stuff like the SubMit button --><br>function checkhead(headpos)<br> checkhead = False<br> for i = LBound(HeadAry) to UBound(HeadAry)<br> if HeadAry(i) = headpos then<br> checkhead = True<br> exit for<br> end if<br> next<br>end function<br><br>'<!-- Returns the Filter Strings to be appended --><br>function ReturnTags(HeadID)<br> ReturnTags = ""<br> for i = LBound(TripleAry) to UBound(TripleAry)<br> if TripleAry(i,0) = Cstr(HeadID) then<br> if ReturnTags <> "" then<br> ReturnTags = ReturnTags & "&" & Server.URLEncode(TripleAry(i,1)) & "=" & Server.URLEncode(TripleAry(i,2))<br> else<br> ReturnTags = ReturnTags & "?" & Server.URLEncode(TripleAry(i,1)) & "=" & Server.URLEncode(TripleAry(i,2))<br> end if<br> end if<br> next<br>end function<br><br>Set FormStr = nothing<br>Set FrmCol = nothing<br>Set Looper2 = nothing<br>Set Looper = nothing<br>Set Number = nothing<br>Set TripleAry = nothing<br>Set HeadAry = nothing<br>Set ValueAry = nothing<br>Set AryData = nothing<br>Set URLList = nothing<br>%><br><br></BODY><br></HTML><br></font> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>