首頁 >後端開發 >php教程 >thinkPHP實現連動選單的功能

thinkPHP實現連動選單的功能

不言
不言原創
2018-06-07 14:35:472471瀏覽

這篇文章主要介紹了thinkPHP實現的聯動選單功能,結合實例形式分析了thinkPHP連動選單的實現步驟與具體操作技巧,需要的朋友可以參考下

本文實例講述了thinkPHP實現的聯動選單功能。分享給大家供大家參考,具體如下:

聯動選單,首先給你看看前端是怎麼寫的:

##

<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>

看到沒有,其實裡面的一級選單對應二級選單都是在同一個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(&#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;
}

為什麼不把SQL語句放在外面當作參數傳遞進去函數groud_category函數呢?這樣就不用老是查資料庫啊。

無非也就是將父ID為0開始查詢,然後對其子欄目的id在做為父ID進行查詢,查詢出屬於其id的子欄目

以上就是本篇文章的全部內容了,謝謝大家的閱讀,更多相關內容請關注PHP中文網!


相關建議:

ThinkPHP和Ajax 實作二級連動的下拉式選單

以上是thinkPHP實現連動選單的功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn