本篇文章给大家分享的是有关如何在thinkPHP项目中实现一个联动菜单功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的磴口网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!看到没有,其实里面的一级菜单对应二级菜单都是在同一个li里面的,li里面的二级三级呢,都是放在dl的dt和dd标签里面;
然后现在我们看看取出来的category_menu,什么样的数据:
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 ( ) )
然后,数据表是怎么样的数据呢:
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 )
数据表数据是酱紫的;
然后,你能写出那个处理数组的方法吗,给你原数据,然后你用迭代处理成你想要的数据,不用太多,6行代码左右,你OK?
代码拷贝多了,人也就变傻了,知不知道???
//把栏目分组,以多维数组形式 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; }
以上就是如何在thinkPHP项目中实现一个联动菜单功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。