Print Page | Close Window

Categories and sub categories

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=2738
Printed Date: 29 March 2026 at 8:34pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Categories and sub categories
Posted By: simflex
Subject: Categories and sub categories
Date Posted: 15 May 2003 at 12:20pm

I have a couple of questions, please.

Please take a look at the code below and see if you can help me.

I am writing a shopping cart program and my boss decided we need to have a treeview of categories.

The spec says:

If you click a category, it should expand to give you a list of subcategories.

Both categories and subcategories are database fields.

Then if you click on any of the subcategories, it should give you

a bried description of that subcategory field.

If you like it, add it to your cart. If you don't go back to treeview and make another selection.

I could not come with anything easy.

After doing an extensively devouring online resources, I came up with this code.

My two questions are:

1, can you please help make it possible to have more than one subcategory?

So far, I am getting one subcategory per category.

I need to be able to display all the subcategories in the database.

When I go to query analyzer (sql server), all subcategories belonging to a category are displayed.

The second question is, how can I hyperlink the subcategory so it takes me to description page where each category is given a brief description?

A response to any or all of the questions will be greatly appreciated.

If there is a link that can satisfy any or all of the above needs, please let me have that too.

Thanks in advance.

here is the code I have so far:

<form action='treeview.asp' method='post'>
<%

Dim filePath
Dim oConnect
Dim oResult
Dim Racine
Dim Item
Dim catOld
Dim DiscOld

catOld=""
DiscOld=""
Racine=4
Item=0

' ************ Map authors database to physical path

'filePath = Server.MapPath("db1.mdb")

' ************ Create ADO Connection Component to connect with sample database

Set oConnect = Server.CreateObject("ADODB.Connection")
oConnect.Open "dsn=scart"

'*************  SQL query for the Category

Set oResult = oConnect.Execute(" select categories.catdescription, products.cname from categories,products where categories.categoryid = products.ccategory order by categories.catdescription " )

do while (Not oResult.eof)

 if oResult("catdescription")<> catOld then
  if catOld <> "" then
   WriteFoot
   WriteFoot
  end if

  '************** Categories **********

  WriteHead "My"&Racine&"TreeView",Item, oResult("catdescription") ,0,True,False,"",False
  catOld=oResult("catdescription")
  Item=Item+1

 

  '************** SubCategories **********

  WriteHead "My"&Racine&"TreeView",Item, oResult("cname") ,1,False,False,"",False
  DiscOld=oResult("cname")
  Item=Item+1

 end if

 Racine=Racine+1
 oResult.MoveNext
loop
WriteFoot
WriteFoot


%>
</form>




Replies:
Posted By: Mart
Date Posted: 15 May 2003 at 2:13pm

Well to get the description page just make ID numbers for things that can be selected and link to desc.asp?ID=** where ** is your ID number then just a simple SELECT scart.* from scart WHERE ID = '" & clng(request.querystring("ID")) & "';"

Martin.



Posted By: simflex
Date Posted: 15 May 2003 at 2:35pm

example?

catdescription is the category Name, cname is the subcategoryname

and cdescription is the description of the item.

Now given the code snippet above, how will that be coded?

Normally, I would use this syntax to accomplish second question:
               Response.Write "<a href=""treeview.asp?categoryID=" & oResult("categoryID") & """>"
Response.Write rs("cdescription") &



Posted By: Ignorance
Date Posted: 15 May 2003 at 4:52pm
Originally posted by simflex simflex wrote:

1, can you please help make it possible to have more than one subcategory?

I too am trying to do something similar to this and the best thing I could find that was close was going to http://www.planet-source-code.com - www.planet-source-code.com and doing a search for "recursive" in the VB and ASP sections. I haven't had time to rebuild the code there to get the desired effect but it should atleast give you a starting point. Hope this helps. If you get something working post it for us. I would really like to see how you end up doing it.



Posted By: ljamal
Date Posted: 15 May 2003 at 5:11pm
The way I accomplish this at http://www.bullcitycomics.com - Bull City Comics
is I have one table with all the categories. Subcategories are linked to other categories via a ParentID which references the Parent. A category can not be a parent to itself.

I query the database to return the ID, PArentID and name of all the categories and store that to an array. A recursive function writes out the tree structure by iterating through the array. A tree is opened by having the ID present in a cookie otherwise it is closed. The tree also indents itself to offset it for the its parent.

This structure allow for an infinite level of subcategories. I've tested it to over 200 levels without any problems. The only thing I have been able to do with the infinite structure is create a bread crumb trail that works correctly.


Posted By: simflex
Date Posted: 16 May 2003 at 5:55am

Ijamal, do you mind sending me a code snippet that you talked about at mailto:simflex@hotmail.com - simflex@hotmail.com

I would really appreciate it.

Ignorance, I will look into the recursive code sample from planetsource.

Thanks to both of you!



Posted By: ljamal
Date Posted: 16 May 2003 at 8:21am
I'll just post it here so every one can use it.
It call some other functions that I use on a regular basis.

SetNumericValue checks the value to insure it's numeric. If not then it sets it to the supplied default. In this case 0.

There is a cookie "main" with "strCat" as one of it's elements. strCat is a string of CategoryID surrounded by delineaters ("|"). This cookie value is strValues

strIndent is the string that stores the indenting usually a non breaking space or two. intIndent store the current level of indentation.

arrCat is a two dimensional array. The first array contains the information. The second the position in the record set. The information in the first array are
0 - categoryID
1 - parent category ID
2 - category name

Clicking on the link that closes part of the tree (collapse=1) in the QueryString removes the Category ID from the cookie. Any other click on the category adds the Category ID to the cookie (opening the tree)

The last category clicked is made bold.

<%
' SubRoutine to Display Category
' Requires "|" delineated values for strValues
' Recursive Subroutine
     intCatID = SetNumericValue(Request("CatID"),0)
     Sub DisplayCat (numParentID, strValues, IntIndent)
          For i = 0 to Ubound(arrCat,2)
               if IsNull(arrCat(1,i)) then arrCat(1,i)=0 end if
               if      arrCat(1,i)=numParentID then
               '     response.Write "<nobr>"
                    if not IsNumeric(intIndent)=True then intIndent=0 end if
                    For j=0 to intIndent
                         Response.Write strIndent
                    Next
                    
                    if CLng(intCatID)=Clng(arrCat(0,i)) then Response.Write "<B><i>" end if
                    if inStr(strValues,"|"&arrCat(0,i)&"|")>0 and inStr(Request("url"),strCart)=0 then
                         Response.Write "<a href="""&strCategory&"?CatID="&arrCat(0,i)&"&Collapse=1"">"&arrCat(2,i)&"</a>
"&vbcrlf
                    else
                         Response.Write "<a href="""&strCategory&"?CatID="&arrCat(0,i)&""">"&arrCat(2,i)&"</a>
"&vbcrlf
                    end if
                    if CLng(intCatID)=Clng(arrCat(0,i)) then Response.Write "</i></B>" end if
     
                    if Instr(strValues,"|"&arrCat(0,i)&"|")>0 and arrCat(0,i)>0 then
                         Call DisplayCat(arrCat(0,i), strValues, intIndent+1)
                    end if
               end if
          Next
     End Sub
%>


Posted By: simflex
Date Posted: 17 May 2003 at 2:56pm

hi Ijamal!

thanks for this post, I really appreciate it.

I was hoping to see everything including the select statement.

You can change your tablename, if you have to.

Thanks again.



Posted By: ljamal
Date Posted: 17 May 2003 at 3:30pm
I'll have to post it later.
Since I run SQLServer I do everything in stored procedures and arrays and since its part of an ecommerce solution, I'll have to stroll through the code.


Posted By: simflex
Date Posted: 17 May 2003 at 4:49pm

thanks a bunch.

I am a sql server stored proc developer myself.

I appreciate your help!!



Posted By: simflex
Date Posted: 17 June 2003 at 2:01pm

hi,

are you still scrolling through the code?



Posted By: ljamal
Date Posted: 17 June 2003 at 6:29pm
Nope, just haven't gotten around to it.

-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net