znalazłem skrypt w necie , ale nie rozumień tu taj powiązań pomiędzy kategorią a podkategorią
tabela i przykładowe dane
CREATE TABLE `testcat` ( `id` mediumint(8) unsigned NOT NULL auto_increment, `title` varchar(200) NOT NULL, `left_id` int(10) NOT NULL, `right_id` int(10) NOT NULL, `level` mediumint(8) unsigned NOT NULL, ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ; -- -- Dumping data for table `testcat` -- INSERT INTO `testcat` VALUES (1, 'Digital Art', 9, 20, 1); INSERT INTO `testcat` VALUES (2, '3D Art', 10, 11, 2); INSERT INTO `testcat` VALUES (3, 'Drawings', 12, 13, 2); INSERT INTO `testcat` VALUES (4, 'Miscellaneous', 14, 15, 2); INSERT INTO `testcat` VALUES (5, 'Photo Manipulation', 16, 17, 2); INSERT INTO `testcat` VALUES (6, 'Wallpapers', 18, 19, 2); INSERT INTO `testcat` VALUES (7, 'Internet Use', 20, 25, 2); INSERT INTO `testcat` VALUES (8, 'Signatures', 21, 22, 3); INSERT INTO `testcat` VALUES (9, 'Avatars', 23, 24, 3); INSERT INTO `testcat` VALUES (10, 'Photography', 1, 8, 1); INSERT INTO `testcat` VALUES (11, 'Landscape', 6, 7, 2); INSERT INTO `testcat` VALUES (12, 'Portrait', 4, 5, 2); INSERT INTO `testcat` VALUES (13, 'Architecture', 2, 3, 2);
i skrypt
<?php $sql = 'SELECT id, left_id, right_id, level, title FROM testcat ORDER BY left_id asc'; $level = 1; $first = false; { while($level > $row['level']) { $level--; } if(has_children($row['left_id'], $row['right_id'])) { print str_repeat("\t", $row['level']) . '<optgroup class="' . $classes['cat'][$row['level']] . '" label="' . $row['title'] . '">' . "\n"; $level++; $first = true; continue; } print str_repeat("\t", $row['level']) . '<option class="' . $classes['element'][$row['level']] . (($first) ? ' first' : '') . '" value="' . $row['id'] . '">' . $row['title'] . '</option>' . "\n"; $first = false; } while($level > 1) { $level--; } function has_children($left_id, $right_id) { // MPTT is good! return (bool) ( ($right_id - $left_id - 1) / 2 ); } <?php $sql = 'SELECT id, left_id, right_id, level, title FROM testcat ORDER BY left_id asc'; $level = 1; $first = false; { while($level > $row['level']) { $level--; } if(has_children($row['left_id'], $row['right_id'])) { print str_repeat("\t", $row['level']) . '<optgroup class="' . $classes['cat'][$row['level']] . '" label="' . $row['title'] . '">' . "\n"; $level++; $first = true; continue; } print str_repeat("\t", $row['level']) . '<option class="' . $classes['element'][$row['level']] . (($first) ? ' first' : '') . '" value="' . $row['id'] . '">' . $row['title'] . '</option>' . "\n"; $first = false; } while($level > 1) { $level--; } function has_children($left_id, $right_id) { // MPTT is good! return (bool) ( ($right_id - $left_id - 1) / 2 ); }