Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ASP Multidimensional Array to HTML listing

Posted on 2009-02-10
14
Medium Priority
?
468 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:FrankMeenz
  • 6
  • 4
  • 3
13 Comments
 
LVL 12

Expert Comment

by:R_Harrison
ID: 23611123
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
 
LVL 28

Expert Comment

by:sybe
ID: 23611234
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
 

Author Comment

by:FrankMeenz
ID: 23611387
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
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 28

Expert Comment

by:sybe
ID: 23611473
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
 

Author Comment

by:FrankMeenz
ID: 23611923
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
 
LVL 28

Expert Comment

by:sybe
ID: 23612072
>  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
 
LVL 12

Expert Comment

by:R_Harrison
ID: 23612327
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
 

Author Comment

by:FrankMeenz
ID: 23612455
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
 
LVL 12

Expert Comment

by:R_Harrison
ID: 23612463
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
 

Author Comment

by:FrankMeenz
ID: 23615324
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
 
LVL 28

Accepted Solution

by:
sybe earned 500 total points
ID: 23616936
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
 

Author Comment

by:FrankMeenz
ID: 23617292
sybe --

this is going to work alot better for me, thank you very much for your time and your help!
0
 

Author Closing Comment

by:FrankMeenz
ID: 31545108
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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

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…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

580 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