ASP Multidimensional Array to HTML listing

Hello,

I have an .asp multidimensional array as follows in the code snippet

I need to output the array in an html table in this format, where I am basically listing unique values of Col2, and listing the child elements that are associated with it.

------------------------
Title1: Name1, Name2
------------------------
Title2: Name3
-------------------------
Title3: Name4, Name5
-------------------------

Not sure where to start here, any help would be appreciated.

thank you!
Dim myArray(2,4)
'myArray(col,row)
myArray(0,0) = "Name1"
myArray(1,0) = "5"
myArray(2,0) = "Title1"
myArray(0,1) = "Name2"
myArray(1,1) = "5"
myArray(2,1) = "Title2"
myArray(0,2) = "Name3"
myArray(1,2) = "6"
myArray(2,2) = "Title2"
myArray(0,3) = "Name4"
myArray(1,3) = "7"
myArray(2,3) = "Title3"
myArray(0,4) = "Name5"
myArray(1,4) = "7"
myArray(2,4) = "Title3"
 
 
 
 
Response.Write "<table border='1'>" 
Response.Write "<tr><td>Col1</td>" 
Response.Write "<td>Col2</td><td>Col3</td></tr>" 
For i = 0 to UBound(myArray,2) 
 
Response.Write "<tr><td>" & myArray(0,i) & "</td>" 
Response.Write "<td>" & myArray(1,i) & "</td>" 
Response.Write "<td>" & myArray(2,i) & "</td></tr>" 
Next 
Response.Write "</table>"

Open in new window

FrankMeenzAsked:
Who is Participating?
 
sybeCommented:
Well, the code I wrote was just a sample of course, and it works with the test data you provided.

I don't know if you can use another indication for the items in the shopping cart, like an id from the database or an ISBN number, something that uniquely identifies the item but also is fit as the name of a node.

You could otherwise use sequential node names and store the value as an attribute, but that is not making it easier.

If things are getting too complicated with XML, you might want to think about another object that is fit for storing the data from your array in a more organized way. The basic idea behind my solution was that the data in the array are not well enough organized for the desired output. So the first step is to organize them better and then create the output. I picked XML as a way to organize the data, but you might think about other objects. Maybe a Dictionary fits better. But also that may depend on your actual data.

<%
'...
 
 
Set oDic = CreateObject("Scripting.Dictionary")
 
For i = 0 to UBound(myArray,2)
    If oDic.Exists(myArray(2,i)) Then
    	oDic(myArray(2,i)) = oDic(myArray(2,i)) & ", " & myArray(0,i)
    Else
    	oDic(myArray(2,i)) = myArray(0,i)
    End If
Next
 
 
Response.write "<table>"
For each element In oDic
    Response.write "<tr><td>" & oDic(element) & "</td></tr>"
Next
Response.write "</table>"
%>

Open in new window

0
 
R_HarrisonCommented:
The below should work for you, let me know if you need any explanations.
<%
Dim myArray(2,4)
'myArray(col,row)
myArray(0,0) = "Name1"
myArray(1,0) = "5"
myArray(2,0) = "Title1"
myArray(0,1) = "Name2"
myArray(1,1) = "5"
myArray(2,1) = "Title2"
myArray(0,2) = "Name3"
myArray(1,2) = "6"
myArray(2,2) = "Title2"
myArray(0,3) = "Name4"
myArray(1,3) = "7"
myArray(2,3) = "Title3"
myArray(0,4) = "Name5"
myArray(1,4) = "7"
myArray(2,4) = "Title3"
 
 
Response.Write "<table border='1'>" 
' the below is used to write out the table headers
Response.Write "<tr>" 
For i = 0 to UBound(myArray,1)
	response.write("<td>Col" & i+1 & "</td>")
Next
Response.Write "</tr>" 
 
 
For i = 0 to UBound(myArray,2) 
	Response.Write "<tr>" 
	for x=0 to UBound(myArray, 1)
		Response.Write "<td>" & myArray(x,i) & "</td>" 
	Next
	Response.Write "</tr>" 
Next 
Response.Write "</table>"
%>

Open in new window

0
 
sybeCommented:
I fear that doing that directly from your array is either very complicated or impossible. What I would do is create another object which stores the data in an easier way.

You could for example create an XML-object and adapt that while looping through your array, and finally use the XML-object to display your table.



<%
Dim myArray(2,4)
'myArray(col,row)
myArray(0,0) = "Name1"
myArray(1,0) = "5"
myArray(2,0) = "Title1"
myArray(0,1) = "Name2"
myArray(1,1) = "5"
myArray(2,1) = "Title2"
myArray(0,2) = "Name3"
myArray(1,2) = "6"
myArray(2,2) = "Title2"
myArray(0,3) = "Name4"
myArray(1,3) = "7"
myArray(2,3) = "Title3"
myArray(0,4) = "Name5"
myArray(1,4) = "7"
myArray(2,4) = "Title3"
 
 
Set oXML = Server.CreateObject("MSXML2.DomDocument.6.0")
Set oRoot = oXML.createElement("root")
oXML.appendChild oRoot
 
For i = 0 to UBound(myArray,2)
    Set oNode = oRoot.selectSingleNode(myArray(2,i))
    If TypeName(oNode) <> "IXMLDOMElement" Then
        ' node does not yet exist
        Set oNode = oXML.createElement(myArray(2,i))
        oRoot.appendChild oNode
    End If
Next
 
For i = 0 to UBound(myArray,2)
    Set oNode = oRoot.selectSingleNode(myArray(2,i))
    Set oChildNode = oXML.createElement(myArray(0,i))
    oNode.appendChild oChildNode
Next
 
Response.write "<table>"
 
For each oNode In oXML.documentElement.ChildNodes
    Response.write "<tr><td>" & oNode.tagName & ": "
    For each oChildNode in oNode.ChildNodes
        Response.write oChildNode.tagName & ", "
    Next
    Response.write "</td></tr>"
Next
Response.write "</table>"
 
%>

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
FrankMeenzAuthor Commented:
The XML solution works just as good as the array loop, however the array loop fits my application better, in this situation.  Thank you both for your input!
0
 
sybeCommented:
Fine with me, but the result from the array loop does not even look like your what you indicated as desired output. It does not list any unique values.

Next time just be a bit clearer in what you ask. You question was apparently only how to loop through a multi-dimensional array.

0
 
FrankMeenzAuthor Commented:
sbye,

after going back and checking, you are correct.  the array solution does not do what i was looking to do, as your xml solution does.  if there is a way to apply the solution to your comment please let me know.

my apologies for the oversight.
0
 
sybeCommented:
>  if there is a way to apply the solution to your comment please let me know

I guess you have to ask at community support. Not sure though.
0
 
R_HarrisonCommented:
Sorry, looks like I misunderstood how you wanted your output to look, I just used the output generated by your code as opposed to the sample you typed.   Should be easy to change though... give me a few minutes and I will repost
0
 
FrankMeenzAuthor Commented:
R Harrison -- that would be great to have an array based solution for this.  The xml works great, however i would prefer to stay within the scope of my current code.

also -- sbye -- just a note, ive been trying to implement your code which works great.   however when Im replacing my test values with actual values in the array, im getting the following error -

msxml6.dll error '80004005'
Expected token 'EOF' found

the line error is on the first instance of the following in the first For Next statement
Set oNode = oRoot.selectSingleNode(localCart(2,i))

its happening when i add spaces to the array values.  So for instance, when i change "Title1" to "Title 1" (or any other value containing spaces) the error comes up.

thanks for all your help here!  again my apologies for any miscommunication on my part.  first time user.
0
 
R_HarrisonCommented:
OK, now I have looked at your output I can see what you wanted... it would be possible with arrays but unless you want the solution that way then go with Sybe.

No objection from me if you want to re-allocate points...
0
 
FrankMeenzAuthor Commented:
sybe --

ive found the reason for my error, it was caused by the spaces in the Node values.  Ive urlEncoded the data and it returns OK, i can de-code them later when needed.

however now im getting another error when using a larger array with my live values:

msxml6.dll error '80004005'
Expression must evaluate to a node-set. <--

its on this line
Set oNode = oRoot.selectSingleNode(LC(8,i))

where LC is my array of shopping cart variables.  Im trying to pull the values from the 9th column as the unique values.  ive only changed the column number in your example, the root and child nodes to select to write the final html table.

Any thoughts on what I am missing would be great.  Im not so well versed in XML, which is why i was preferring the array-based solution.

thanks



0
 
FrankMeenzAuthor Commented:
sybe --

this is going to work alot better for me, thank you very much for your time and your help!
0
 
FrankMeenzAuthor Commented:
Thank you very much for your help.  Ive been doing some research while waiting for some input, and I was on the right path to what you provided.  Excellent!
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.