根据当前分类ID获取所有父类或所有子分类

获取指定分类所有父ID号

$categoryList = Db::name('goods_category')
    ->where('status',1)
    ->field('gc_id,gc_name,parent_id')
    ->select();
if(!empty($categoryList))
{
    while($catId != 0){
        foreach($categoryList as $item){
            if($item['gc_id']==$catId){
                $tree[]=$item;
                $catId=$item['parent_id'];
                break;
            }
        }
    }
}
return array_reverse($tree);    //数组倒序排列

//获取指定分类的所有子分类ID

function getAllChildCateIds($categoryID)
{
   //初始化ID数组
   $array[] = $categoryID;
   do
   {
      $ids = '';
      $catWhere['parent_id'] = array('IN',$categoryID);
      $cate = Db::name('goods_category')->where($catWhere)->field('gc_id')->select();
      foreach ($cate as $k=>$v)
      {
         $array[] = $v['gc_id'];
         $ids .= ',' . $v['gc_id'];
       }
       $ids = substr($ids, 1, strlen($ids));
       $categoryID = $ids;
    }
    while (!empty($cate));
    $ids = implode(',', $array);
    return $ids;
}

//获取指定分类的所有子分类 键为ID,值为分类名


   function getCateKv($categoryID)
    {
        //初始化ID数组,赋值当前分类
        $array[] = M('cate')->where("id={$categoryID}")->getField("cateName");
        do
        {
            $ids = '';
            $where['pid'] = array('in',$categoryID);
            $cate = M('cate')->where($where)->select();
            echo M('cate')->_sql();
            foreach ($cate as $k=>$v)
            {
                $array[$v['id']] = $v['cateName'];
                $ids .= ',' . $v['id'];
            }
            $ids = substr($ids, 1, strlen($ids));
            $categoryID = $ids;
        }
        while (!empty($cate));
        $ids = implode(',', $array);
 
         //return $ids; //  返回字符串
        return $array //返回数组
    }

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值