This article mainly introduces the linkage menu function implemented by thinkPHP, and analyzes the implementation steps and specific operation techniques of thinkPHP linkage menu in the form of examples. Friends in need can refer to the following
The examples in this article describe the implementation of thinkPHP linkage menu function. Share it with everyone for your reference, the details are as follows:
Linked menu, first let me show you how the front end is written:
<p id="newCat"> <p class="all_type" id="allGoogsCat">所有商品分类</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="external nofollow" >{$v.category_name}</a> </span> <p class="p none"> <foreach name="v['childs']" key="two" item="v2"> <dl> <dt> <a href="{:U('Category/index',array('id'=>$v2['category_id'],'level'=>2))}" rel="external nofollow" >{$v2.category_name}</a> </dt> <dd class="fl"> <foreach name="v2['childs']" item="v3"> <a href="{:U('Category/index',array('id'=>$v3['category_id'],'level'=>3))}" rel="external nofollow" > {$v3.category_name} </a> </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'){ ?> //商品分类鼠标滑过 function spflHover() { $(".index-spfl-left ul li").live("mouseover", function () { $(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(); }); //鼠标滑过分类显示 $("#newCat").mouseover(function () { $(".spfl-warp").show(); }).mouseout(function () { if ($("#ismain").val() == "1") { $(".spfl-warp").show(); } else { $(".spfl-warp").hide(); } }) } spflHover(); <?php } ?> </script>
Did you see that, in fact, the first-level menu corresponds to the second-level menu in the same li. The second-level and third-level menus in li are all placed in the dt and dd tags of dl. Inside;
Then now let’s take a look at the category_menu taken out, what kind of data it is:
Array ( [0] => Array ( [category_id] => 84 [category_name] => 家装主材 [parent_id] => 0 [listorder] => 200 [display] => 1 [childs] => Array ( [0] => Array ( [category_id] => 85 [category_name] => 厨卫 [parent_id] => 84 [listorder] => 200 [display] => 1 [childs] => Array ( [0] => Array ( [category_id] => 99 [category_name] => 厨盆/水槽 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [1] => Array ( [category_id] => 98 [category_name] => 卫浴配件 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [2] => Array ( [category_id] => 97 [category_name] => 卫浴龙头 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [3] => Array ( [category_id] => 96 [category_name] => 龙头 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [4] => Array ( [category_id] => 95 [category_name] => 淋浴房 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [5] => Array ( [category_id] => 94 [category_name] => 智能坐便器 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [6] => Array ( [category_id] => 93 [category_name] => 浴室柜 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [7] => Array ( [category_id] => 92 [category_name] => 坐便器 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [8] => Array ( [category_id] => 91 [category_name] => 浴霸 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [9] => Array ( [category_id] => 90 [category_name] => 地漏 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [10] => Array ( [category_id] => 89 [category_name] => 坐便器盖板 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [11] => Array ( [category_id] => 88 [category_name] => 洗面盆 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [12] => Array ( [category_id] => 87 [category_name] => 角阀 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [13] => Array ( [category_id] => 100 [category_name] => 卫浴五金 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [14] => Array ( [category_id] => 101 [category_name] => 厨房挂件/配件 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [15] => Array ( [category_id] => 86 [category_name] => 淋浴花洒 [parent_id] => 85 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) ) ) [1] => Array ( [category_id] => 126 [category_name] => 墙纸 [parent_id] => 84 [listorder] => 200 [display] => 1 [childs] => Array ( [0] => Array ( [category_id] => 130 [category_name] => 3D墙纸 [parent_id] => 126 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [1] => Array ( [category_id] => 129 [category_name] => 纯纸墙纸 [parent_id] => 126 [listorder] => 200 [display] => 1 [childs] => Array ( ) ) [2] => Array ( [category_id] => 128 [category_name] => PVC墙纸 [parent_id] => 126 [listorder] => 200 [display] => 1 [childs] => Array ( ) )
Then, what does the data table look like? Where is the data:
Array ( [0] => Array ( [category_id] => 211 [category_name] => 鞋柜 [parent_id] => 31 [listorder] => 200 [display] => 1 ) [1] => Array ( [category_id] => 194 [category_name] => 相框/照片墙 [parent_id] => 189 [listorder] => 200 [display] => 1 ) [2] => Array ( [category_id] => 193 [category_name] => 帘艺隔断 [parent_id] => 189 [listorder] => 200 [display] => 1 ) [3] => Array ( [category_id] => 192 [category_name] => 沙发垫套/椅垫 [parent_id] => 189 [listorder] => 200 [display] => 1 ) [4] => Array ( [category_id] => 191 [category_name] => 地毯地垫 [parent_id] => 189 [listorder] => 200 [display] => 1 ) [5] => Array ( [category_id] => 190 [category_name] => 桌布/罩件 [parent_id] => 189 [listorder] => 200 [display] => 1 )
The data in the data table is purple;
Then, can you write the method for processing the array? Here you go The original data, and then you use iteration to process it into the data you want. It doesn’t need to be too much. It’s about 6 lines of code. Are you OK?
If you copy too much code, people will become stupid, you know? ? ?
//把栏目分组,以多维数组形式 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 and pass it as a parameter to the function groud_category function? This way you don't have to check the database all the time.
It is nothing more than starting the query with the parent ID as 0, and then querying the id of the sub-column as the parent ID, and querying the sub-column belonging to its id
That’s it. The entire content of the article is here. Thank you everyone for reading. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
ThinkPHP and Ajax implement secondary linkage drop-down menu
The above is the detailed content of thinkPHP implements the function of linkage menu. For more information, please follow other related articles on the PHP Chinese website!