php - How to style codeigniter multi level menu? -
i want create multi level menu codeigniter.
my table structure this:
id -- category -- parent_id
i use code in model:
function all_category_ordered($level = 0, $prefix = '<li>') { $rows = $this->db ->select('id,category,parent_id') ->where('parent_id', $level) ->order_by('id','asc') ->get('category') ->result(); $category = null; if (count($rows) > 0) { foreach ($rows $row) { $category .= $prefix . $row->category . "\n"; // append subcategories $category .= "</li>".$this->all_category_ordered($row->id, $prefix . '*') ; } } return $category; }
it works fine , out put of code this:
<li>menu 1</li> <li>menu 2</li> <li>menu 3</li> <li>*submenu 1</li> <li>menu 4</li> <li>*submenu 2</li>
but want change model out put in ul
, li
correct order style correctly.
preferred order:
<ul> <li>menu 1</li> <li>menu 2</li> <li>menu 3 <ul> <li>*submenu 1</li> </ul> </li> <li>menu 3 <ul> <li>*submenu 1</li> </ul> </li> </ul>
any appreciated.
you're using recursive function - that's good. should stick approach, don't control when particular ul
should opened , closed , that's missing.
below find very, very old crap should give hint controlling process of opening / closing list on every level.
<?php $query = mysql_query('select menu.id, menu.id_parent, menu.link, menu.alt, library.title library library, menu menu menu.link = library.id order menu.level asc'); while($row = mysql_fetch_array($query)) { $menu_array[] = array('id'=>$row['id'], 'id_parent'=>$row['id_parent'], 'link'=>$row['link'], 'alt'=>$row['alt'], 'title'=>$row['title']); } function menu($id) { global $menu_array; $has_childs = false; $result = array(); // nothing if empty if (empty($menu_array)) { return; } foreach ($menu_array $value) { if ($value['id_parent'] == $id) { $result[] = $value; } } foreach ($result $value) { if ($has_childs === false) { $has_childs = true; if ($value['id_parent'] == 0) { echo ('<ul id="root">'); // root level } else { echo ('<ul>'); // sub levels } } echo ('<li><a href="#">'.$value['title'].'</a>'); menu($value['id']); echo ('</li>'); } if ($has_childs === true) { echo ('</ul>'); } } menu(0);
again, adjust it. remove query , replace ci model returns. see if structure similar. give example function menu()
.
edit:
here's db structure.
create table if not exists `menu` ( `id` int(11) not null, `id_parent` int(11) not null default '0', `link` int(11) not null default '0', `level` int(11) not null default '0', `alt` varchar(255) collate utf8_polish_ci not null default '' ) engine=myisam auto_increment=74 default charset=utf8 collate=utf8_polish_ci; -- -- zrzut danych tabeli `menu` -- insert `menu` (`id`, `id_parent`, `link`, `level`, `alt`) values (66, 0, 197, 3, 'strona główna'), (3, 2, 199, 1, 'kanał rss'), (4, 2, 200, 2, 'archiwum newsów'), (5, 0, 24, 6, 'depeche mode'), (6, 5, 1, 1, 'biografia grupy'), (7, 10, 26, 1, 'biografia'), (8, 15, 94, 1, 'biografia'), (9, 19, 107, 1, 'biografia'), (10, 5, 25, 2, ''), (11, 10, 27, 2, 'albumy'), (12, 10, 29, 3, 'single'), (13, 10, 30, 4, 'video'), (14, 10, 31, 5, 'teksty'), (15, 5, 93, 3, ''), (16, 15, 95, 2, 'albumy'), (17, 15, 102, 3, 'single'), (18, 15, 103, 4, 'teksty'), (19, 5, 104, 4, ''), (20, 19, 108, 2, 'wytwórnia płytowa toast hawaii'), (21, 19, 109, 3, 'tost hawajski'), (22, 5, 105, 5, ''), (23, 22, 111, 1, 'biografia'), (24, 22, 110, 2, 'odejście z depeche mode'), (26, 22, 221, 3, 'albumy'), (27, 22, 219, 4, 'single'), (28, 22, 220, 5, 'teksty'), (29, 5, 106, 6, ''), (30, 29, 112, 1, 'biografia'), (31, 29, 113, 2, ''), (32, 0, 55, 7, 'dyskografia'), (33, 32, 164, 1, 'albumy'), (34, 32, 165, 2, 'single'), (35, 32, 166, 3, 'video'), (36, 0, 17, 8, 'teksty utworów'), (37, 0, 53, 9, 'trasy koncertowe'), (38, 37, 76, 1, ''), (39, 37, 77, 2, ''), (40, 37, 78, 3, ''), (41, 37, 79, 4, ''), (42, 37, 80, 5, ''), (43, 37, 81, 6, ''), (44, 37, 82, 7, ''), (45, 37, 83, 8, ''), (46, 37, 84, 9, ''), (47, 37, 85, 10, ''), (48, 37, 86, 11, ''), (49, 37, 87, 12, ''), (50, 37, 88, 13, ''), (51, 37, 89, 14, ''), (52, 37, 90, 15, ''), (53, 37, 91, 16, ''), (54, 0, 201, 10, 'ciekawostki'), (55, 0, 202, 11, 'fani'), (56, 0, 203, 12, 'forum'), (57, 0, 42, 13, ''), (58, 0, 196, 14, ''), (59, 0, 41, 15, ''), (60, 59, 204, 1, 'kontakt'), (61, 59, 277, 2, ''), (62, 37, 282, 17, ''), (63, 62, 280, 1, ''), (67, 0, 198, 5, 'aktualności'), (68, 67, 199, 1, 'kanał rss'), (69, 67, 200, 2, 'archiwum newsów'), (70, 62, 289, 2, ''), (71, 55, 292, 1, ''), (72, 56, 293, 1, '');
sure, feel free use it. :) said. it's old code it's working. can see pasted example here. id , id_parent - these 2 hold relation between each other, change link href put inside href
, level depth , alt title of menu entry. have stuff clean work in 100%. create controller in ci, move query part new method inside model , there go. once list generated can style in view. :)
Comments
Post a Comment