Hi,
(I’m sorry for the summary but I’ve not other idea)
I’ve this table with a few data example
CREATE TABLE IF NOT EXISTS `dc_category` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`parent_id` int(10) unsigned DEFAULT NULL,
`title` varchar(255) NOT NULL,
`slug` varchar(255) NOT NULL,
`description` text,
`ico` varchar(255) DEFAULT NULL,
`published` int(10) NOT NULL,
`highlighted` enum('0','1') NOT NULL DEFAULT '0' COMMENT '0 no, 1 yes',
PRIMARY KEY (`id`),
UNIQUE KEY `unique_dc_category_title` (`title`),
UNIQUE KEY `unique_dc_category_slug` (`slug`),
KEY `fk_dc_category` (`parent_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `dc_category`
--
INSERT INTO `dc_category` (`id`, `parent_id`, `title`, `slug`, `description`, `ico`, `published`, `highlighted`) VALUES
(5, NULL, 'Storage devices', 'storage-devices', NULL, NULL, 1318958095, '0'),
(8, 5, 'Floppy disk', 'floppy-disk', NULL, NULL, 1318958139, '0'),
(10, 5, 'Hard disk', 'hard-disk', NULL, NULL, 1318958177, '0'),
(11, 5, 'Usb key', 'usb-key', NULL, NULL, 1318958210, '0'),
(19, NULL, 'Monitors', 'monitors', NULL, NULL, 1234567890, '0');
ALTER TABLE `dc_category`
ADD CONSTRAINT `fk_dc_category` FOREIGN KEY (`parent_id`) REFERENCES `dc_category` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
I’ve to retrieve
Storage devices > Floppy disk
Storage devices > Hard disk
Storage devices > Usb key
Monitors
not Storage devices
with a query like this (my 5 cents )
SELECT `c`.`id`, `c`.`title` FROM `dc_category` AS `c`
LEFT JOIN `dc_category` AS `cc` ON cc.id = c.parent_id ORDER BY coalesce(c.parent_id, c.id), c.id ASC
I’ve to use it to build a select to insert items
so no item in the parent (in the menu a parent category with children is unclickabled)
Can you help me, please ?