DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: (Train Wreck) Multidimensional Dynamic Array in ASP  (Read 9,170 times)

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
(Train Wreck) Multidimensional Dynamic Array in ASP
« on: December 06, 2010, 09:52 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)
<%
Dim ar_LAPN, iCount
Dim ar_vCode(), ar_vFName(), ar_vActive(), iVends
ar_LAPN = Split(Request.Form("LAPN"), ",")
iCount  = Request.Form("iCount")

Dim ar_nVend00(10, 1000), ar_pVend00(10, 1000)

Dim MyConn, MyQuery, mRS '-----> SQL Query Variables
set MyConn = Server.CreateObject("ADODB.Connection")
	MyConn.Open(MySQL)

MyQuery = "SELECT VendorCode, VendorFName, Active, BorderColor, FontClass FROM VendorNames"
Set mRS = MyConn.Execute(MyQuery)
If mRS.BOF And mRS.EOF Then
    Response.Write("MySQL Query Failed! " & MyQuery)
    Response.End
Else
	iVends = 0
	mRS.MoveFirst
	WHILE NOT mRS.EOF
		ReDim Preserve ar_vCode(iVends + 1)
		ReDim Preserve ar_vFName(iVends + 1)
		ar_vCode(iVends)        = mRS("VendorCode")
		ar_vFName(iVends)       = mRS("VendorFName")
		ar_vActive(iVends)      = mRS("Active") '--+++--> Add Column for Vendor X Only if They're Active
		
		if ar_vActive(iVends) Then '--------------------------------------+++-->
'			ReDim Preserve ar_nVend00(iVends + 1)
'			ReDim Preserve ar_pVend00(iVends + 1)
			ar_nVend00(iVends, 0) = Split(Request.Form("nVend00" & iVends), ",")
			ar_pVend00(iVends, 0) = Split(Request.Form("pVend00" & iVends), ",")
		end if
		
		iVends = iVends + 1
		mRS.MoveNext
	WEND
	iVends = iVends - 1
End If



for i=0 to iCount
	response.write("LAPN: " & ar_LAPN(i) & " ")
	for iV=0 to iVends
		response.write(" Qty: " & ar_nVend00(iVends, iCount) & " * Price: " & ar_pVend00(iVends, iCount) & " | ")
	next
	response.write("Vends-break;<br>")
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, 11:22 PM by Stoic Joker »

timns

  • Veni, vidi, debuggi
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,211
    • Participated in NANY Challenge Four Times
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #1 on: December 06, 2010, 10:40 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, 10:44 PM by timns »

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #2 on: December 06, 2010, 11:45 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 07, 2010, 12:15 AM by Stoic Joker »

timns

  • Veni, vidi, debuggi
  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 1,211
    • Participated in NANY Challenge Four Times
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #3 on: December 07, 2010, 01:08 AM »
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,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #4 on: December 07, 2010, 03:50 AM »
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)
<!--#include virtual="inc/dbConn.inc"-->
<%
Response.Buffer = False '--+++--> HOLY SHIT!!! (that helped)

Dim ar_LAPN, iCount
Dim ar_vCode(10), ar_vFName(10), ar_vActive(10), iVends
ar_LAPN = Split(Request.Form("LAPN"), ",")
iCount  = Request.Form("iCount")

Dim ar_nVend00, ar_pVend00
ReDim ar_nVend00(10)
ReDim ar_pVend00(10)

Dim MyConn, MyQuery, mRS '-----> SQL Query Variables
set MyConn = Server.CreateObject("ADODB.Connection")
	MyConn.Open(MySQL)

MyQuery = "SELECT VendorCode, VendorFName, Active, BorderColor, FontClass FROM VendorNames"
Set mRS = MyConn.Execute(MyQuery)
If mRS.BOF And mRS.EOF Then
    Response.Write("MySQL Query Failed! " & MyQuery)
    Response.End
Else
	iVends = 0
	mRS.MoveFirst
	WHILE NOT mRS.EOF
		ar_vActive(iVends)      = mRS("Active") '--+++--> Add Column for Vendor X Only if They're Active
		ar_vCode(iVends)        = mRS("VendorCode")
		ar_vFName(iVends)       = mRS("VendorFName")
		if ar_vActive(iVends) Then '--------------------------------------+++-->
			response.write("Vendor: " & ar_vFName(iVends) & " Is Active: " & ar_vActive(iVends) & "<br>")
'			ar_nVend00(iVends, 0) = Split(Request.Form("nVend00" & iVends), ",")
'			ar_pVend00(iVends, 0) = Split(Request.Form("pVend00" & iVends), ",")
			ar_nVend00(iVends) = Request.Form("nVend00" & iVends)
			ar_pVend00(iVends) = Request.Form("pVend00" & iVends)
			
'			response.write(ar_nVend00(iVends) & "<br>")
'			response.write(ar_pVend00(iVends) & "<br>")
		else
			response.write(ar_vFName(iVends) & " Vendor Number: " & iVends & " is NOT Active!<br>")
		end if
		iVends = iVends + 1 '--+++--> This MUST be Here so the Vend00x Number Matches the db Info.
		mRS.MoveNext
	WEND
'	ReDim Preserve ar_vActive(iVends)
'	ReDim Preserve ar_vFName(iVends)
'	ReDim Preserve ar_vCode(iVends)
	iVends = iVends - 1
End If

response.write("<br><br>")

for i=0 to iCount


			MyQuery = "SELECT Printer, Description FROM Inventory WHERE LAPN = '" & ar_LAPN(i) & "'"
			Set mRS = MyConn.Execute(MyQuery)
			Printer = mRS("Printer")
			Description = mRS("Description")


	response.write("LAPN: " & ar_LAPN(i) & " :-: " & Description & "<br>")
	for iV=0 to iVends
		Dim ar_nV, ar_pV
		
		if ar_vActive(iV) Then '--------------------------------------+++-->
'			response.write("<br>==========================================================================================<br>")
'			response.write(ar_nVend00(iV) & "<br>")
'			response.write(ar_pVend00(iV) & "<br>")
'			response.write("==========================================================================================<br>")
		ar_nV = Split(ar_nVend00(iV), ",")
		ar_pV = Split(ar_pVend00(iV), ",")
'		response.write(" Qty: " & ar_nVend00(iVends, iCount) & " * Price: " & ar_pVend00(iVends, iCount) & " | ")
		response.write("Vendor: " & ar_vFName(iV) & " Qty: " & ar_nV(iCount) & " * Price: " & ar_pV(iCount) & "<br>")
		else
			response.write("==========================================================================================<br>")
			response.write("Vendor: " & ar_vFName(iV) & " - IS NOT ACTIVE!!!")
			response.write("<br>==========================================================================================<br>")
		end if
	next
	response.write("End of LAPN: " & ar_LAPN(i) & " Vends-break;<br><br>")
next
%>

Thank you!

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,652
    • Cody Award
    • Participated in NANY Challenge Five Times
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #5 on: December 07, 2010, 04:55 AM »
Okay, looks like I get to answer my own question (sort of/thanks timns) Here's the currently finally working code:
Code: (asp)
<!--#include virtual="inc/dbConn.inc"-->
<%
'--+++--> Orders_ReceiveArray.asp
Response.Buffer = False '--+++--> HOLY SHIT!!!

Dim ar_LAPN, iCount
Dim ar_vCode(10), ar_vFName(10), ar_vActive(10), iVends
ar_LAPN = Split(Request.Form("LAPN"), ",")
iCount  = Request.Form("iCount")

Dim ar_nVend00, ar_pVend00
ReDim ar_nVend00(10)
ReDim ar_pVend00(10)

Dim MyConn, MyQuery, mRS '-----> SQL Query Variables
set MyConn = Server.CreateObject("ADODB.Connection")
	MyConn.Open(MySQL)

MyQuery = "SELECT VendorCode, VendorFName, Active, BorderColor, FontClass FROM VendorNames"
Set mRS = MyConn.Execute(MyQuery)
If mRS.BOF And mRS.EOF Then
    Response.Write("MySQL Query Failed! " & MyQuery)
    Response.End
Else
	iVends = 0
	mRS.MoveFirst
	WHILE NOT mRS.EOF
		ar_vActive(iVends)      = mRS("Active") '--+++--> Add Column for Vendor X Only if They're Active
		ar_vCode(iVends)        = mRS("VendorCode")
		ar_vFName(iVends)       = mRS("VendorFName")
		if ar_vActive(iVends) Then '--------------------------------------+++-->
			ar_nVend00(iVends) = Request.Form("nVend00" & iVends)
			ar_pVend00(iVends) = Request.Form("pVend00" & iVends)
		end if
		iVends = iVends + 1 '--+++--> This MUST be Here so the Vend00x Number Matches the db Info.
		mRS.MoveNext
	WEND
	iVends = iVends - 1
End If

'============== MySQL TimeStamp Functions next 3
function mysqltimestamp(aspts)
	dim thetime,thedate
	aspts=cstr(aspts)
	thedate=left(aspts,instr(aspts," ")-1)
	thetime=right(aspts,len(aspts)-instr(aspts," "))
	mysqltimestamp=mysqldate(thedate)&" "&mysqltime(thetime)
end function

function mysqlDate(d)
	dim strNewDate
	strNewDate = year( d ) & "-" & month( d ) & "-" & day(d)
	mysqlDate = strNewDate
end function

function mysqlTime(t)
 dim strSuffix, arTime, i, x
 t = trim( Lcase( t ) )
 if inStr( t, "pm" ) > 0 OR inStr( t, "am" ) > 0 then 
  strSuffix = right( t, 2 )
  t = left( t, inStr( t, strSuffix ) -2 )
  t = trim( t )
 end if 
 for i = 1 to len( t )
  x = mid( t, i, 1 )
  if not isNumeric( x ) and x <> ":" then t = replace( t, x, "" )
 next
 arTime = split( t, ":" )
 t = ""
 for i = 0 to 2
  if uBound( arTime ) < i then redim preserve arTime( i )
  if i = 0 then
    if strSuffix = "pm" and cInt( arTime( i ) ) < 12 then 
     arTime( i ) = cInt( arTime( i ) ) + 12
    end if
  end if
  do until len( arTime( i ) ) = 2
   arTime( i ) = "0" & arTime( i )
  loop
  t = t & arTime( i )
  if i < 2 then t = t & ":"
 next
 arTime = null
 if dir = 2 then t = t & " " & strSuffix
 mysqlTime = t
end function
'============== END MySQL TimeStamp Functions
TimeDate = mysqltimestamp(Now)
%>
Production Parts Order<br>
<%


for iV=0 to iVends
	Dim ar_nV, ar_pV
	
	if ar_vActive(iV) Then '------( IF Vendor X IS Active )-------+++-->
		ar_nV = Split(ar_nVend00(iV), ",")
		ar_pV = Split(ar_pVend00(iV), ",")
		
		for i=0 to iCount
			if ar_nV(i) > 0 Then '--+++--> Create Entry Only IF the Part has a Quantity
				MyQuery = "SELECT Printer, Description FROM Inventory WHERE LAPN = '" & ar_LAPN(i) & "'"
				Set mRS = MyConn.Execute(MyQuery)
				Printer = mRS("Printer")
				Description = mRS("Description")
				
				MyQuery = "INSERT INTO OrderHistory (LAPN, Printer, Description, Quantity, PriceEach, Vendor, timeStamp) " &_
									"VALUES ('" & ar_LAPN(i) & "', '" & Printer & "', '" & Description & "', '" & ar_nV(i) &_
									"', '" & ar_pV(i) & "', '" & ar_vFName(iV) & "', '" & TimeDate & "')"
				MyConn.Execute(MyQuery)
'				Response.Write(MyQuery & "<br>")
			end if
		next
	end if
next

mRS.Close
MyConn.Close '------------> Close db Connection,
Set mRS = Nothing
Set MyConn = Nothing '----> Free Memory & Leave!
Response.Redirect "./"
%>

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

f0dder

  • [Well, THAT escalated quickly!]
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 9,153
    • Participated in NANY Challenge Once
Re: (Train Wreck) Multidimensional Dynamic Array in ASP
« Reply #6 on: December 07, 2010, 07:53 AM »
...welcome to TheDailyWtf.com :P
- carpe noctem

Original address: https://www.donationcoder.com/forum/index.php?topic=24802.0