使用PHP MySQL的类别层次结构(按顺序) [英] Category Hierarchy (in order) using PHP MySQL

查看:100
本文介绍了使用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.

          这篇关于使用PHP MySQL的类别层次结构(按顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆