通常我在写项目的时候,在写一些例如商城分类的时候会实现对应分类的上级分类,或者其它项目部门管理的上级部门的时候一般就会用到无限极分类来进行分类
第一步:首先在数据表设计的时候,如果要实现无限极分类,一般我会在数据表多添加一个字段pid,下面我通过一张新建的数据表来说明一下,
(1)建表:
-- ----------------------------
-- Table structure for pid
-- ----------------------------
DROP TABLE IF EXISTS `pid`;
CREATE TABLE pid (
id tinyint unsigned NOT NULL AUTO_INCREMENT primary key comment '主键id',
name varchar(32) NOT NULL,
nickname varchar(32) DEFAULT NULL,
pid tinyint(10) unsigned DEFAULT NULL,
sort mediumint(10) unsigned DEFAULT 50
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(2)插入数据:
-- ----------------------------
-- Records of pid
-- ----------------------------
INSERT INTO `pid` VALUES ('1', '科技', '', '0', '50');
INSERT INTO `pid` VALUES ('2', '军事', '', '0', '50');
INSERT INTO `pid` VALUES ('3', '人与自然', '', '0', '50');
INSERT INTO `pid` VALUES ('4', '美食', '', '