Thinkphp dynamic menu loading

Source: Internet
Author: User
Thinkphp

Public function loadMenu (){
$ Menu = D ('menu ');
$ Lists = $ menu-> order ('Id Asc ')-> getField ('Id, menuid, menuname, parentid, URL ');
Foreach ($ lists as $ key => $ value ){
$ Lists [$ key] ['parentid'] = $ value ['parentid'];
$ Lists [$ key] ['menuid'] = $ value ['menuid'];
$ Lists [$ key] ['menuname'] = $ value ['menuname'];

$ Lists [$ key] ['OP'] = array (
'Delete' => U ('delete', array ('menuid' => $ value ['menuid']),
'Deletechilmenu' => U ('deletechildmenu1', array ('menuid' => $ value ['menuid']),
);
$ Lists [$ key] ['URL'] = $ value ['URL'];
$ Ids [] = $ value ['menuid'];

}

Dump ($ ids );
// Get a reply
$ Childmenu = D ('childmenu ');
$ Map ['menuid'] = array ('in', $ ids );
// Dump ($ ids );
$ Childmenus = $ childmenu-> where ($ map)-> order ('menuid asc ')-> select ();
// Dump ($ childmenus );
Dump ($ lists );
If (is_array ($ childmenus )){
Foreach ($ childmenus as $ key => $ value ){
$ Value ['childmenuname'] = $ value ['childmenuname'];
$ Value ['childmenurl'] = $ value ['childmenurl'];
$ Value ['OP'] = array (
'Delete' => U ('deletechilmenu', array ('childmenuid' => $ value ['childmenuid']),
);
// Print_r ($ lists [$ value ['menuid'] ['newchildmenu'] [$ value ['childmenuid']);
$ Lists [$ value ['menuid'] ['newchildmenu'] [$ value ['childmenuid'] = $ value;
Print_r ($ lists [$ value ['menuid'] ['newchildmenu'] [$ value ['childmenuid']);
}
}
Dump ($ lists );
// $ This-> assign ('list', $ lists );
$ This-> list = $ lists;
$ This-> display ('message/lyb_leftmenu ');
}

The result of dump ($ lists) is as follows:
Array (5 ){
[1] => array (7 ){
["Id"] => string (1) "1"
["Menuid"] => string (1) "1"
["Menuname"] => string (12) "system settings"
["Parentid"] => string (1) "0"
["URL"] => string (31) "/app/index. php/System/setSystem"
["Op"] => array (2 ){
["Delete"] => string (43) "/app/index. php/Message/delete/menuid/1.html"
["Deletechildmenu"] => string (53) "/app/index. php/Message/deletechildmenu1/menuid/1.html"
}
["Newchildmenu"] => array (1 ){
[8] => array (5 ){
["Childmenuid"] => string (1) "8"
["Menuid"] => string (1) "1"
["Childmenuname"] => string (21) "administrator password settings"
["ChildmenuURL"] => string (48) "/app/index. php/Message/update_adminpassword.html"
["Op"] => array (1 ){
["Delete"] => string (57) "/app/index. php/Message/deletechildmenu/childmenuid/8.html"
}
}
}
}
[3] => array (6 ){
["Id"] => string (1) "3"
["Menuid"] => string (2) "10"
["Menuname"] => string (12) "View message"
["Parentid"] => string (1) "0"
["URL"] => NULL
["Op"] => array (2 ){
["Delete"] => string (44) "/app/index. php/Message/delete/menuid/10.html"
["Deletechildmenu"] => string (54) "/app/index. php/Message/deletechildmenu1/menuid/10.html"
}
}
[6] => array (6 ){
["Id"] => string (1) "6"
["Menuid"] => string (2) "15"
["Menuname"] => string (12) "menu settings"
["Parentid"] => string (1) "0"
["URL"] => NULL
["Op"] => array (2 ){
["Delete"] => string (44) "/app/index. php/Message/delete/menuid/15.html"
["Deletechildmenu"] => string (54) "/app/index. php/Message/deletechildmenu1/menuid/15.html"
}
}
[10] => array (1 ){
["Newchildmenu"] => array (2 ){
[9] => array (5 ){
["Childmenuid"] => string (1) "9"
["Menuid"] => string (2) "10"
["Childmenuname"] => string (18) "View all messages"
["ChildmenuURL"] => string (32) "/app/index. php/Message/leaveWord"
["Op"] => array (1 ){
["Delete"] => string (57) "/app/index. php/Message/deletechildmenu/childmenuid/9.html"
}
}
[10] => array (5 ){
["Childmenuid"] => string (2) "10"
["Menuid"] => string (2) "10"
["Childmenuname"] => string (12) "Review message"
["ChildmenuURL"] => string (37) "/app/index. php/Message/checkLeaveWord"
["Op"] => array (1 ){
["Delete"] => string (58) "/app/index. php/Message/deletechildmenu/childmenuid/10.html"
}
}
}
}
[15] => array (1 ){
["Newchildmenu"] => array (1 ){
[11] => array (5 ){
["Childmenuid"] => string (2) "11"
["Menuid"] => string (2) "15"
["Childmenuname"] => string (18) "View all Menus"
["ChildmenuURL"] => string (4) "NULL"
["Op"] => array (1 ){
["Delete"] => string (58) "/app/index. php/Message/deletechildmenu/childmenuid/11.html"
}
}
}
}
}

I want to view all the messages. Why can't I check the comments?


Reply to discussion (solution)

Need to sort sub-menus?

This is not necessarily because the constructed menu structure must comply with the requirements, that is, the requirement for $ lists

For more information, see

$ List = array ('id' => '1', 'menuname' => 'system settings', 'parentid' => '0 ', 'sort '=> '0',), array ('id' => '2', 'menuname' => 'User management ', 'parentid' => '0', 'sort '=> '1',), array ('id' => '3 ', 'menuname' => 'Article management', 'parentid' => '0', 'sort '=> '2 ',), array ('id' => '4', 'menuname' => 'Comment management', 'parentid' => '0 ', 'sort '=> '3',), array ('id' => '5', 'menuname' => 'Category management ', 'parentid' => '0', 'sort '=> '4',), array ('id' => '6 ', 'menuname' => 'User list', 'parentid' => '1', 'sort '=> '0 ',), array ('id' => '7', 'menuname' => 'User review', 'parentid' => '2 ', 'sort '=> '2',), array ('id' => '8', 'menuname' => 'Article add ', 'parentid' => '3', 'sort '=> '3',), array ('id' => '9 ', 'menuname' => 'Article review', 'parentid' => '3', 'sort '=> '4',),); function tree (& $ list, $ parent_id, $ deep) {static $ tree = array (); foreach ($ list as $ v) {if ($ v ['parentid'] = $ parent_id) {$ v ['sort '] = $ deep; $ tree [] = $ v; tree ($ list, $ v ['id'], $ deep + 1) ;}}return $ tree ;}$ result = tree ($ list,); // Infinitus class echo'
'; Print_r ($ result); echo'
';foreach($result as $v){echo '|' . str_repeat('----',$v['sort']);echo $v['menuname'];echo '
';}

I designed the background menu for reference only.

$m = M('menu');$pagedata = $m->join('left join pagemenu on pagemenu.type = menu.id')->field('pagemenu.id,menu.menuname,pagemenu.pagename,pagemenu.type,pagemenu.pageurl')->select();$res=array();foreach($pagedata as $val){$res[$val['menuname']][]=$val;}$this->assign('res', $res);$this->display('left');



Tpl

           
    
   
{$key}
  • {$v.pagename}

Html





  • {$ Vo. menuname}




    • {$ Rvo. childmenuname}







For more information about your code, I 'd like to use my php code, data table structure, and html. can you change it to my menu structure:
System Menu
| -- Administrator password modification
View messages
|-View all messages
|-Review message
Menu settings
|-View all menus
|-Add menu
|-Delete menu
|-Edit menu

The problem is that the administrator password modification menu can be placed in the $ lists array, and the location is correct. check all messages and review comments and put the wrong location in $ lists.

Can you check whether it can be changed?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.