thinkPHP implements the function of linkage menu

不言
Release: 2023-03-30 13:18:01
Original
2440 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:

<p id="newCat">
  <p class="all_type" id="allGoogsCat">所有商品分类</p>
  <p class="spfl-warp <?php if(CONTROLLER_NAME != &#39;Index&#39; || ACTION_NAME != &#39;index&#39;){echo &#39;hide&#39;;} ?> ">
    <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(&#39;Category/index&#39;,array(&#39;id&#39;=>$v[&#39;category_id&#39;],&#39;level&#39;=>1))}" rel="external nofollow" >{$v.category_name}</a>
          </span>
          <p class="p none">
            <foreach name="v[&#39;childs&#39;]" key="two" item="v2">
              <dl>
                <dt>
                  <a href="{:U(&#39;Category/index&#39;,array(&#39;id&#39;=>$v2[&#39;category_id&#39;],&#39;level&#39;=>2))}" rel="external nofollow" >{$v2.category_name}</a>
                </dt>
                <dd class="fl">
                  <foreach name="v2[&#39;childs&#39;]" item="v3">
                    <a href="{:U(&#39;Category/index&#39;,array(&#39;id&#39;=>$v3[&#39;category_id&#39;],&#39;level&#39;=>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 != &#39;Index&#39; || ACTION_NAME != &#39;index&#39;){ ?>
  //商品分类鼠标滑过
  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>
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(&#39;display = 1&#39;)->order(&#39;listorder asc&#39;)->select();
    $tmp = array();
    foreach($list as $v){
      if($v[&#39;parent_id&#39;] == $id){
        $v[&#39;childs&#39;] = $this->group_category($v[&#39;category_id&#39;]);
        $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