Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Galera é nós!
Qual é a lógica em PHP para listar categoria com subcategorias infinitas.
E só uma tabela então o select tem que ser recursivo.
Exemplo:
categoria
|__ subcategoria 1
|__ subcategoria 2
| |__ subcategoria 2.1
| |__ subcategoria 2.2
| |__ subcategoria 2.2.1
|__ subcategoria 3
|__ subcategoria 3.1
|__ subcategoria 3.2
|__ subcategoria 3.2.1
Minha tabela
CREATE TABLE category (id_category int(11) UNSIGNED NOT NULL AUTO_INCREMENT ,id_parent int(11) UNSIGNED NOT NULL ,id_store_default int(11) UNSIGNED NOT NULL DEFAULT 1 ,name varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,level_depth tinyint(3) UNSIGNED NOT NULL DEFAULT 0 ,nleft int(10) UNSIGNED NOT NULL DEFAULT 0 ,nright int(10) UNSIGNED NOT NULL DEFAULT 0 ,active tinyint(1) UNSIGNED NOT NULL DEFAULT 0 ,date_add datetime NOT NULL ,date_upd datetime NOT NULL ,position int(10) UNSIGNED NOT NULL DEFAULT 0 ,is_root_category tinyint(1) NOT NULL DEFAULT 0 ,
PRIMARY KEY (id_category),
INDEX category_parent (id_parent) USING BTREE ,
INDEX nleftright (nleft, nright) USING BTREE ,
INDEX nleftrightactive (nleft, nright, active) USING BTREE ,
INDEX level_depth (level_depth) USING BTREE ,
INDEX nright (nright) USING BTREE ,
INDEX nleft (nleft) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
AUTO_INCREMENT=1
ROW_FORMAT=COMPACT
;
Carregando comentários...