Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • December 06, 2016, 01:52:13 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: (Train Wreck) Multidimensional Dynamic Array in ASP  (Read 3312 times)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
(Train Wreck) Multidimensional Dynamic Array in ASP
« on: December 06, 2010, 03:52:02 PM »
Greetings
    I'm currently (trying not to scream...) working on an office project that was done in ASP ... I've come to enjoy ASP about as much as I enjoy being repeetedly stabbed with miscellanous randomly sharpened objects.

Anyhow...

Part ordering interface, takes parts from multiple vendors, and compiles them into seperate orders for each vendor. Hence the need for a multidimensional array - which has to be dynamic, because the parts list is ever changing.

I cannot for the life of me find a tutorial on working with multidimensional dynamic arrays that makes any sense/works/doesn't try to trivialize the insane amount of dificulty there (apparently) is in doing this with ASP. Christ, if I was working in C++ I'd have been done with this hours ago.

Here is a stripped down (non-working) example of what I'm trying to do. It currently is just trying to dump the array(s)(s) back into the page via response.write to avoid listing the SQL query stuff and keep the example simple:
Code: ASP [Select]
  1. <%
  2. Dim ar_LAPN, iCount
  3. Dim ar_vCode(), ar_vFName(), ar_vActive(), iVends
  4. ar_LAPN = Split(Request.Form("LAPN"), ",")
  5. iCount  = Request.Form("iCount")
  6.  
  7. Dim ar_nVend00(10, 1000), ar_pVend00(10, 1000)
  8.  
  9. Dim MyConn, MyQuery, mRS '-----> SQL Query Variables
  10. set MyConn = Server.CreateObject("ADODB.Connection")
  11.         MyConn.Open(MySQL)
  12.  
  13. MyQuery = "SELECT VendorCode, VendorFName, Active, BorderColor, FontClass FROM VendorNames"
  14. Set mRS = MyConn.Execute(MyQuery)
  15. If mRS.BOF And mRS.EOF Then
  16.     Response.Write("MySQL Query Failed! " & MyQuery)
  17.     Response.End
  18. Else
  19.         iVends = 0
  20.         mRS.MoveFirst
  21.         WHILE NOT mRS.EOF
  22.                 ReDim Preserve ar_vCode(iVends + 1)
  23.                 ReDim Preserve ar_vFName(iVends + 1)
  24.                 ar_vCode(iVends)        = mRS("VendorCode")
  25.                 ar_vFName(iVends)       = mRS("VendorFName")
  26.                 ar_vActive(iVends)      = mRS("Active") '--+++--> Add Column for Vendor X Only if They're Active
  27.                
  28.                 if ar_vActive(iVends) Then '--------------------------------------+++-->
  29. '                       ReDim Preserve ar_nVend00(iVends + 1)
  30. '                       ReDim Preserve ar_pVend00(iVends + 1)
  31.                         ar_nVend00(iVends, 0) = Split(Request.Form("nVend00" & iVends), ",")
  32.                         ar_pVend00(iVends, 0) = Split(Request.Form("pVend00" & iVends), ",")
  33.                 end if
  34.                
  35.                 iVends = iVends + 1
  36.                 mRS.MoveNext
  37.         WEND
  38.         iVends = iVends - 1
  39. End If
  40.  
  41.  
  42.  
  43. for i=0 to iCount
  44.         response.write("LAPN: " & ar_LAPN(i) & " ")
  45.         for iV=0 to iVends
  46.                 response.write(" Qty: " & ar_nVend00(iVends, iCount) & " * Price: " & ar_pVend00(iVends, iCount) & " | ")
  47.         next
  48.         response.write("Vends-break;<br>")
  49. next

While (this version of) the code runs without error there are no values output when run even tho all data pulled from the previous page is correct.

Note: this was a working production system before I started working on making it a bit more flexible/robust - I'm trying to get away from hardcoding the vendors.

Yes, I am working on (torching...) a Lab Clone of the production system.


Edit: reworked output code to make more sense out of what I'm doing - It was rather cryptic in retrospect.
« Last Edit: December 06, 2010, 05:22:30 PM by Stoic Joker »

timns

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,211
    • View Profile
    • Donate to Member
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #1 on: December 06, 2010, 04:40:06 PM »
Shouldn't these 2 lines use iCount too?
(You're indexing using iCount later on)
         ar_nVend00(iVends, 0) = Split(Request.Form("nVend00" & iVends), ",")
         ar_pVend00(iVends, 0) = Split(Request.Form("pVend00" & iVends), ",")


Also, your loops are one step too big - shouldn't they stop at iCount-1 and iVends-1?

for i=0 to iCount-1
for iV=0 to iVends-1
response.write(ar_nVend00(iVends, iCount) & "Vend ->")
next
response.write("br<br>")
« Last Edit: December 06, 2010, 04:44:21 PM by timns »

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #2 on: December 06, 2010, 05:45:01 PM »
Shouldn't these 2 lines use iCount too?
(You're indexing using iCount later on)
         ar_nVend00(iVends, 0) = Split(Request.Form("nVend00" & iVends), ",")
         ar_pVend00(iVends, 0) = Split(Request.Form("pVend00" & iVends), ",")

You would think wouldn't you (I did), but it seems that Split(...) creates the "array" part of the array auto-magically. So a single dimension array is declared as just a plain dim (i.e. Dim myArray) and then the split does the rest.

myArray - Split(...)

Now it's used normally as myArray(x) ... This I've used elsewhere in the code so I know it works. One of the tutorials I ran across said... that to populate a multidimensional array you use myArray(x, 0) because Split would then begin populating dimension 2 from position zero. *Shrug* ...I couldn't find a hole in the reasoning ... So I gave it a shot.

I tried using iCount there just now and it results in a type mismatch error. :(

Also, your loops are one step too big - shouldn't they stop at iCount-1 and iVends-1?

for i=0 to iCount-1
for iV=0 to iVends-1
response.write(ar_nVend00(iVends, iCount) & "Vend ->")
next
response.write("br<br>")

It does appear that way, but iVends is decremented accordingly at the end of the while loop and iCount gets the same treatment on the previous page before being handed to this one.

Correction: iVends would have been correct if I was incrementing it on the right side of the if statement...  :wallbash: ...Fixed now, rest of code still broken :) (..shit)

I touched up the code above so the output string makes a bit more sense of what I'm doing (It was a might cryptic)


Thank you! (I'm willing to try anything at this point)
« Last Edit: December 06, 2010, 06:15:45 PM by Stoic Joker »

timns

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,211
    • View Profile
    • Donate to Member
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #3 on: December 06, 2010, 07:08:58 PM »
So you only have a maximum of 10 vendors? Those arrays (10,1000) may be the wrong way around? (1000,10)?

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #4 on: December 06, 2010, 09:50:44 PM »
Actually there are currently 6 vendors and about 400 parts (I'm trying to leave room for expansion), we're dealing with a lot of different parts that only a few people handle.

The array defined as (10, 1000) is actually a static array that is known to be large enough (6/400) for the current data set. I'd hoped to go fully dynamic but (only the right most dimension of the array can be redefined) as usual ASP sucks to much to do anything fancy with. So I'm resigning myself to using static arrays (unless a cheat can be found).

Christ, I'd rather be gargling battery acid...

On a brighter note, one the tutorials I came across listed a work-around for ASP's short-comings, in the process of explaining why what I'd wanted to do was friggin impossible. So... Now I have a semi working 2dim array - and by working I mean it runs with out errors or crashing. Unfortunately it also runs without any valid output ... It just iterates garbage. *Joy*

So here is the new and "improved" still totally borked code:
Code: ASP [Select]
  1. <!--#include virtual="inc/dbConn.inc"-->
  2. <%
  3. Response.Buffer = False '--+++--> HOLY SHIT!!! (that helped)
  4.  
  5. Dim ar_LAPN, iCount
  6. Dim ar_vCode(10), ar_vFName(10), ar_vActive(10), iVends
  7. ar_LAPN = Split(Request.Form("LAPN"), ",")
  8. iCount  = Request.Form("iCount")
  9.  
  10. Dim ar_nVend00, ar_pVend00
  11. ReDim ar_nVend00(10)
  12. ReDim ar_pVend00(10)
  13.  
  14. Dim MyConn, MyQuery, mRS '-----> SQL Query Variables
  15. set MyConn = Server.CreateObject("ADODB.Connection")
  16.         MyConn.Open(MySQL)
  17.  
  18. MyQuery = "SELECT VendorCode, VendorFName, Active, BorderColor, FontClass FROM VendorNames"
  19. Set mRS = MyConn.Execute(MyQuery)
  20. If mRS.BOF And mRS.EOF Then
  21.     Response.Write("MySQL Query Failed! " & MyQuery)
  22.     Response.End
  23. Else
  24.         iVends = 0
  25.         mRS.MoveFirst
  26.         WHILE NOT mRS.EOF
  27.                 ar_vActive(iVends)      = mRS("Active") '--+++--> Add Column for Vendor X Only if They're Active
  28.                 ar_vCode(iVends)        = mRS("VendorCode")
  29.                 ar_vFName(iVends)       = mRS("VendorFName")
  30.                 if ar_vActive(iVends) Then '--------------------------------------+++-->
  31.                         response.write("Vendor: " & ar_vFName(iVends) & " Is Active: " & ar_vActive(iVends) & "<br>")
  32. '                       ar_nVend00(iVends, 0) = Split(Request.Form("nVend00" & iVends), ",")
  33. '                       ar_pVend00(iVends, 0) = Split(Request.Form("pVend00" & iVends), ",")
  34.                         ar_nVend00(iVends) = Request.Form("nVend00" & iVends)
  35.                         ar_pVend00(iVends) = Request.Form("pVend00" & iVends)
  36.                        
  37. '                       response.write(ar_nVend00(iVends) & "<br>")
  38. '                       response.write(ar_pVend00(iVends) & "<br>")
  39.                 else
  40.                         response.write(ar_vFName(iVends) & " Vendor Number: " & iVends & " is NOT Active!<br>")
  41.                 end if
  42.                 iVends = iVends + 1 '--+++--> This MUST be Here so the Vend00x Number Matches the db Info.
  43.                 mRS.MoveNext
  44.         WEND
  45. '       ReDim Preserve ar_vActive(iVends)
  46. '       ReDim Preserve ar_vFName(iVends)
  47. '       ReDim Preserve ar_vCode(iVends)
  48.         iVends = iVends - 1
  49. End If
  50.  
  51. response.write("<br><br>")
  52.  
  53. for i=0 to iCount
  54.  
  55.  
  56.                         MyQuery = "SELECT Printer, Description FROM Inventory WHERE LAPN = '" & ar_LAPN(i) & "'"
  57.                         Set mRS = MyConn.Execute(MyQuery)
  58.                         Printer = mRS("Printer")
  59.                         Description = mRS("Description")
  60.  
  61.  
  62.         response.write("LAPN: " & ar_LAPN(i) & " :-: " & Description & "<br>")
  63.         for iV=0 to iVends
  64.                 Dim ar_nV, ar_pV
  65.                
  66.                 if ar_vActive(iV) Then '--------------------------------------+++-->
  67. '                       response.write("<br>==========================================================================================<br>")
  68. '                       response.write(ar_nVend00(iV) & "<br>")
  69. '                       response.write(ar_pVend00(iV) & "<br>")
  70. '                       response.write("==========================================================================================<br>")
  71.                 ar_nV = Split(ar_nVend00(iV), ",")
  72.                 ar_pV = Split(ar_pVend00(iV), ",")
  73. '               response.write(" Qty: " & ar_nVend00(iVends, iCount) & " * Price: " & ar_pVend00(iVends, iCount) & " | ")
  74.                 response.write("Vendor: " & ar_vFName(iV) & " Qty: " & ar_nV(iCount) & " * Price: " & ar_pV(iCount) & "<br>")
  75.                 else
  76.                         response.write("==========================================================================================<br>")
  77.                         response.write("Vendor: " & ar_vFName(iV) & " - IS NOT ACTIVE!!!")
  78.                         response.write("<br>==========================================================================================<br>")
  79.                 end if
  80.         next
  81.         response.write("End of LAPN: " & ar_LAPN(i) & " Vends-break;<br><br>")
  82. next
  83. %>

Thank you!

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,296
    • View Profile
    • www.StoicJoker.com
    • Donate to Member
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #5 on: December 06, 2010, 10:55:11 PM »
Okay, looks like I get to answer my own question (sort of/thanks timns) Here's the currently finally working code:
Code: ASP [Select]
  1. <!--#include virtual="inc/dbConn.inc"-->
  2. <%
  3. '--+++--> Orders_ReceiveArray.asp
  4. Response.Buffer = False '--+++--> HOLY SHIT!!!
  5.  
  6. Dim ar_LAPN, iCount
  7. Dim ar_vCode(10), ar_vFName(10), ar_vActive(10), iVends
  8. ar_LAPN = Split(Request.Form("LAPN"), ",")
  9. iCount  = Request.Form("iCount")
  10.  
  11. Dim ar_nVend00, ar_pVend00
  12. ReDim ar_nVend00(10)
  13. ReDim ar_pVend00(10)
  14.  
  15. Dim MyConn, MyQuery, mRS '-----> SQL Query Variables
  16. set MyConn = Server.CreateObject("ADODB.Connection")
  17.         MyConn.Open(MySQL)
  18.  
  19. MyQuery = "SELECT VendorCode, VendorFName, Active, BorderColor, FontClass FROM VendorNames"
  20. Set mRS = MyConn.Execute(MyQuery)
  21. If mRS.BOF And mRS.EOF Then
  22.     Response.Write("MySQL Query Failed! " & MyQuery)
  23.     Response.End
  24. Else
  25.         iVends = 0
  26.         mRS.MoveFirst
  27.         WHILE NOT mRS.EOF
  28.                 ar_vActive(iVends)      = mRS("Active") '--+++--> Add Column for Vendor X Only if They're Active
  29.                 ar_vCode(iVends)        = mRS("VendorCode")
  30.                 ar_vFName(iVends)       = mRS("VendorFName")
  31.                 if ar_vActive(iVends) Then '--------------------------------------+++-->
  32.                         ar_nVend00(iVends) = Request.Form("nVend00" & iVends)
  33.                         ar_pVend00(iVends) = Request.Form("pVend00" & iVends)
  34.                 end if
  35.                 iVends = iVends + 1 '--+++--> This MUST be Here so the Vend00x Number Matches the db Info.
  36.                 mRS.MoveNext
  37.         WEND
  38.         iVends = iVends - 1
  39. End If
  40.  
  41. '============== MySQL TimeStamp Functions next 3
  42. function mysqltimestamp(aspts)
  43.         dim thetime,thedate
  44.         aspts=cstr(aspts)
  45.         thedate=left(aspts,instr(aspts," ")-1)
  46.         thetime=right(aspts,len(aspts)-instr(aspts," "))
  47.         mysqltimestamp=mysqldate(thedate)&" "&mysqltime(thetime)
  48. end function
  49.  
  50. function mysqlDate(d)
  51.         dim strNewDate
  52.         strNewDate = year( d ) & "-" & month( d ) & "-" & day(d)
  53.         mysqlDate = strNewDate
  54. end function
  55.  
  56. function mysqlTime(t)
  57.  dim strSuffix, arTime, i, x
  58.  t = trim( Lcase( t ) )
  59.  if inStr( t, "pm" ) > 0 OR inStr( t, "am" ) > 0 then
  60.   strSuffix = right( t, 2 )
  61.   t = left( t, inStr( t, strSuffix ) -2 )
  62.   t = trim( t )
  63.  end if
  64.  for i = 1 to len( t )
  65.   x = mid( t, i, 1 )
  66.   if not isNumeric( x ) and x <> ":" then t = replace( t, x, "" )
  67.  next
  68.  arTime = split( t, ":" )
  69.  t = ""
  70.  for i = 0 to 2
  71.   if uBound( arTime ) < i then redim preserve arTime( i )
  72.   if i = 0 then
  73.     if strSuffix = "pm" and cInt( arTime( i ) ) < 12 then
  74.      arTime( i ) = cInt( arTime( i ) ) + 12
  75.     end if
  76.   end if
  77.   do until len( arTime( i ) ) = 2
  78.    arTime( i ) = "0" & arTime( i )
  79.   loop
  80.   t = t & arTime( i )
  81.   if i < 2 then t = t & ":"
  82.  next
  83.  arTime = null
  84.  if dir = 2 then t = t & " " & strSuffix
  85.  mysqlTime = t
  86. end function
  87. '============== END MySQL TimeStamp Functions
  88. TimeDate = mysqltimestamp(Now)
  89. %>
  90. Production Parts Order<br>
  91. <%
  92.  
  93.  
  94. for iV=0 to iVends
  95.         Dim ar_nV, ar_pV
  96.        
  97.         if ar_vActive(iV) Then '------( IF Vendor X IS Active )-------+++-->
  98.                 ar_nV = Split(ar_nVend00(iV), ",")
  99.                 ar_pV = Split(ar_pVend00(iV), ",")
  100.                
  101.                 for i=0 to iCount
  102.                         if ar_nV(i) > 0 Then '--+++--> Create Entry Only IF the Part has a Quantity
  103.                                 MyQuery = "SELECT Printer, Description FROM Inventory WHERE LAPN = '" & ar_LAPN(i) & "'"
  104.                                 Set mRS = MyConn.Execute(MyQuery)
  105.                                 Printer = mRS("Printer")
  106.                                 Description = mRS("Description")
  107.                                
  108.                                 MyQuery = "INSERT INTO OrderHistory (LAPN, Printer, Description, Quantity, PriceEach, Vendor, timeStamp) " &_
  109.                                                                         "VALUES ('" & ar_LAPN(i) & "', '" & Printer & "', '" & Description & "', '" & ar_nV(i) &_
  110.                                                                         "', '" & ar_pV(i) & "', '" & ar_vFName(iV) & "', '" & TimeDate & "')"
  111.                                 MyConn.Execute(MyQuery)
  112. '                               Response.Write(MyQuery & "<br>")
  113.                         end if
  114.                 next
  115.         end if
  116. next
  117.  
  118. mRS.Close
  119. MyConn.Close '------------> Close db Connection,
  120. Set mRS = Nothing
  121. Set MyConn = Nothing '----> Free Memory & Leave!
  122. Response.Redirect "./"
  123. %>

Code formatting thingy doesn't seem to like tabs much do it? ...Hell it's midnight, I ain't fix'n it.

f0dder

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,029
  • [Well, THAT escalated quickly!]
    • View Profile
    • f0dder's place
    • Read more about this member.
    • Donate to Member
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #6 on: December 07, 2010, 01:53:11 AM »
...welcome to TheDailyWtf.com :P
- carpe noctem