Multidimensional Array Drop-Down?

My issue is, I have set up a hierarchal category system based on 1 table in a SQL db, structured like this:

tableName
gcID <-int
parentID <-int (related to gcID)
gCat <-nvarchar

I have code to build myself a drop-down list so I can move stuff from one category to another.....problem is I link to the edit page with a querystring that is the categories ID#.  Q is how can I get the drop-down to auto select the correct category....

code follows:
this is an include file:
<script language="javascript" runat="server">
function jssort(a)
{
   return (new VBArray(a)).toArray().sort().join(String.fromCharCode(0));
}
</script>
<%
class invcat
   dim id, category, parentid, description
end class
class invcats
   dim cats
   dim sortindex()  'holds sorted key index
   sub class_initialize
        set cats = server.createobject("Scripting.Dictionary")
   end sub
   sub class_terminate
        set cats = nothing
   end sub
   sub BuildFromDB(connstr)
       LoadFromDB connstr
       SetLabels
       BuildSortIndex
   end sub
   sub BuildFromArray(array)
       LoadFromArray array
       SetLabels
       BuildSortIndex
   end sub
   sub LoadFromArray(a)
       dim i,n,line,cat
       for i = lbound(a) to ubound(a)
            line = split(a(i),"^")
            set cat = new invcat
            cat.id = line(0)
            cat.parentid = line(1)
            cat.category = line(2)
            cats.add cat.id, cat
       next
   end sub
  sub LoadFromDB(conn)
        dim rs, cat
        set rs = server.createobject("ADODB.Recordset")
        rs.open "select gcID, parentID, gCat from fileCats", conn
        do while not rs.eof
            set cat = new invcat
            cat.id = rs("gcID")
            cat.parentid = rs("parentID")
            cat.category = rs("gCat")
            cats.add clng(cat.id), cat
            rs.movenext
        loop
        rs.close
        set rs = nothing
   end sub
   sub SetLabels
      dim i
      for each i in cats.keys
          cats(i).description = mid(BuildLabel(i),4)
      next
   end sub
   function BuildLabel(id)  
       if id = 0 then
          BuildLabel = ""
          exit function
       else
          dim curcat
          set curcat = cats(id)
          BuildLabel = BuildLabel(curcat.parentid) & " - " & curcat.category
       end if
   end function
   sub BuildSortIndex
       dim key,descripts,desc
       set descripts = server.createobject("Scripting.Dictionary")
       for each key in cats.keys
           desc = cats(key).description
           if descripts.exists(desc) then
              descripts(desc) = descripts(desc) & "|" & key
           else
              descripts(desc) = key
           end if
       next
       dim sorted
       sorted = split(jssort(descripts.keys), chr(0))
       dim i,n,keys,index
       index = 0
       redim sortindex(cats.count - 1)
       for i = lbound(sorted) to ubound(sorted)
           keys = split(descripts(sorted(i)), "|")
           for n = lbound(keys) to ubound(keys)
              sortindex(index) = keys(n)
              index = index + 1
           next
       next
   end sub
  function SelectBox
        dim i,out,cat
        out = "<select name=""gcID"">" & vbcrlf
        for i = lbound(sortindex) to ubound(sortindex)
           if cats.exists(clng(sortindex(i))) then
           set cat = cats(clng(sortindex(i)))
                  'if cat.id = pCat then
                  '      ll=" selected"
                  'end if

                out = out & "<option value=""" & cat.id & """"&ll&">" & cat.description &"</option>" & vbcrlf
        end if
        next
        out = out & "</select>" & vbcrlf
        SelectBox = out
   end function
end class
%>



and the drop-down is:
<%
                        dim myCats
                        set myCats = new invcats
                              myCats.BuildFromDB(inrsConn)
                              write(myCats.SelectBox)
                        set myCats = nothing
                        %>
LVL 25
kevp75Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
clockwatcherConnect With a Mentor Commented:
Here you go...  the problem is with the types.  clng(3) <> cstr(3)... force the types and your code should work fine:

Here's a modification to the class:

  function SelectBoxWithSelection(selection)
        dim i,out,cat
        out = "<select name=""gcID"">" & vbcrlf
        for i = lbound(sortindex) to ubound(sortindex)
           if cats.exists(clng(sortindex(i))) then
           set cat = cats(clng(sortindex(i)))
               if cstr(cat.id) = cstr(selection) then
                    ll=" selected"
               end if

                out = out & "<option value=""" & cat.id & """"&ll&">" & cat.description &"</option>" & vbcrlf
        end if
        next
        out = out & "</select>" & vbcrlf
        SelectBoxWithSelection = out

  end function

  function SelectBox
      SelectBox = SelectBoxWithSelection("")
  end function

And then calling it:

                    dim myCats
                    set myCats = new invcats
                         myCats.BuildFromDB(inrsConn)
                         write(myCats.SelectBoxWithSelection(request("pCat"))
                    set myCats = nothing

0
 
kevp75Author Commented:
I guess it'd probably be a good idea to show what this does and the data in the table....so click here and you'll see.
http://www.festivalcos.com/filerepo/temp.asp
0
 
CtrlAltDlCommented:
Can't you just do this in the function SelectBox:

if cat.id = Request.Form("gcID") then
     ll=" selected"
end if

If that isn't it (which I doubt it is) you may need to explain the work flow.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
kevp75Author Commented:
i've tried with:
if cat.id = Request.Form("pCat") then
     ll=" selected"
end if

if cat.id = Request.querystring("pCat") then
     ll=" selected"
end if

neither have worked.  I'm not sure what the work flow is, someone here helped me do it.

if you look here http://www.festivalcos.com/filerepo/temp.asp?pCat=10, i've displayed the value of the querystring and the categories id, however when I add in the if statement nothing happens (we're looking to get 10-10-Documents - Test - Test 2 - Test 1 selected for this particular querystring)
0
 
kevp75Author Commented:
here's the updated code:
<script language="javascript" runat="server">
function jssort(a)
{
   return (new VBArray(a)).toArray().sort().join(String.fromCharCode(0));
}
</script>
<%
pCat = request.querystring("pCat")
if pCat = 0 OR ISNULL(pCat) OR pCat = "" then
      pCat = 0
else
      pCat = cint(pCat)
end if
class invcat
   dim id, category, parentid, description
end class
class invcats
   dim cats
   dim sortindex()  'holds sorted key index
   sub class_initialize
        set cats = server.createobject("Scripting.Dictionary")
   end sub
   sub class_terminate
        set cats = nothing
   end sub
   sub BuildFromDB(connstr)
       LoadFromDB connstr
       SetLabels
       BuildSortIndex
   end sub
   sub BuildFromArray(array)
       LoadFromArray array
       SetLabels
       BuildSortIndex
   end sub
   sub LoadFromArray(a)
       dim i,n,line,cat
       for i = lbound(a) to ubound(a)
            line = split(a(i),"^")
            set cat = new invcat
            cat.id = line(0)
            cat.parentid = line(1)
            cat.category = line(2)
            cats.add cat.id, cat
       next
   end sub
  sub LoadFromDB(conn)
        dim rs, cat
        set rs = server.createobject("ADODB.Recordset")
        rs.open "select gcID, parentID, gCat from fileCats", conn
        do while not rs.eof
            set cat = new invcat
            cat.id = rs("gcID")
            cat.parentid = rs("parentID")
            cat.category = rs("gCat")
            cats.add clng(cat.id), cat
            rs.movenext
        loop
        rs.close
        set rs = nothing
   end sub
   sub SetLabels
      dim i
      for each i in cats.keys
          cats(i).description = mid(BuildLabel(i),4)
      next
   end sub
   function BuildLabel(id)  
       if id = 0 then
          BuildLabel = ""
          exit function
       else
          dim curcat
          set curcat = cats(id)
          BuildLabel = BuildLabel(curcat.parentid) & " - " & curcat.category
       end if
   end function
   sub BuildSortIndex
       dim key,descripts,desc
       set descripts = server.createobject("Scripting.Dictionary")
       for each key in cats.keys
           desc = cats(key).description
           if descripts.exists(desc) then
              descripts(desc) = descripts(desc) & "|" & key
           else
              descripts(desc) = key
           end if
       next
       dim sorted
       sorted = split(jssort(descripts.keys), chr(0))
       dim i,n,keys,index
       index = 0
       redim sortindex(cats.count - 1)
       for i = lbound(sorted) to ubound(sorted)
           keys = split(descripts(sorted(i)), "|")
           for n = lbound(keys) to ubound(keys)
              sortindex(index) = keys(n)
              index = index + 1
           next
       next
   end sub
  function SelectBox
        dim i,out,cat
        out = "<select name=""gcID"">" & vbcrlf
        for i = lbound(sortindex) to ubound(sortindex)
           if cats.exists(clng(sortindex(i))) then
           set cat = cats(clng(sortindex(i)))
                  if cat.id = pCat then
                        ll = " selected"
                  end if
                out = out & "<option value=""" & cat.id & """"&ll&">"& pCat &"-" & cat.id &"-"& cat.description &"</option>" & vbcrlf
        end if
        next
        out = out & "</select>" & vbcrlf
        SelectBox = out
   end function
end class
%>


same method of using it...
0
 
kevp75Author Commented:
nothing's changed, however now I get an Object Required Error on this line of the class:
set curcat = cats(id)
0
 
kevp75Author Commented:
anyone, anyone......bueller, bueller?
0
 
CtrlAltDlCommented:
The object required error is probably because your SQL statement below is EOF, so it won't create the cat object and you can't create a new curcat object if cats is not already an object.

        rs.open "select gcID, parentID, gCat from fileCats", conn
        do while not rs.eof
            set cat = new invcat

So depending on what is in the pCat querystring it should pre-select a particular option in the Select box?  Is that what you are trying to do?
0
 
kevp75Author Commented:
correct.  not too sure why it would be an eof error though, because there are records in the table....
0
 
kevp75Author Commented:
i wonder if I add the " selected" to the dictionary object, but have it added on the id
0
 
CtrlAltDlCommented:
What did you end up doing?  I assume you got it working now?
0
 
kevp75Author Commented:
i have not got it working at all...

please see http://www.festivalcos.com/filerepo/temp.asp

and since there hasn't been a solution, I wanted to clean up my asked questions...
0
 
kevp75Author Commented:
sorry clockwatcher.

have a look at the page:http://www.festivalcos.com/filerepo/temp.asp

clicking the link will initiate the querystring pCat=16

which should 'select'    16CADImages - AutoCAD
0
 
kevp75Author Commented:
(
p.s. to show the 16CADImages...etc, all I did was change this line:
out = out & "<option value=""" & cat.id & """"&ll&">" & cat.description &"</option>" & vbcrlf

to this:
out = out & "<option value=""" & cat.id & """"&ll&">" & cat.id & cat.description &"</option>" & vbcrlf
)
0
 
clockwatcherCommented:
Paste your entire code... forcing the type match should fix the problem.  It does in my tests.
0
 
kevp75Author Commented:
the code is exactly how you had fisrt showed me, however I added in the value for pCat and the value of cat.id into what is displayed in the drop-down:

special.asp:
<script language="javascript" runat="server">
function jssort(a)
{
   return (new VBArray(a)).toArray().sort().join(String.fromCharCode(0));
}
</script>
<%
class invcat
   dim id, category, parentid, description
end class
class invcats
   dim cats
   dim sortindex()
   sub class_initialize
        set cats = server.createobject("Scripting.Dictionary")
   end sub
   sub class_terminate
        set cats = nothing
   end sub
   sub BuildFromDB(connstr)
       LoadFromDB connstr
       SetLabels
       BuildSortIndex
   end sub
   sub BuildFromArray(array)
       LoadFromArray array
       SetLabels
       BuildSortIndex
   end sub
   sub LoadFromArray(a)
       dim i,n,line,cat
       for i = lbound(a) to ubound(a)
            line = split(a(i),"^")
            set cat = new invcat
            cat.id = line(0)
            cat.parentid = line(1)
            cat.category = line(2)
            cats.add cat.id, cat
       next
   end sub
  sub LoadFromDB(conn)
        dim rs, cat
        set rs = server.createobject("ADODB.Recordset")
        rs.open "select gcID, parentID, gCat from fileCats", conn
        do while not rs.eof
            set cat = new invcat
            cat.id = rs("gcID")
            cat.parentid = rs("parentID")
            cat.category = rs("gCat")
            cats.add clng(cat.id), cat
            rs.movenext
        loop
        rs.close
        set rs = nothing
   end sub
   sub SetLabels
      dim i
      for each i in cats.keys
          cats(i).description = mid(BuildLabel(i),4)
      next
   end sub
   function BuildLabel(id)  
       if id = 0 then
          BuildLabel = ""
          exit function
       else
          dim curcat
          set curcat = cats(id)
          BuildLabel = BuildLabel(curcat.parentid) & " - " & curcat.category
       end if
   end function
   sub BuildSortIndex
       dim key,descripts,desc
       set descripts = server.createobject("Scripting.Dictionary")
       for each key in cats.keys
           desc = cats(key).description
           if descripts.exists(desc) then
              descripts(desc) = descripts(desc) & "|" & key
           else
              descripts(desc) = key
           end if
       next
       dim sorted
       sorted = split(jssort(descripts.keys), chr(0))
       dim i,n,keys,index
       index = 0
       redim sortindex(cats.count - 1)
       for i = lbound(sorted) to ubound(sorted)
           keys = split(descripts(sorted(i)), "|")
           for n = lbound(keys) to ubound(keys)
              sortindex(index) = keys(n)
              index = index + 1
           next
       next
   end sub
function SelectBoxWithSelection(selection)
        dim i,out,cat
        out = "<select name=""gcID"">" & vbcrlf
        for i = lbound(sortindex) to ubound(sortindex)
           if cats.exists(clng(sortindex(i))) then
           set cat = cats(clng(sortindex(i)))
               if cint(cat.id) = cint(selection) then
                           lb = "yes"
                    ll=" selected"
               end if

                out = out & "<option value=""" & cat.id & """"&ll&">" & selection &"-"& cat.id & cat.description &"</option>" & vbcrlf
        end if
        next
        out = out & "</select>" & vbcrlf
        SelectBoxWithSelection = out

  end function

  function SelectBox
     SelectBox = SelectBoxWithSelection("")
  end function
end class
%>

temp.asp:
<!--#include virtual="/filerepo/inc/special.asp"-->
<p>If you <a href="/filerepo/temp.asp?pCat=16">click here</a>, you will see that it does not select a default..</p>
<p>this link selects the record that should be id of 16, as you can see in the select, I am displaying the value of the querystring (pCat) and the value of cat.id, and if they are equal, than that particular record should be selected, but you can see it is not.</p>
<%
                        dim myCats
                        set myCats = new invcats
                              myCats.BuildFromDB(inrsConn)
                              write(myCats.SelectBoxWithSelection(request("pCat")))
                        set myCats = nothing
                        %>
0
 
kevp75Author Commented:
p.s.   the lb in there was simply to show in the drop-down whether pCat was equal to cat.id, and serves no real purpose....
0
 
clockwatcherCommented:
You need to reset your variable.  It's getting set and staying set.

  for i = lbound(sortindex) to ubound(sortindex)
           if cats.exists(clng(sortindex(i))) then
           set cat = cats(clng(sortindex(i)))
               ll = ""
               lb = ""
               if cint(cat.id) = cint(selection) then
                       lb = "yes"
                    ll=" selected"
               end if

                out = out & "<option value=""" & cat.id & """"&ll&">" & selection &"-"& cat.id & cat.description &"</option>" & vbcrlf
        end if
0
 
kevp75Author Commented:
DOH!

thanks a ton clock!
0
 
kevp75Author Commented:
one more time.....if anyone is interested....
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21938504.html
0
All Courses

From novice to tech pro — start learning today.