服务器之家

服务器之家 > 正文

thinkPHP实现的联动菜单功能详解

时间:2021-05-17 16:56     来源/作者:许伟强

本文实例讲述了thinkphp实现的联动菜单功能。分享给大家供大家参考,具体如下:

联动菜单,首先给你看看前端是怎么写的:

thinkPHP实现的联动菜单功能详解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<div id="newcat">
  <div class="all_type" id="allgoogscat">所有商品分类</div>
  <div class="spfl-warp <?php if(controller_name != 'index' || action_name != 'index'){echo 'hide';} ?> ">
    <div class="index-spfl-left" id="divcatlist">
      <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>
          <div class="div 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>
                <div class="cl"></div>
              </dl>
            </foreach>
          </div>
        </li>
      </foreach>
    </ul>
  </div>
  </div>
</div>
<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(".div").hide();
      $(this).find(".div").show();
    }).live("mouseout", function () {
      $(this).removeclass("active");
      $(".index-spfl-left").find(".div").hide();
      $(this).find(".div").hide();
    });
    //鼠标滑过分类显示
    $("#newcat").mouseover(function () {
      $(".spfl-warp").show();
    }).mouseout(function () {
      if ($("#ismain").val() == "1") {
        $(".spfl-warp").show();
      } else {
        $(".spfl-warp").hide();
      }
    })
  }
  spflhover();
<?php } ?>
</script>

看到没有,其实里面的一级菜单对应二级菜单都是在同一个li里面的,li里面的二级三级呢,都是放在dl的dt和dd标签里面;

然后现在我们看看取出来的category_menu,什么样的数据:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
            (
            )
          )

然后,数据表是怎么样的数据呢:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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?

代码拷贝多了,人也就变傻了,知不知道???

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//把栏目分组,以多维数组形式
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;
}

为什么不把sql语句放在外面作为参数传递进去函数groud_category函数呢?这样就不用老是查数据库啊。

无非也就是将父id为0开始查询,然后对其子栏目的id在做为父id进行查询,查询出属于其id的子栏目

希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部