繁体   English   中英

PHP + mySQL递归选择以创建层次结构菜单

[英]PHP + mySQL recursive select to create hierarchy menu

我正在学习PHP和mySQL,并且需要一些帮助来创建一些代码以基于父子元素构建层次结构菜单。 深层次将是1个孩子。

这是表结构:

CREATE TABLE `businessprocess` (`bp_id` int(11), `bp_order` int(11), `bp_name` varchar(50), `bp_parent` int(11), `bp_active` tinyint(1), `dp_id` int(11)) ENGINE=InnoDB;

这个数据:

INSERT INTO `businessprocess` (`bp_id`, `bp_order`, `bp_name`, `bp_parent`, `bp_active`, `dp_id`) VALUES
(1, 1000, 'Solicitud de Servicios (fijo y movil)', NULL, 1, 1),
(2, 1100, 'Personas', 1, 1, 1),
(3, 1200, 'Empresas', 1, 1, 1),
(4, 2000, 'Baja de Servicio', NULL, 1, 1),
(5, 2100, 'Personas', 4, 1, 1),
(6, 2200, 'Empresas', 4, 1, 1),
(7, 2300, 'Defunción', 4, 1, 1),
(8, 2400, 'Tercero', 4, 1, 1),
(9, 3000, 'Modificacion de Servicio', NULL, 1, 1),
(10, 4000, 'Recambio de Equipo', NULL, 1, 1),
(11, 3100, 'Personas o Empresas', 9, 1, 1),
(12, 3200, 'Tercero', 9, 1, 1),
(13, 4100, 'Personas o Empresas', 10, 1, 1),
(14, 4200, 'Tercero', 10, 1, 1);

这是PHP菜单所需的输出:

    Solicitud de Servicios (fijo y movil)
    --Personas
    --Empresas
    Baja de Servicio
    --Personas
    --Empresas
    --Defunción
    --Tercero
    Modificacion de Servicio
    --Personas o Empresas
    --Tercero
    Recambio de Equipo
    --Personas o Empresas
    --Tercero

任何帮助都非常感谢。 谢谢! 交流电

首轮

 SELECT bp_id, `bp_name` FROM `businessprocess` WHERE bp_parent IS NULL

每个返回结果运行的秒数

SELECT `bp_name` FROM `businessprocess` WHERE bp_parent IS NOT NULL  AND `bp_parent` = bp_id(gotten from the previous result)

它应该显示您所需要的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM