1. 二宝博客首页
  2. php
  3. Thinkphp

ThinkPHP分类相关查询(获取所有子分类,获取父分类,下一级分类等)

//获取指定分类的所有子分类ID号    function getAllChildcateIds($categoryID)    {        //初始化ID数组        $array[] = $categoryID;        do        {            $ids = '';            $where['pid'] = array('in',$categoryID);            $cate = M('cate')->where($where)->select();            foreach ($cate as $k=>$v)            {                $array[] = $v['id'];                $ids .= ',' . $v['id'];            }            $ids = substr($ids, 1, strlen($ids));            $categoryID = $ids;        }        while (!empty($cate));        $ids = implode(',', $array);        return $ids;    //  返回字符串        //return $array //返回数组    }      //获取指定分类所有父ID号    function getAllFcateIds($categoryID)    {        //初始化ID数组        $array[] = $categoryID;                 do        {            $ids = '';            $where['id'] = array('in',$categoryID);            $cate = M('cate')->where($where)->select();            echo M('cate')->_sql();            foreach ($cate as $v)            {                $array[] = $v['pid'];                $ids .= ',' . $v['pid'];            }            $ids = substr($ids, 1, strlen($ids));            $categoryID = $ids;        }        while (!empty($cate));         $ids = implode(',', $array);         return $ids;   //  返回字符串        //return $array //返回数组    }        //获取指定分类的所有子分类 键为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 //返回数组    }]]>

原创文章,作者:键盘游走者,如若转载,请注明出处:http://www.708034.com/2017/04/thinkphp%e5%88%86%e7%b1%bb%e7%9b%b8%e5%85%b3%e6%9f%a5%e8%af%a2%e8%8e%b7%e5%8f%96%e6%89%80%e6%9c%89%e5%ad%90%e5%88%86%e7%b1%bb%ef%bc%8c%e8%8e%b7%e5%8f%96%e7%88%b6%e5%88%86%e7%b1%bb%ef%bc%8c%e4%b8%8b/

发表评论

电子邮件地址不会被公开。 必填项已用*标注