CREATE TABLE `strony` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `tytul` varchar(250) collate utf8_unicode_ci NOT NULL, `link` varchar(250) collate utf8_unicode_ci NOT NULL, `tresc` longtext collate utf8_unicode_ci, `parent_id` bigint(20) DEFAULT NULL, `sortorder` bigint(20) DEFAULT NULL, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`), KEY `parent_id_idx` (`parent_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=17 ; -- -- Zrzut danych tabeli `strony` -- INSERT INTO `strony` (`id`, `tytul`, `link`, `tresc`, `parent_id`, `sortorder`, `created_at`, `updated_at`) VALUES (1, 'aaa', 'aaa', NULL, NULL, 0, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (2, 'bbb', 'bbb', NULL, NULL, 1, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (3, 'ccc', 'ccc', NULL, NULL, 6, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (4, 'ddd', 'ddd', '', 3, 0, '2010-04-17 14:29:46', '2010-04-17 14:45:39'), (5, 'eee', 'eee', NULL, 3, 1, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (8, 'fff', 'fff', NULL, NULL, 7, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (9, 'ggg', 'ggg', NULL, NULL, 3, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (10, 'hhh', 'hhh', NULL, NULL, 4, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (11, 'iii', 'iii', NULL, 10, 0, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (12, 'jjj', 'jjj', NULL, 10, 1, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (13, 'kkk', 'kkk', NULL, 10, 2, '2010-04-17 14:29:46', '2010-04-17 14:29:46'), (14, 'lll', 'lll', 'aaa', 4, 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00'), (15, 'mmm', 'mmm', 'bbb', 5, 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00'), (16, 'nnn', 'nnn', 'ccc', 15, 0, '0000-00-00 00:00:00', '0000-00-00 00:00:00'); -- -- Ograniczenia dla zrzutów tabel -- -- -- Ograniczenia dla tabeli `strony` -- ALTER TABLE `strony` ADD CONSTRAINT `strony_parent_id_strony_id` FOREIGN KEY (`parent_id`) REFERENCES `strony` (`id`);
I kod PHP:
class Map { private $menu; private $menu_link; public function tree_menu($index) { $query = mysql_query ("SELECT *, IFNULL(parent_id, 0) AS parent FROM Strony HAVING parent = $index ORDER BY sortorder"); $this->menu.= '<ul>'; { $this->menu.= '<li>'; if($x['parent_id'] == null) $this->menu_link = null; $this->menu_link = $this->menu_link.'/'.$x['link']; $this->menu.= "<a href='".$this->menu_link."'>".$x['tytul']."</a>"; $this->tree_menu($x['id']); $this->menu.= '</li>'; } $this->menu.= '</ul>'; } public function showMap() { } } $map = new Map(); $map->tree_menu(0); $map->showMap();
Tworzy mi to poprawne drzewko mapy strony:

Ale linki są błędne, np. jak jest gałąź: ccc-eee-mmm-nnn to link powinien być postaci:
ccc/eee/mmm/nnn
a teraz mam:
ccc/ddd/lll/eee/mmm/nnn
Nie wiem już jak to zrobić żeby mi tworzyło poprawne linki, może macie jakiś pomysł ?