使用PHP MySQL的类别层次结构(按顺序)

问题描述:

我正在尝试订购层次结构中的所有类别和子类别:

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

主要要点是如何从MySQL ORDERLY(使用 POSITION 字段)获取它们

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

  • A类猫->位置10
    • 子类别1 ->位置10
    • Sub_Sub_Cat 1 ->位置20
      • Sub_Sub_Cat 2 ->位置10
      • Cat A --> position 10
        • Sub-Cat 1 --> position 10
        • Sub_Sub_Cat 1 --> position 20
          • Sub_Sub_Cat 2 --> position 10

          MySQL代码:

           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 ;
          

您要使用SQL遍历树吗?对于邻接列表模型,这是不可能的,您必须使用嵌套集模型.然后您可以ORDER BY left以正确的顺序获取整棵树.

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.