kuzey beytar
kuzey beytar

Reputation: 3227

Category Hierarchy (in order) using PHP MySQL

I am trying to ORDER my all categories and sub-categories in a hierarchy:

The main point is how to get them from MySQL ORDERLY (using POSITION field)

MySQL code:

 CREATE TABLE IF NOT EXISTS `categories` (
   `category_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
   `position` smallint(5) unsigned,
   `parent_id` mediumint(8) unsigned NOT NULL DEFAULT '0'
  PRIMARY KEY (`category_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;

Upvotes: 4

Views: 1842

Answers (1)

AndreKR
AndreKR

Reputation: 33658

You want to traverse the tree using SQL? That is not possible with the adjacency list model, you have to use the nested sets model. Then you can just ORDER BY left to get the whole tree in the correct order.

Upvotes: 5

Related Questions