Build a menu + submenu from database - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Build a menu + submenu from database (/showthread.php?tid=39765) Pages:
1
2
|
Build a menu + submenu from database - El Forum - 03-20-2011 [eluser]redraw[/eluser] Dear all, sorry for my bad english. I want build a menu + submenu from database : here my database : Code: /* my model : Code: class Menumodel extends Model{ my controller : Code: function index() my view : Code: <ul class="dropdown dropdown-horizontal"> The menu and sub menu can show up on the site, but it's goes wrong user which have user_id = 'admin' it should have 3 menu menu 1 with 1 sub menu (gl) menu 2 with 2 sub menu (obat, abit) menu 3 without sub menu but in the view.php it's only show menu 1 and 2 which mean it's only show the menu that have submenu. the menu which dont have a submenu it's not show up. not only that.. menu 2 is double show up on the view.php with each sub menu.. like this menu 2 -> sub menu 1 (obat) menu 2 -> sub menu 2 (abit) not just like this : menu 2 ->2 sub menu 1 (obat) and sub menu 2 (abit) all i want is all menu show up somehow depending user access on database, not depending sub menu please help me.. thank you Build a menu + submenu from database - El Forum - 03-20-2011 [eluser]bgreene[/eluser] you need a "left join" to get records which have no sub records ie $this->db->join('comments', 'comments.id = blogs.id', 'left'); Build a menu + submenu from database - El Forum - 03-20-2011 [eluser]redraw[/eluser] @bgreene yes you right.. thank you so much but i still got a problem menu which have 2 or more sub-menu, show twice . example : menu 2 have 2 sub-menu in the site show like this : menu 2 -> sub-menu 1 menu 2 -> sub-menu 2 not just only like this : menu 2 -> sub-menu 1 & sub-menu 2 please advice.. Thanks Build a menu + submenu from database - El Forum - 03-21-2011 [eluser]bgreene[/eluser] you need a better loop, something like the following $output = ''; $crntmenu = ''; $subcount = 0; foreach rows as $row { if ($row->mainmenu == $crntmenu){ // append submenu item if ($subcount == 0) $output = <ul> $output .= <li> $row->submenu </li> $subcount++; } else { // new mainmenu item if $subcount > 0 $output .= </ul> $subcount = 0; if ($crntmenu == '') $output .= <ul><li><li> $row->mainmenu else $output .= </ul></li><li> $row->mainmenu $crntmenu = $row->mainmenu } } if ($crntmenu != '') $output .= </ul> echo $output Build a menu + submenu from database - El Forum - 03-21-2011 [eluser]redraw[/eluser] thanks for the fast answer man.. i really appreciate it. I transform your logical code above to my script : Code: <?php when i call it.. i have just one single link = http:\\localhost\mysite\index.php\home\ not menus how to fix it? i am so sory.. i am very beginner for this Thank you Build a menu + submenu from database - El Forum - 03-21-2011 [eluser]bgreene[/eluser] $output .= (the . before the = turns it into append) Build a menu + submenu from database - El Forum - 03-21-2011 [eluser]redraw[/eluser] i turn . to append : Code: if ($crntmenu != '') { when i do that. it give me parse error... please advice Build a menu + submenu from database - El Forum - 03-21-2011 [eluser]bgreene[/eluser] no,no,no you should type it as it is ie $output .= '</ul>' ".=" tells php to append the '</ul>' to whatever is already there Build a menu + submenu from database - El Forum - 03-21-2011 [eluser]redraw[/eluser] sorry for this misunderstanding i turn the code like you said (all the $output now like this : Code: $output .= '</ul>'; but it make no different.. the site just show one single link = http:\\localhost\mysite\index.php\home\ thanks Build a menu + submenu from database - El Forum - 03-21-2011 [eluser]bgreene[/eluser] $output .= '<li>'. anchor('haluser/'.$row->subprogram,$row->subprogram).'</li>'; only use echo at the very end |