<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>McWilliams' World &#187; Code</title>
	<atom:link href="http://www.mcwilliamsworld.com/archives/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mcwilliamsworld.com</link>
	<description>McWilliams' thoughts on Xbox, deals, free stuff, and anything else</description>
	<lastBuildDate>Mon, 13 Jul 2009 13:53:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Code: ASP/VBScript, properly nested list from array</title>
		<link>http://www.mcwilliamsworld.com/archives/2007/10/code-aspvbscript-properly-nested-list-from-array/</link>
		<comments>http://www.mcwilliamsworld.com/archives/2007/10/code-aspvbscript-properly-nested-list-from-array/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 16:48:41 +0000</pubDate>
		<dc:creator>mcwilliams</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://www.mcwilliamsworld.com/archives/2007/10/05/code-aspvbscript-properly-nested-list-from-array/</guid>
		<description><![CDATA[Eureka! Finally! It only took me about 5 days of tinkering but I managed to  code a dynamic,  multi-nested list from an array. The project is to expand the number of levels that available to the web sites at work. Currently we only alloted two level. That was a number of years ago. Now some [...]]]></description>
			<content:encoded><![CDATA[<p>Eureka! Finally!</p>
<p>It only took me about 5 days of tinkering but I managed to  code a dynamic,  multi-nested list from an array.</p>
<p>The project is to expand the number of levels that available to the web sites at work. Currently we only alloted two level. That was a number of years ago. Now some of our devisions are revamping their content and are needing multiple levels.</p>
<p>So I met with our database guys (DBAs) to draft up a cross reference relational table structure and some stored procedures to bring back the hierarchy of pages so I can draw up a properly nested list of links to use as page navigation (wow that was a long sentence). I&#8217;ve been banging my head against the wall for the better part of the week &#8211; getting close to a solution but then scrapping the code to come at the problem at a different angle. Today I finally made it work and it&#8217;s universal (hopefully).</p>
<p>The logic is what tripped me up. I had to think backwards in a way to determine how to close the list items (&lt;li&gt;) and any nested lists.</p>
<p>Here&#8217;s what I wrote. Assume we have our recordset returned as a 2d array via GetRows. There&#8217;s more to be done as proper tabbing, and other site-based logic but this gets the output I need.</p>
<p><code><br />
&lt;%<br />
' -------------------------------------<br />
dim numcols, numrows<br />
' cols = 1<br />
' rows = 2<br />
numcols=ubound(alldata,1)<br />
numrows=ubound(alldata,2)</p>
<p>' our fields<br />
dim fld_xref, fld_id, fld_title<br />
dim title, id, xref</p>
<p> fld_title = 0<br />
 fld_id  = 1<br />
 fld_xref  = 2</p>
<p>' our formating<br />
dim ul_open, ul_close, li_open, li_close, list_close</p>
<p> ul_open  = "&lt;ul&gt;" &amp; nl<br />
 ul_close  = "&lt;/ul&gt;" &amp; nl<br />
 <br />
 li_open  = vbTab &amp; "&lt;li&gt;"<br />
 li_close  = "&lt;/li&gt;" &amp; nl<br />
 <br />
 list_close =  ul_close &amp; nl &amp; li_close &amp; nl</p>
<p>dim x<br />
dim l<br />
dim levels<br />
dim tempArray<br />
dim parentID<br />
dim lastID<br />
dim lastXref<br />
 <br />
' response.Write("&lt;p&gt;&lt;a href=""#"" id=""expand_all""&gt;&lt;strong&gt;[+]&lt;/strong&gt; Expand All&lt;/a&gt; | &lt;a href=""#"" id=""collapse_all""&gt;&lt;strong&gt;[-]&lt;/strong&gt; Collapse All&lt;/a&gt; &lt;/p&gt;" &amp; nl)<br />
 Response.Write("&lt;ul id=""list""&gt;" &amp; nl)</p>
<p>For x = 0 to numRows</p>
<p>' --------------------------------------------------<br />
' Set the stage<br />
' --------------------------------------------------<br />
 <br />
 title  = Trim(allData(fld_title, x))<br />
 id  = allData(fld_id, x)<br />
 xref = allData(fld_xref, x)</p>
<p> ' ignore first pass<br />
 if x &gt; 0 then<br />
 <br />
  if xref = lastID then<br />
  ' LAST ID WAS PARENT TO CURRENT ID<br />
  ' open a new &lt;ul&gt;  <br />
   parentID = lastID<br />
   levels = levels &amp; " " &amp; lastID<br />
   response.Write(nl &amp; ul_open)<br />
   <br />
  elseif xref = lastXref then<br />
  ' Current ID is a sibling<br />
  ' we'll just close the previous &lt;li&gt;<br />
   response.Write(li_close &amp; nl)<br />
   <br />
  else<br />
  ' we're not sure what we're dealing with<br />
  ' more than likely we need to close a nested list<br />
     <br />
   'close the last &lt;li&gt;<br />
   response.Write(li_close)<br />
  <br />
   ' generate our array of parent IDs<br />
   tempArray = Split(levels)<br />
  <br />
   ' walk backwards through the array so<br />
   ' we get the proper count of lists to close<br />
   for l = Ubound(tempArray) to 1 Step -1<br />
    parentID = CInt(tempArray(l))<br />
    if xref &lt;&gt; parentID then<br />
     ' if xref does not match our parent ID<br />
     ' close the list<br />
     response.Write(list_close)<br />
     <br />
     ' remove the previous ID from the list<br />
     ' so the next walk throug, there will be one less<br />
     levels = replace(levels, " " &amp; parentID, "")<br />
     <br />
    else<br />
     ' we have a match, stop processing the list<br />
     exit for<br />
    end if<br />
       <br />
   next <br />
     <br />
  end if<br />
  <br />
 end if<br />
 <br />
 ' output our record <br />
 response.Write(li_open)<br />
 response.Write(title &amp; " | id: " &amp; id &amp; " | xref: " &amp; xref &amp; " | levels: " &amp; levels)<br />
   <br />
 lastID = id<br />
 lastXref = xref<br />
 <br />
Next</p>
<p> response.Write(li_close)<br />
 Response.Write(nl &amp; ul_close)<br />
%&gt;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.mcwilliamsworld.com/archives/2007/10/code-aspvbscript-properly-nested-list-from-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
