[英]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.