相思资源网 Design By www.200059.com

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

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

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

<div id="newCat">
  <div class="all_type" id="allGoogsCat">所有商品分类</div>
  <div class="spfl-warp <">
    <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">
<".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();
<"htmlcode">
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;
}

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

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

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。

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

标签:
thinkPHP,联动菜单

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“thinkPHP实现的联动菜单功能详解”

暂无thinkPHP实现的联动菜单功能详解的评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?