thinkPHP implements the function of linkage menu

不言
Release: 2023-03-30 13:18:01
Original
2315 people have browsed it

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:

所有商品分类

Copy after login

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
            (
            )
          )
Copy after login

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
    )
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!