A Dynamic Navigation List Bound to a Database Containing ID & ParentID (Cat & Subcat)

GeForceX

Supreme [H]ardness
Joined
Mar 19, 2003
Messages
4,172
I am a beginner web developer and I've just begun learning developing with ASP.NET in Visual Studio 2008. So my technical skills are very rudimentary. I spent five hours on this and I have gotten nowhere, really.

Just as my title said, I am attempting to create a vertical navigation list that is dynamic. By dynamic, I mean that it would retrieve its list of data from the database that contains categories and subcategories. The reason why anyone would do this is because it allows the admin to manage products in the database without having to touch the code of the website.

For example, my category table would look like this:

Code:
categoryID   categoryName   categoryParentID

1	     Entertainment  NULL
2	     Finance	    NULL
3	     Games	    NULL
5	     Adult	    1
6	     Family	    1
7	     Humor	    1
8	     Business	    2
9	     Personal	    2
10	     Stocks	    2
11	     Arcade         3
12	     Classics	    3
13	     Action	    3

Entertainment, Finance, and Games are the main categories whereas everything else are the subcategories linked to the main categories by its ParentID.

To make a dynamic navigation list for just the categories is easy: just set up a DataList and insert a LinkButton and bind it to the DataSource:

CCicZ.png


And the result would be this:

vfwYr.png


Unfortunately, it is clearly the incorrect way to do it as we have the exact list from the category table and it lacks the ideal category->subcategory listing.

The closest solution I've seen was this website: http://www.finalwebsites.com/tutorials/dynamic-navigation-list.php but it is in pure SQL and PHP which I have no understanding of just yet. But its approach is spot on and it works! I just don't know how to do it in ASP.NET.

Please, any advice on this? I am not sure where to go next. :(

Thanks.
 

cnick79

[H]ard|Gawd
Joined
Oct 5, 2004
Messages
1,833
I never used ASP.NET so I'm not sure what it is capable of, but I would loop through your data table and generate an unordered list of the parent categories and another unordered list for all of the parent's subcategories.

Have a look here, it should tell you everything you need to know.
 

GeForceX

Supreme [H]ardness
Joined
Mar 19, 2003
Messages
4,172
I agree - I should loop through but I am not sure how. I have limited programming ability. I am not sure what the logic should be actually. Would it be something like this:

Code:
categoryID = 1

loop

If ParentID = "NULL"
   Then Print as Category
ElseIf ParentID = INT
   ThenIf INT = categoryID
      Then Print as SubCategory

categoryID = categoryID + 1

EndIf

But I know this is terrible logic and that's some bad psuedocode. I have found some additional information from other websites but they are still out of my technical understanding. Your link did not really answer my question.
 

ShoeLace

Gawd
Joined
Apr 5, 2006
Messages
650
What happens if the admin deletes the Games category?

Example, Games is removed. Now Arcade is pointing to ID 3 but it no longer exists. If the admin decides to re-add Games for whatever reason it could have a different ID.

Are you going to be removing sub-categories if the parent category is removed?

You might want to re-think how you have your table setup.
 

GeForceX

Supreme [H]ardness
Joined
Mar 19, 2003
Messages
4,172
While I agree with your challenge, for my purpose, I don't think Games would ever be deleted.

However, since you see a problem with how the table is set up, can you show me the way? How should I establish this? By creating three separate tables, each focusing on a single category?

But my main concern here is how do I set up a dynamic navigation list with categories/subcategories.

Thanks!
 

tgabe213

2[H]4U
Joined
Aug 27, 2007
Messages
3,684
While I agree with your challenge, for my purpose, I don't think Games would ever be deleted.

However, since you see a problem with how the table is set up, can you show me the way? How should I establish this? By creating three separate tables, each focusing on a single category?

But my main concern here is how do I set up a dynamic navigation list with categories/subcategories.

Thanks!

I'm going to assume this is being done in VB (based on your pseudo code)?

You could maybe try looking in to something like Datasets or DataTables. Select all of your data, and loop through it. Each of the primary headings could be added to the datatable, then as you loop through each sub category, you could dynamically add it to the row after it's respective primary category heading.
 

ShoeLace

Gawd
Joined
Apr 5, 2006
Messages
650
It's not really a problem. I mean in the real world sub categories would be deleted and main categories would be too. I just threw that out there to make you aware you might want to consider adding in functionality to handle that.

I would probably have a parent and child table though.

Fields:
parent-> id, name
child-> id, cid, name

The cid in the child table would be the id of the row in the parent table.

I'd likely put a 'timestamp' field in both too, this way down the line you can add potential features like having new categories/sub-categories be posted at the top of the list or maybe include a "new" image for some period of time.
 
Top