473,786 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

method for making categories/subcategories.

Greetings,

I'm working on a project that involved that has need of a categorization
system. Logically speaking, the system will have elements, these elements
will belong to at least a single category, sometimes more, additionally,
categories may also be members of any number of categories.

Well -- Thats the vision at least. I'm looking for some pointers, tutorials,
white paper, napkins, whatever explaining how this can be done.

I'm working on currently having a database entry:

category
cat_id | cat_name

category_map
map_id | cat_id | child_of

Category map would have a many to 1 relationship with category, I think. 1
entry in category will point to multiple entries in category_map.

To build the category list, I would have to walk all the category elements,
search for any children, then search each of those children for child
matches. Could get ugly with all the lookups, caching the result would help,
but it would be nice if there was some way of doing this that I'm just not
seeing. ;)

If anyone has worked on something like this before, I would really really
appreciate some pointers on how to structure this.

Thanks much,

--Brian
Jul 17 '05 #1
6 18289
I hate this.. Just as I give up and go begging for help, I find something:

http://groups.google.com/groups?hl=e...aud.ifi.uio.no

"Brian" <cpnmscg02 (@) sneakemail.com> wrote in message
news:cZ1lb.5984 52$Oz4.601263@r wcrnsc54...
Greetings,

I'm working on a project that involved that has need of a categorization
system. Logically speaking, the system will have elements, these elements
will belong to at least a single category, sometimes more, additionally,
categories may also be members of any number of categories.

Well -- Thats the vision at least. I'm looking for some pointers, tutorials, white paper, napkins, whatever explaining how this can be done.

I'm working on currently having a database entry:

category
cat_id | cat_name

category_map
map_id | cat_id | child_of

Category map would have a many to 1 relationship with category, I think. 1
entry in category will point to multiple entries in category_map.

To build the category list, I would have to walk all the category elements, search for any children, then search each of those children for child
matches. Could get ugly with all the lookups, caching the result would help, but it would be nice if there was some way of doing this that I'm just not
seeing. ;)

If anyone has worked on something like this before, I would really really
appreciate some pointers on how to structure this.

Thanks much,

--Brian

Jul 17 '05 #2
Brian wrote:
I hate this.. Just as I give up and go begging for help, I find something:

http://groups.google.com/groups?hl=e...aud.ifi.uio.no

"Brian" <cpnmscg02 (@) sneakemail.com> wrote in message
news:cZ1lb.5984 52$Oz4.601263@r wcrnsc54...
Greetings,

I'm working on a project that involved that has need of a categorization
system. Logically speaking, the system will have elements, these elements
will belong to at least a single category, sometimes more, additionally,
categories may also be members of any number of categories.

Well -- Thats the vision at least. I'm looking for some pointers,


tutorials,
white paper, napkins, whatever explaining how this can be done.

I'm working on currently having a database entry:

category
cat_id | cat_name

category_ma p
map_id | cat_id | child_of

Category map would have a many to 1 relationship with category, I think. 1
entry in category will point to multiple entries in category_map.

To build the category list, I would have to walk all the category


elements,
search for any children, then search each of those children for child
matches. Could get ugly with all the lookups, caching the result would


help,
but it would be nice if there was some way of doing this that I'm just not
seeing. ;)

If anyone has worked on something like this before, I would really really
appreciate some pointers on how to structure this.

Thanks much,

--Brian



Hi Brian,

I'll give my AU 2c anyway.

Some people like to say that the simple 'parent_id, child_id' type
relationship isn't an intelligent enough solution, and is quite
inffefficent.

While this may be true for some cases, the fact is all the methods of
tree storage, you either shift the processing time to tree
generation-time, or shift it too creation time, and in the end if you
are using PHP to generate a listing, you still need to do some
processing on it!

In most of the programming that I've needed to do, creation/modification
times [of a tree] is essential, so doing complex rebuilds for the sake
of generation-time efficiency, isn't an option. A content management
system I've created has over 100,000 documents, each document
categorised by a large number of categories, which uses the simple
'parent, child' table relationship!

That said, for some of the 'solutions' stated in many of the alternative
tree storage documents, they are difficult to implement in simpler
DBMS's (such as MySQL) and generally just a waste of human time, even
though technically cool :).

The biggest issue with simple tree traversal is the DBMS quieries, as
you stated earlier, as you step down the tree you would need to do a
query to find children etc... Resulting in a minimum of one query per
category! Not very efficent.

The solution? Do it in PHP with ONE function (as I said before, if your
going to generate the tree in HTML or whatever, you will still need to
do some PHP anyway, so why not just trade the load from the DBMS, and
leave it up to the PHP thread), do the tree in ONE query.

Benifits:
- You can store a tree simply. Good old 'parent, child' thing.
- You only do one query.
- You leave the dirty work for PHP, and don't thrash the DBMS.
- You can modify the tree easily, relates to the first point.
For my example, let's say we have two tables.
- 'hierarchy' (category_id, child_category_ id) and
- 'category' (category_id, title)

The 'hierarchy' has a crap load of rows, and there is equally as many
categories.

-So to create a tree, first do a simply query, say.

$sql = "SELECT c.category_id, h.category_id AS parent_id, c.title
FROM heirarchy h, category c
WHERE h.child_categor y_id = c.category_id";
- This makes a rather large list, but who really cares.
- Now Stick it in an array.

$listing = Array ();
while($row = getrow($result) )
{

$listing[] = array(
'parent_id' => $row['parent_id'],
'category_id' => $row['category_id'],
'category_title ' => $row['title']
);
}

- Done the first part. Your DBMS loves you, all in one query.
- Time to generate the 'tree', you need a recursive PHP function for this.
// Create a function
function display_tree($p arent, $level, $array)
{

foreach ($array AS $node)
{
// Indent
for ($i = 0; $i < $level; $i++) $html .= '&nbsp;'

if ($parent == $node['parent_id'])
{

$html .= $row['category_title '].'<br />';

// Get the children of this node
$html .= display_tree($n ode['category_id'], ($level + 1), $array);
} // end parent

} // end foreach

return $html;

} // end display list
// Now use it
echo display_tree(0, 0, $listing);
Done!
That's my 2c. Hope it helps, just another solution to the problem. IMHO
it does the job really well, at least for me. I use this approach on a
tree with around ~3000 nodes and it only takes a few milliseconds to create.
Jul 17 '05 #3
Brian wrote:
I hate this.. Just as I give up and go begging for help, I find something:

http://groups.google.com/groups?hl=e...aud.ifi.uio.no

"Brian" <cpnmscg02 (@) sneakemail.com> wrote in message
news:cZ1lb.5984 52$Oz4.601263@r wcrnsc54...
Greetings,

I'm working on a project that involved that has need of a categorization
system. Logically speaking, the system will have elements, these elements
will belong to at least a single category, sometimes more, additionally,
categories may also be members of any number of categories.

Well -- Thats the vision at least. I'm looking for some pointers,


tutorials,
white paper, napkins, whatever explaining how this can be done.

I'm working on currently having a database entry:

category
cat_id | cat_name

category_ma p
map_id | cat_id | child_of

Category map would have a many to 1 relationship with category, I think. 1
entry in category will point to multiple entries in category_map.

To build the category list, I would have to walk all the category


elements,
search for any children, then search each of those children for child
matches. Could get ugly with all the lookups, caching the result would


help,
but it would be nice if there was some way of doing this that I'm just not
seeing. ;)

If anyone has worked on something like this before, I would really really
appreciate some pointers on how to structure this.

Thanks much,

--Brian



Hi Brian,

I'll give my AU 2c anyway.

Some people like to say that the simple 'parent_id, child_id' type
relationship isn't an intelligent enough solution, and is quite
inffefficent.

While this may be true for some cases, the fact is all the methods of
tree storage, you either shift the processing time to tree
generation-time, or shift it too creation time, and in the end if you
are using PHP to generate a listing, you still need to do some
processing on it!

In most of the programming that I've needed to do, creation/modification
times [of a tree] is essential, so doing complex rebuilds for the sake
of generation-time efficiency, isn't an option. A content management
system I've created has over 100,000 documents, each document
categorised by a large number of categories, which uses the simple
'parent, child' table relationship!

That said, for some of the 'solutions' stated in many of the alternative
tree storage documents, they are difficult to implement in simpler
DBMS's (such as MySQL) and generally just a waste of human time, even
though technically cool :).

The biggest issue with simple tree traversal is the DBMS quieries, as
you stated earlier, as you step down the tree you would need to do a
query to find children etc... Resulting in a minimum of one query per
category! Not very efficent.

The solution? Do it in PHP with ONE function (as I said before, if your
going to generate the tree in HTML or whatever, you will still need to
do some PHP anyway, so why not just trade the load from the DBMS, and
leave it up to the PHP thread), do the tree in ONE query.

Benifits:
- You can store a tree simply. Good old 'parent, child' thing.
- You only do one query.
- You leave the dirty work for PHP, and don't thrash the DBMS.
- You can modify the tree easily, relates to the first point.
For my example, let's say we have two tables.
- 'hierarchy' (category_id, child_category_ id) and
- 'category' (category_id, title)

The 'hierarchy' has a crap load of rows, and there is equally as many
categories.

-So to create a tree, first do a simply query, say.

$sql = "SELECT c.category_id, h.category_id AS parent_id, c.title
FROM heirarchy h, category c
WHERE h.child_categor y_id = c.category_id";
- This makes a rather large list, but who really cares.
- Now Stick it in an array.

$listing = Array ();
while($row = getrow($result) )
{

$listing[] = array(
'parent_id' => $row['parent_id'],
'category_id' => $row['category_id'],
'category_title ' => $row['title']
);
}

- Done the first part. Your DBMS loves you, all in one query.
- Time to generate the 'tree', you need a recursive PHP function for this.
// Create a function
function display_tree($p arent, $level, $array)
{

foreach ($array AS $node)
{
// Indent
for ($i = 0; $i < $level; $i++) $html .= '&nbsp;'

if ($parent == $node['parent_id'])
{

$html .= $row['category_title '].'<br />';

// Get the children of this node
$html .= display_tree($n ode['category_id'], ($level + 1), $array);
} // end parent

} // end foreach

return $html;

} // end display list
// Now use it
echo display_tree(0, 0, $listing);
Done!
That's my 2c. Hope it helps, just another solution to the problem. IMHO
it does the job really well, at least for me. I use this approach on a
tree with around ~3000 nodes and it only takes a few milliseconds to create.
Jul 17 '05 #4

"Brian" <cpnmscg02 (@) sneakemail.com> wrote in message
news:cZ1lb.5984 52$Oz4.601263@r wcrnsc54...
Greetings,
I'm working on a project that involved that has need of a categorization
system. Logically speaking, the system will have elements, these elements
will belong to at least a single category, sometimes more, additionally,
categories may also be members of any number of categories.
Well -- Thats the vision at least. I'm looking for some pointers, tutorials, white paper, napkins, whatever explaining how this can be done.


I'm working with a product catalog system. My categories table is something
like this:

id
name
parent_id
generation

why I added generation? because I want to be sure that there are no
recursive categories where a is child of b and b is child of a.

when I add category, I 1) add it under some category with generation one
less than the parent OR 2) find the lowest generation that exists.

I made a category class in order to make sure that my operations go right
always.
Member functions like
category->insert
category->delete
category->destroy
should return true if successfull or false (and error message) if failure.

Other normal functions:
get_lowest_gen( ) returns lowest generation (integer)
get_children($c at_id) returns in array of integers the children of $cat_id
build_cat_tree( ) should order all categories - AND YOU DON'T NEED to do one
database query for every category. Just find the lowest generation then
start to recursively go trough every category in that generation
(get_children). Function needs perhaps to add some more indent to every
generation, if it prints something.

It is very good idea to test the class separately with as simple interface
as possible, in order to be sure.


Jul 17 '05 #5
Wow, what a great post, thank you for taking the time to write that.

I'm going to take some time to absorb it, and I'll get back to you.

-Brian

"Unleaded" <un******@unlea dedonline.net.n 0spam> wrote in message
news:3F******** ******@unleaded online.net.n0sp am...
Brian wrote:
I hate this.. Just as I give up and go begging for help, I find something:
http://groups.google.com/groups?hl=e...aud.ifi.uio.no
"Brian" <cpnmscg02 (@) sneakemail.com> wrote in message
news:cZ1lb.5984 52$Oz4.601263@r wcrnsc54...
Greetings,

I'm working on a project that involved that has need of a categorization
system. Logically speaking, the system will have elements, these elementswill belong to at least a single category, sometimes more, additionally,
categories may also be members of any number of categories.

Well -- Thats the vision at least. I'm looking for some pointers,
tutorials,
white paper, napkins, whatever explaining how this can be done.

I'm working on currently having a database entry:

category
cat_id | cat_name

category_ma p
map_id | cat_id | child_of

Category map would have a many to 1 relationship with category, I think. 1entry in category will point to multiple entries in category_map.

To build the category list, I would have to walk all the category


elements,
search for any children, then search each of those children for child
matches. Could get ugly with all the lookups, caching the result would


help,
but it would be nice if there was some way of doing this that I'm just notseeing. ;)

If anyone has worked on something like this before, I would really reallyappreciate some pointers on how to structure this.

Thanks much,

--Brian



Hi Brian,

I'll give my AU 2c anyway.

Some people like to say that the simple 'parent_id, child_id' type
relationship isn't an intelligent enough solution, and is quite
inffefficent.

While this may be true for some cases, the fact is all the methods of
tree storage, you either shift the processing time to tree
generation-time, or shift it too creation time, and in the end if you
are using PHP to generate a listing, you still need to do some
processing on it!

In most of the programming that I've needed to do, creation/modification
times [of a tree] is essential, so doing complex rebuilds for the sake
of generation-time efficiency, isn't an option. A content management
system I've created has over 100,000 documents, each document
categorised by a large number of categories, which uses the simple
'parent, child' table relationship!

That said, for some of the 'solutions' stated in many of the alternative
tree storage documents, they are difficult to implement in simpler
DBMS's (such as MySQL) and generally just a waste of human time, even
though technically cool :).

The biggest issue with simple tree traversal is the DBMS quieries, as
you stated earlier, as you step down the tree you would need to do a
query to find children etc... Resulting in a minimum of one query per
category! Not very efficent.

The solution? Do it in PHP with ONE function (as I said before, if your
going to generate the tree in HTML or whatever, you will still need to
do some PHP anyway, so why not just trade the load from the DBMS, and
leave it up to the PHP thread), do the tree in ONE query.

Benifits:
- You can store a tree simply. Good old 'parent, child' thing.
- You only do one query.
- You leave the dirty work for PHP, and don't thrash the DBMS.
- You can modify the tree easily, relates to the first point.
For my example, let's say we have two tables.
- 'hierarchy' (category_id, child_category_ id) and
- 'category' (category_id, title)

The 'hierarchy' has a crap load of rows, and there is equally as many
categories.

-So to create a tree, first do a simply query, say.

$sql = "SELECT c.category_id, h.category_id AS parent_id, c.title
FROM heirarchy h, category c
WHERE h.child_categor y_id = c.category_id";
- This makes a rather large list, but who really cares.
- Now Stick it in an array.

$listing = Array ();
while($row = getrow($result) )
{

$listing[] = array(
'parent_id' => $row['parent_id'],
'category_id' => $row['category_id'],
'category_title ' => $row['title']
);
}

- Done the first part. Your DBMS loves you, all in one query.
- Time to generate the 'tree', you need a recursive PHP function for this.
// Create a function
function display_tree($p arent, $level, $array)
{

foreach ($array AS $node)
{
// Indent
for ($i = 0; $i < $level; $i++) $html .= '&nbsp;'

if ($parent == $node['parent_id'])
{

$html .= $row['category_title '].'<br />';

// Get the children of this node
$html .= display_tree($n ode['category_id'], ($level + 1), $array);
} // end parent

} // end foreach

return $html;

} // end display list
// Now use it
echo display_tree(0, 0, $listing);
Done!
That's my 2c. Hope it helps, just another solution to the problem. IMHO
it does the job really well, at least for me. I use this approach on a
tree with around ~3000 nodes and it only takes a few milliseconds to

create.

Jul 17 '05 #6
Thanks for your thoughts on this, I'll have to review the logic and see how
it fits for me.

My biggest issue is the application I'm writing has the requirement of child
categories belonging to multiple parents
Cat1 Cat6
/\ /\
/ \ / \
cat2 Cat3 / Cat7
/ \---+---+------\--+
/ \ / \ |
cat4 cat5 cat8
Screwed up, eh?

I'm thinking that the system is going to have to be a complex php function
that generates the categorys, stores it as a hash, then retreieves the hash
vs. regenerating it. The cache would be cleared and regenerated whenever a
category modifications were made.

Again, thank you.

"Perttu Pulkkinen" <Pe************ **@co.jyu.fi> wrote in message
news:_9******** *****@read3.ine t.fi...

"Brian" <cpnmscg02 (@) sneakemail.com> wrote in message
news:cZ1lb.5984 52$Oz4.601263@r wcrnsc54...
Greetings,
I'm working on a project that involved that has need of a categorization
system. Logically speaking, the system will have elements, these elements will belong to at least a single category, sometimes more, additionally,
categories may also be members of any number of categories.
Well -- Thats the vision at least. I'm looking for some pointers, tutorials,
white paper, napkins, whatever explaining how this can be done.


I'm working with a product catalog system. My categories table is

something like this:

id
name
parent_id
generation

why I added generation? because I want to be sure that there are no
recursive categories where a is child of b and b is child of a.

when I add category, I 1) add it under some category with generation one
less than the parent OR 2) find the lowest generation that exists.

I made a category class in order to make sure that my operations go right
always.
Member functions like
category->insert
category->delete
category->destroy
should return true if successfull or false (and error message) if failure.

Other normal functions:
get_lowest_gen( ) returns lowest generation (integer)
get_children($c at_id) returns in array of integers the children of $cat_id
build_cat_tree( ) should order all categories - AND YOU DON'T NEED to do one database query for every category. Just find the lowest generation then
start to recursively go trough every category in that generation
(get_children). Function needs perhaps to add some more indent to every
generation, if it prints something.

It is very good idea to test the class separately with as simple interface
as possible, in order to be sure.

Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3527
by: Troy Lynch | last post by:
I'm working on writing a website which I need to have lists of products listed in categories and subcategories, and need to keep track of whats in the tree. Like how many products from the root all the way down through the subcategories. I was thinking maybe in each category it would have a field which lists all the root categories. I hope this is a good explanation... it's kinda hard to explain I guess. I figure there were probably some...
4
3238
by: Bill | last post by:
I've got a bookstore I'm developing, and I wanted to list all the categories on the home page of the site. However, there are so many, that they now extend way below the screen, making the page look sloppy. I want, therefore, to limit the number of categories on the home page, to a few general categores, then clicking on one will reveal the subcatgories underneath. How do I go about doing this? Thanks,
0
3258
by: Ralph Guzman | last post by:
TASK: I have to generate a report with all categories, subcategories and products in database. PROBLEM: I want to write one query that will return: 1. category 2. subcategory: determined by parent_id 3. products in each category or subcategory.
1
1870
by: Neil McGuigan | last post by:
Hi, I want to store product categories in my db and am a little lost as to where to start. They can be hierarchical, such as "Books" > "Cook Books", so my table is like this: int CategoryId int ParentCategoryId Nullable
13
3883
by: hornedw | last post by:
I have been working on a ecommerce website for myself. What I needed some assistance on was when i was trying to display the categories/subcategories for the different products. I decided to use the modified preorder tree transversal algorithm. What I wanted was on the first page is to display the catogories as follows Books (35) Electronics(23) The number inside the parenthesis being the number of products in that
4
2512
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for cross-posting. I am trying to build a "checklist", where a user can navigate to an ASP page on the intranet which shows a list of "questions" that the user can check off. I am trying to figure out how to do this so that it is scalable, but I am...
3
4607
Staria
by: Staria | last post by:
Hi... I'm currently working on a project where I would like to have Categories and SubCategories. I've banged my head on this for several days, but can't seem to get this the way I want it to display. Thought I would see if I might be able to get a little help. What I would like is for my code to print out the categories/subcategories for my menu and want it to look like: Category 1 SubCategory 1 SubCategory 2 Category...
2
1837
by: DotNetNewbie | last post by:
Hello, I need to display a list of categories on the left side of a page, and I am confused how I would do this using a List Control (as appose to weaving my own HTML on the server side). My HTML should look like: <ul>
2
2664
by: itpvision | last post by:
Hello, I have a link navigation of categories retrieved from the database, while I know how to display subcategories based on category link clicked with classical approach, I want to do this with ajax, The problem is with the classical style,I know the GET sent and the row im in with the category, so by simple comparison, I know how to display the subcategories under its parent category, but with ajax, I cannot know the GET and compare...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10164
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4067
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.