PHP/MySQL Category Hierarchy
Anyone have any suggestions for stuff to read on working with categories and sub-categories with PHP and MySQL ?
Currently I use a category table like this.
I've removed fields irrelevant to category hierarchy such as "label" and "description".
When I need to know all of the descendants of any given category, I do something like this.
Which will give me something the following when I use print_r on $c->lineage.
Various routines like that have been able to do anything I need, so far. They're fairly simple as far as I'm concerned as well, which I like. I've looked at solutions to categories such as the "left_id, right_id" solution phpBB3 uses and that just seems really complicated to me.
But anyways, anyone have any suggestions for stuff to read on dealing with hierarchies with PHP and MySQL ?
Currently I use a category table like this.
I've removed fields irrelevant to category hierarchy such as "label" and "description".
CREATE TABLE IF NOT EXISTS `categories` (
`category_id` smallint(5) unsigned NOT NULL auto_increment,
`parent_id` smallint(6) unsigned default NULL,
PRIMARY KEY (`category_id`),
KEY `parent_id` (`parent_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
`category_id` smallint(5) unsigned NOT NULL auto_increment,
`parent_id` smallint(6) unsigned default NULL,
PRIMARY KEY (`category_id`),
KEY `parent_id` (`parent_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
- CREATE TABLE IF NOT EXISTS `categories` (
- `category_id` smallint(5) unsigned NOT NULL auto_increment,
- `parent_id` smallint(6) unsigned default NULL,
- PRIMARY KEY (`category_id`),
- KEY `parent_id` (`parent_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
When I need to know all of the descendants of any given category, I do something like this.
function lineage($branch)
{
$decendants = array();
foreach($branch->sub_categories as $key => $val)
{
$decendants[] = $key;
$decendants = array_merge($decendants, lineage($val));
}
return $decendants;
}
$c = new stdClass;
$c->lineage = array();
$result = $db->query('SELECT category_id, parent_id FROM ' . CATEGORIES_TABLE, MYSQLI_USE_RESULT);
while($row = $result->fetch_object())
{
$row->sub_categories = array();
$c->lineage[$row->category_id] = $row;
}
$result->close();
foreach($c->lineage as $key => $val)
{
if($val->parent_id)
{
$c->lineage[$val->parent_id]->sub_categories[$key] =& $c->lineage[$key];
}
}
foreach($c->lineage as $key => $val)
{
$c->lineage[$key]->decendants = lineage($val);
}
{
$decendants = array();
foreach($branch->sub_categories as $key => $val)
{
$decendants[] = $key;
$decendants = array_merge($decendants, lineage($val));
}
return $decendants;
}
$c = new stdClass;
$c->lineage = array();
$result = $db->query('SELECT category_id, parent_id FROM ' . CATEGORIES_TABLE, MYSQLI_USE_RESULT);
while($row = $result->fetch_object())
{
$row->sub_categories = array();
$c->lineage[$row->category_id] = $row;
}
$result->close();
foreach($c->lineage as $key => $val)
{
if($val->parent_id)
{
$c->lineage[$val->parent_id]->sub_categories[$key] =& $c->lineage[$key];
}
}
foreach($c->lineage as $key => $val)
{
$c->lineage[$key]->decendants = lineage($val);
}
- function lineage($branch)
- {
- $decendants = array();
- foreach($branch->sub_categories as $key => $val)
- {
- $decendants[] = $key;
- $decendants = array_merge($decendants, lineage($val));
- }
- return $decendants;
- }
- $c = new stdClass;
- $c->lineage = array();
- $result = $db->query('SELECT category_id, parent_id FROM ' . CATEGORIES_TABLE, MYSQLI_USE_RESULT);
- while($row = $result->fetch_object())
- {
- $row->sub_categories = array();
- $c->lineage[$row->category_id] = $row;
- }
- $result->close();
- foreach($c->lineage as $key => $val)
- {
- if($val->parent_id)
- {
- $c->lineage[$val->parent_id]->sub_categories[$key] =& $c->lineage[$key];
- }
- }
- foreach($c->lineage as $key => $val)
- {
- $c->lineage[$key]->decendants = lineage($val);
- }
Which will give me something the following when I use print_r on $c->lineage.
Array
(
[1] => stdClass Object
(
[category_id] => 1
[parent_id] =>
[sub_categories] => Array
(
[2] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[sub_categories] => Array
(
[3] => stdClass Object
(
[category_id] => 3
[parent_id] => 2
[sub_categories] => Array
(
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
)
[decendants] => Array
(
[0] => 4
[1] => 5
)
)
)
[decendants] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
)
)
[decendants] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
)
[2] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[sub_categories] => Array
(
[3] => stdClass Object
(
[category_id] => 3
[parent_id] => 2
[sub_categories] => Array
(
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
)
[decendants] => Array
(
[0] => 4
[1] => 5
)
)
)
[decendants] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
)
[3] => stdClass Object
(
[category_id] => 3
[parent_id] => 2
[sub_categories] => Array
(
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
)
[decendants] => Array
(
[0] => 4
[1] => 5
)
)
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
(
[1] => stdClass Object
(
[category_id] => 1
[parent_id] =>
[sub_categories] => Array
(
[2] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[sub_categories] => Array
(
[3] => stdClass Object
(
[category_id] => 3
[parent_id] => 2
[sub_categories] => Array
(
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
)
[decendants] => Array
(
[0] => 4
[1] => 5
)
)
)
[decendants] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
)
)
[decendants] => Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
)
[2] => stdClass Object
(
[category_id] => 2
[parent_id] => 1
[sub_categories] => Array
(
[3] => stdClass Object
(
[category_id] => 3
[parent_id] => 2
[sub_categories] => Array
(
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
)
[decendants] => Array
(
[0] => 4
[1] => 5
)
)
)
[decendants] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
)
[3] => stdClass Object
(
[category_id] => 3
[parent_id] => 2
[sub_categories] => Array
(
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
)
[decendants] => Array
(
[0] => 4
[1] => 5
)
)
[4] => stdClass Object
(
[category_id] => 4
[parent_id] => 3
[sub_categories] => Array
(
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
[decendants] => Array
(
[0] => 5
)
)
[5] => stdClass Object
(
[category_id] => 5
[parent_id] => 4
[sub_categories] => Array
(
)
[decendants] => Array
(
)
)
)
- Array
- (
- [1] => stdClass Object
- (
- [category_id] => 1
- [parent_id] =>
- [sub_categories] => Array
- (
- [2] => stdClass Object
- (
- [category_id] => 2
- [parent_id] => 1
- [sub_categories] => Array
- (
- [3] => stdClass Object
- (
- [category_id] => 3
- [parent_id] => 2
- [sub_categories] => Array
- (
- [4] => stdClass Object
- (
- [category_id] => 4
- [parent_id] => 3
- [sub_categories] => Array
- (
- [5] => stdClass Object
- (
- [category_id] => 5
- [parent_id] => 4
- [sub_categories] => Array
- (
- )
- [decendants] => Array
- (
- )
- )
- )
- [decendants] => Array
- (
- [0] => 5
- )
- )
- )
- [decendants] => Array
- (
- [0] => 4
- [1] => 5
- )
- )
- )
- [decendants] => Array
- (
- [0] => 3
- [1] => 4
- [2] => 5
- )
- )
- )
- [decendants] => Array
- (
- [0] => 2
- [1] => 3
- [2] => 4
- [3] => 5
- )
- )
- [2] => stdClass Object
- (
- [category_id] => 2
- [parent_id] => 1
- [sub_categories] => Array
- (
- [3] => stdClass Object
- (
- [category_id] => 3
- [parent_id] => 2
- [sub_categories] => Array
- (
- [4] => stdClass Object
- (
- [category_id] => 4
- [parent_id] => 3
- [sub_categories] => Array
- (
- [5] => stdClass Object
- (
- [category_id] => 5
- [parent_id] => 4
- [sub_categories] => Array
- (
- )
- [decendants] => Array
- (
- )
- )
- )
- [decendants] => Array
- (
- [0] => 5
- )
- )
- )
- [decendants] => Array
- (
- [0] => 4
- [1] => 5
- )
- )
- )
- [decendants] => Array
- (
- [0] => 3
- [1] => 4
- [2] => 5
- )
- )
- [3] => stdClass Object
- (
- [category_id] => 3
- [parent_id] => 2
- [sub_categories] => Array
- (
- [4] => stdClass Object
- (
- [category_id] => 4
- [parent_id] => 3
- [sub_categories] => Array
- (
- [5] => stdClass Object
- (
- [category_id] => 5
- [parent_id] => 4
- [sub_categories] => Array
- (
- )
- [decendants] => Array
- (
- )
- )
- )
- [decendants] => Array
- (
- [0] => 5
- )
- )
- )
- [decendants] => Array
- (
- [0] => 4
- [1] => 5
- )
- )
- [4] => stdClass Object
- (
- [category_id] => 4
- [parent_id] => 3
- [sub_categories] => Array
- (
- [5] => stdClass Object
- (
- [category_id] => 5
- [parent_id] => 4
- [sub_categories] => Array
- (
- )
- [decendants] => Array
- (
- )
- )
- )
- [decendants] => Array
- (
- [0] => 5
- )
- )
- [5] => stdClass Object
- (
- [category_id] => 5
- [parent_id] => 4
- [sub_categories] => Array
- (
- )
- [decendants] => Array
- (
- )
- )
- )
Various routines like that have been able to do anything I need, so far. They're fairly simple as far as I'm concerned as well, which I like. I've looked at solutions to categories such as the "left_id, right_id" solution phpBB3 uses and that just seems really complicated to me.
But anyways, anyone have any suggestions for stuff to read on dealing with hierarchies with PHP and MySQL ?
Why yes, yes I am.
- Anonymous
- Bot


- Joined: 25 Feb 2008
- Posts: ?
- Loc: Ozzuland
- Status: Online
October 31st, 2009, 3:05 pm
Page 1 of 1
To Reply to this topic you need to LOGIN or REGISTER. It is free.
Post Information
- Total Posts in this topic: 1 post
- Users browsing this forum: No registered users and 354 guests
- You cannot post new topics in this forum
- You cannot reply to topics in this forum
- You cannot edit your posts in this forum
- You cannot delete your posts in this forum
- You cannot post attachments in this forum


