Thinkphp implements the function of the Linkage menu

Source: Internet
Author: User
This article mainly introduced the thinkphp realization of the Linkage menu function, combined with the example form analysis of the thinkphp Linkage menu implementation steps and specific operating skills, the need for friends can refer to the next

This paper describes the function of linkage menu implemented by thinkphp. Share to everyone for your reference, as follows:

Linkage menu, first show you how the front-end is written:

<p id= "Newcat" > <p class= "all_type" id= "Allgoogscat" > All product Categories </p> <p class= "Spfl-warp <?php if ( Controller_name! = ' Index ' | |        Action_name! = ' index ') {echo ' Hide ';}?> "> <p class=" index-spfl-left "id=" pcatlist "> <ul> <foreach name= "Category_menu" key= "one" item= "V" > <li class= "li{$one +1}" > <span><em ></em> <a href= "{: U (' Category/index ', array (' id ' = = $v [' category_id '], ' Level ' =>1)}" rel= "extern Al nofollow ">{$v .category_name}</a> </span> <p class=" p None "> <fore Ach name= "v[' Childs '" key= "" item= "V2" > <dl> <dt> <a HR Ef= "{: U (' Category/index ', array (' id ' = = $v 2[' category_id '], ' Level ' =>2)}" rel= "External nofollow" >{$ v2.category_name}</a> </dt> <dd class= "FL" > <foreach n Ame= "v2[" Childs '] "item=" v3 "> <a href=" {: U (' Category/index ', array (' id ' = = $v 3[' category_id '], ' Level ' =>3)) } "rel=" external nofollow "> {$v 3.category_name} </a> &L            t;/foreach> </dd> <p class= "cl" ></p> </dl> </foreach> </p> </li> </foreach> </ul> </p> </p>< /p><script type= "Text/javascript" ><?php if (controller_name! = ' Index ' | | Action_name = ' index ') {?>//product category mouse over function Spflhover () {$ (". Index-spfl-left ul Li"). Live ("MouseOver", Functi      On () {$ (this). AddClass ("active"). Siblings (). Removeclass ("active");      $ (". Index-spfl-left"). Find (". P"). Hide ();    $ (this). Find (". P"). Show ();      }). Live ("Mouseout", function () {$ (this). Removeclass ("active");      $ (". Index-spfl-left"). Find (". P"). Hide ();   $ (this). Find (". P"). Hide (); });    The mouse-over category displays $ ("#newCat"). MouseOver (function () {$ (". Spfl-warp"). Show ();      }). mouseout (function () {if ($ ("#ismain"). val () = = "1") {$ (". Spfl-warp"). Show ();      } else {$ (". Spfl-warp"). Hide (); }})} spflhover (); <?php}?></script>

See no, in fact, the first level menu in the corresponding two-level menu is in the same Li, Li inside the level two three, are placed in the DL DT and DD label inside;

And now we look at the category_menu, what kind of data:

Array ([0] = = Array ([category_id] = [category_name] = home Improvement main material [parent_id] = 0 [Listorder] = = [Display] = 1 [Childs] = = Array ([0] = = Array ([category_id] = [Categor         Y_name] [parent_id] = [Listorder] = [Display] + 1 [childs] = = Array           ([0] = = Array ([category_id] = [category_name] = kitchen sink/sink            [parent_id] = [Listorder] = [Display] = 1 [childs] = = Array ()) [1] = = Array ([category_id] = 98 [Category_nam E] + bathroom accessories [parent_id] = [Listorder] = [Display] = 1 [Childs           ] = = Array ()) [2] = = Array ([category_id] = 97 [Category_name] => Sanitary faucet [parent_id] = [Listorder] = [Display] = 1 [Childs] =& Gt Array ()) [3] = = Array ([category_id] = 96 [           Category_name] = faucet [parent_id] [Listorder] = [Display] = 1 [Childs] = = Array ()) [4] = = Array ([category_id] =& Gt [Category_name] = shower room [parent_id] = [Listorder] = [Display] = 1 [Childs] = = Array ()) [5] = = Array ([            CATEGORY_ID] = 94 [category_name] + smart toilet [parent_id] [listorder] = 200          [Display] = 1 [Childs] = Array ()) [6] = = Array       (    [category_id] = [category_name] = bathroom cabinet [parent_id] = [Listorder] = [Display] = 1 [Childs] = Array ()) [7] = Arra           Y ([category_id] = [Category_name] + toilet [parent_id] = 85         [Listorder] = [Display] = 1 [childs] = = Array ()) [8] = = Array ([category_id] = [Category_name] = [parent_id]            [Listorder] = [Display] = 1 [childs] = = Array (           )) [9] = = Array ([category_id] = [category_name] = floor Drain            [parent_id] = [Listorder] = [Display] = 1 [childs] = = Array   (            )       ) [+] = Array ([category_id] = [Category_name] = toilet cover plate            [parent_id] = [Listorder] = [Display] = 1 [childs] = = Array ()) [One] = Array ([category_id] = [Category_na Me] = wash basin [parent_id] = [Listorder] = [Display] = 1 [Childs           ] = = Array ()) [+] = Array ([category_id] = 87 [Category_name] = angle valve [parent_id] = [Listorder] = [display] = = 1 [Childs] = = Array ()) [+] = Array ([Categ           ORY_ID] [category_name] = bathroom hardware [parent_id] [listorder] = 200 [Display] = 1          [Childs] = = Array ()) [+] = = Array ([Categor           Y_ID] = 101 [category_name] + kitchen pendant/accessory [parent_id] [listorder] = 200          [Display] = 1 [Childs] = = Array ()) [+] = array ([category_id] = [Category_name] + [parent_id] [] = [the] [LIS]       Torder] [[Display] = + 1 [Childs] = = Array ()))        ) [1] = = Array ([category_id] = 126 [category_name] + wallpaper [parent_id] + 84          [Listorder] = [Display] = 1 [Childs] = = Array ([0] = = Array ([category_id] = [Category_name] + 3D wallpaper [parent_id] = 126 [Listor          Der] = 200 [Display] = 1 [Childs] = Array ()) [1] = = Array ( [category_id] = 129 [Category_name] + pure paper wallpaper [parent_id] = 126 [Listorde R] = [Display] + 1 [childs] = = Array ()) [2] = > Array ([category_id] [+] [category_name] = PVC Wallpaper [parent_id] =          126 [Listorder] = [Display] = 1 [childs] = = Array () )

Then, what kind of data is the data table:

Array ([0] = = Array ([category_id] = 211 [Category_name] = shoe cabinet [parent_id] = [Li Storder] = [Display] + 1) [1] = = Array ([category_id] = 194 [Category_name] =&G T Picture frame/Photo Wall [parent_id] = 189 [Listorder] = [Display] + 1) [2] = = Array ([categ ORY_ID] = 193 [category_name] + curtain Art partition [parent_id] = 189 [Listorder] = [Display] =&G T      1) [3] = = Array ([category_id] = 192 [category_name] = sofa Cushion cover/cushion [parent_id] 189 [Listorder] = [Display] = 1) [4] = = Array ([category_id] = 191 [Category_nam      E] = Carpet mat [parent_id] = 189 [Listorder] = [Display] + 1) [5] = = Array ( [category_id] = [Category_name] = tablecloth/cover [parent_id] = 189 [Listorder] = [disp Lay] = 1)

The data table data is Jiangzi;

Then, can you write the method that handles the array, give you the original data, and then you use the iteration to process the data you want, not too much, 6 lines of code around, you OK?

Code copy more, people will become silly, do you know???

The columns are grouped in a multidimensional array form public Function group_category ($id = 0) {    $list = $this->where (' display = 1 ')->order (' Listorder ASC ')->select ();    $tmp = Array ();    foreach ($list as $v) {      if ($v [' parent_id '] = = $id) {        $v [' childs '] = $this->group_category ($v [' category_id ']) ;        $tmp [] = $v;      }    }    return $tmp;}

Why not put the SQL statement outside as a parameter to pass in the function Groud_category function? So you don't always have to look up databases.

That is, the parent ID is 0 to start the query, and then the ID of its sub-column as the parent ID query, query the sub-column belongs to its ID

The above is the whole content of this article, thank you for reading, more relevant content please pay attention to topic.alibabacloud.com!

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.