Link to home
Start Free TrialLog in
Avatar of kevp75
kevp75Flag for United States of America

asked on

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
                        %>
Avatar of kevp75
kevp75
Flag of United States of America image

ASKER

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
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.
Avatar of kevp75

ASKER

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)
Avatar of kevp75

ASKER

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...
Avatar of kevp75

ASKER

nothing's changed, however now I get an Object Required Error on this line of the class:
set curcat = cats(id)
Avatar of kevp75

ASKER

anyone, anyone......bueller, bueller?
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?
Avatar of kevp75

ASKER

correct.  not too sure why it would be an eof error though, because there are records in the table....
Avatar of kevp75

ASKER

i wonder if I add the " selected" to the dictionary object, but have it added on the id
What did you end up doing?  I assume you got it working now?
Avatar of kevp75

ASKER

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...
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kevp75

ASKER

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
Avatar of kevp75

ASKER

(
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
)
Avatar of clockwatcher
clockwatcher

Paste your entire code... forcing the type match should fix the problem.  It does in my tests.
Avatar of kevp75

ASKER

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
                        %>
Avatar of kevp75

ASKER

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....
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
Avatar of kevp75

ASKER

DOH!

thanks a ton clock!
Avatar of kevp75

ASKER

one more time.....if anyone is interested....
https://www.experts-exchange.com/questions/21938504/Drop-Down-List.html