一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

解析thinkphp的左右值無限分類

以前一直使用父子無限分類,這種分類結構清晰,使用也簡單。但若分類數(shù)量很大的話,在查詢上性能不佳。比如在做導航菜單中,我要根據(jù)某一分類查詢出整個分類樹的話(祖輩)。
性能消耗是非常大的,要么做遞歸,要么做多次查詢。故,對于分類的數(shù)據(jù)量很大的情況,我推薦使用左右值,以減少查詢上的麻煩。
復制代碼 代碼如下:
_id
    /**
         +----------------------------------------------------------
         * 構造函數(shù)
         * @access public
         * @return void
         +----------------------------------------------------------
         */
    public  function __construct($left,$right,$id){
        parent::__construct();
       $this->_left = $left;
       $this->_right = $right;
       $this->_id = $id;
    }
    /**
      +----------------------------------------------------------
      * 根據(jù)node$this->_id得到該node的所有值
      * @access public
      * @param $nodeId
      * @return array
      +----------------------------------------------------------
     */    
    public  function getNodeById($nodeId)
    {
        if($nodeId>0)
        {
            return $this->getById($nodeId);
        }
        else
        {
            throw_exception('未知$this->_id');
            return false;
        }
    }
    /**
           +----------------------------------------------------------
           * 獲取父節(jié)點,含直屬父類(type=1),所有父類:type=0
           * @access public
           * @param $nodeId int 節(jié)點$this->_id
           * @return $parentNode array()
           +----------------------------------------------------------
          */    
    public  function getParentNode($nodeId,$type = 0)
    {
        if($nodeId == 0) throw_exception('未知$this->_id');;
        $currentNode = $this->getNodeById($nodeId);
        if($currentNode)
        {
            $condition = " ".$this->_left.'<'.$currentNode[$this->_left].' and '.$this->_right.' >'.$currentNode[$this->_right]." ";
            if($type ==1) //直屬父類
            {
                return $this->where($condition)->order($this->_left." DESC")->limit(1)->find();
                //                $sql = "SELECT * FROM ".TABLE_NAME." WHERE {$condition} ORDER BY ".$this->_left." DESC LIMIT 1";
                //                return mysql_query($sql) or die(mysql_error());
            }
            else if($type ==0)
            {
                return $this->where($condition)->findAll();
                //                $sql = "SELECT * FROM ".TABLE_NAME." WHERE {$condition} ";
                //                return mysql_query($sql) or die(mysql_error());
            }
        }
        else
        {
            return false;
        }
    }
    /**
         +----------------------------------------------------------
         * 當前節(jié)點下子孫節(jié)點總數(shù).子孫總數(shù)=(當前節(jié)點的右值 - 當前節(jié)點的左值-1)/2
         * @access public
         * @param $node_id int 節(jié)點$this->_id
         * @return $amount int 該節(jié)點下的子孫總數(shù)         *
         +----------------------------------------------------------
         */
    public  function getChildCount($nodeId)
    {
        $currentNode = $this->getNodeById($nodeId);
        if(!empty($currentNode))
        {
            return (int)($currentNode[$this->_right]-$currentNode[$this->_left] -1)/2;
        }
    }
    /**
      +----------------------------------------------------------
      * 獲取當前節(jié)點下所有子節(jié)點。 當 A子類的右節(jié)點=B子類左節(jié)點-1 則 A、B屬于同一級別
      * @access public
      * @param $curentId
      * @param  $type int 0:當前節(jié)點下所有子類,1為當前節(jié)點下一級子類
      * @return bool
      +----------------------------------------------------------
     */    
    public  function getChild($nodeId,$type=0)
    {
        $currentNode = $this->getNodeById($nodeId);
        if($currentNode[$this->_left]-$currentNode[$this->_right] ==1)
        {
            return false; //當 該節(jié)點左值 - 右值=1  時,其下沒有子節(jié)點。
        }
        else
        {
            $condition = $this->_left.'>'.$currentNode[$this->_left].' and '.$this->_right .'<'.$currentNode[$this->_right];
            $child = $this->where($condition)->findAll();
            if($type == 0)//所有子類
            {
                return $child;
            }
            else if($type ==1) //獲取當前節(jié)點下一級分類
            {                       
                $subArr = array(); //一級子類
                foreach ($child as $k=>$sub) {
                    //子類的左節(jié)點=父類左節(jié)點+1,則子類為第一個子類
                    if($sub[$this->_left]==$currentNode[$this->_left]+1)
                    {
                        //$right = $sub[$k][$this->_right]; //當前節(jié)點的右節(jié)點
                        $firstSub = $sub; //當前節(jié)點下第一個子類
                        array_push($subArr,$firstSub); //子類入棧
                        unset($child[$k]);
                    }
                }
                $rightVal =  $firstSub[$this->_right]; //第一個子節(jié)點為比較標志
                $childCount = count($child);//剩余子節(jié)點數(shù)
                for($i=0;$i<$childCount;$i++) //循環(huán)檢索出 同級子節(jié)點
                {
                    foreach ($child as $key => $sub2) {
                        if($rightVal == $sub2[$this->_left]-1)
                        {
                            $rightVal = $sub2[$this->_right]; //把循環(huán)當前的node的右節(jié)點當做比較值
                            array_push($subArr,$sub2);
                            unset($child[$key]);
                        }
                    }
                }
                return $subArr;
            }
        }
    }
    /**
         +----------------------------------------------------------
         * 返回當前節(jié)點的完整路徑
         * @access public
         * @param $nodeId
         * @return array
         +----------------------------------------------------------
        */    
    public  function getSinglePath($nodeId)
    {
        $sql = "select parent.* from __TABLE__ as node,__TABLE__ as parent where node.{$this->_left} between parent.{$this->_left}
            AND parent.{$this->_right} AND node.{$this->_id} = {$nodeId} order by parent.{$this->_left}";
//        echo $sql;
        return $this->query($sql);
    }
    /**
      +----------------------------------------------------------
      * 添加子節(jié)點,分3種:0:在當前節(jié)點下最后追加一個子節(jié)點;1:在當前節(jié)點下追加第一個子節(jié)點;

2:在當前節(jié)點下的某個子節(jié)點后追加
復制代碼 代碼如下:
      * @access public
      * @param $currentId int
      * @param $nodeName string 新節(jié)點名稱     
      * @param $targetId int 追加到當前節(jié)點下子節(jié)點的指定節(jié)點后
      * @return bool
      +----------------------------------------------------------
     */   
    public  function addNode($nodeId,$newData,$type=0,$targetId=0)
    {
        if(empty($newData))
        {
            throw_exception('新分類不能為空');
        }
        $currentNode = $this->getNodeById($nodeId);
        switch ($type) {
            case 0:
                $leftNode  = $currentNode[$this->_right]; //新節(jié)點的左值為父節(jié)點的右值
                $rightNode = $leftNode+1;
                break;
            case 1:
                $leftNode = $currentNode[$this->_left]+1; //新節(jié)點的左值為父節(jié)點的左值+1
                $rightNode = $leftNode+1;
                break;
            case 2:
                $otherNode = $this->getNodeById($targetId);
                $leftNode = $otherNode[$this->_right]+1;
                $rightNode = $leftNode+1;
            default:
                break;
        }
//         $sql = "UPDATE ".TABLE_NAME." SET ".$this->_right."=".$this->_right."+2 WHERE ".$this->_right." >= ".$leftNode;
//        $sql2 = "UPDATE ".TABLE_NAME." SET ".$this->_left."=".$this->_left."+2 WHERE ".$this->_left.">".$leftNode;
        $this->setInc($this->_right,$this->_right.">=".$leftNode,2); //把所有右值大于新節(jié)點左值的節(jié)點的右值+2,注意效率
        $this->setInc($this->_left,$this->_left.">".$leftNode,2);   //把所有大于新節(jié)點的左值+2
        $newData[$this->_left] = (int)$leftNode;
        $newData[$this->_right] =(int) $rightNode;
        return $this->add($newData);
    }
    /**
         +----------------------------------------------------------
         * 刪除節(jié)點
         * @access public
         * @param type 操作類型,默認為0刪除當前節(jié)點下的所有子節(jié)點,1為刪除包括自身的節(jié)點
         * @param $nodeId int 要刪除的$this->_id
         * @return bool
         +----------------------------------------------------------
        */    
    public  function rmNode($nodeId,$type =1)
    {
        $currentNode = $this->getNodeById($nodeId);
        if($type == 1) //刪除包含自身的節(jié)點
        {
            $sql = "DELETE FROM __TABLE__ WHERE ".$this->_left.">= {$currentNode[$this->_left]} AND ".$this->_right."<= {$currentNode[$this->_right]}";
            $childCount = ($this->getChildCount($nodeId)+1)*2; //要更新的值
            $sql2 = "UPDATE  __TABLE__  SET ".$this->_right."=".$this->_right."-".$childCount." WHERE ".$this->_right.">".$currentNode[$this->_right];
            $sql3 = "UPDATE  __TABLE__  SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
        }
        else //刪除當前節(jié)點下的所有節(jié)點
        {
            $sql ="DELETE FROM __TABLE__ WHERE ".$this->_left."> {$currentNode[$this->_left]} AND ".$this->_right."< {$currentNode[$this->_right]}";
            $childCount = $this->getChildCount($nodeId)*2; //要更新的值
            $sql2 = "UPDATE __TABLE__ SET ".$this->_right."=".$this->_right ."-".$childCount." WHERE ".$this->_right.">=".$currentNode[$this->_right];
            $sql3 = "UPDATE __TABLE__ SET ".$this->_left."=".$this->_left."-".$childCount." WHERE ".$this->_left.">".$currentNode[$this->_left];
        }
         $this->execute($sql); 
         $this->execute($sql2); 
         $this->execute($sql3); 
        return true;
    }
     /**
      +----------------------------------------------------------
      * 修改節(jié)點,名稱等
      * @access public
      * @param $newData array()必須含有 要修改的$this->_id,k-v必須對齊,如arr['node_name'] = '商品'
      * @return bool
      +----------------------------------------------------------
     */    
    public  function modiNode($newData)
       {
            if(!empty($newData))
            {
                $id = $newData[$this->_id];               
                unset($newData[$this->_id]);
                return $this->save($newData,$this->_id.'='.$id);                              
          }
       }
}
?>

php技術解析thinkphp的左右值無限分類,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本加勒比网站 | 女人张腿给男人桶视频免费版 | 日本高清色视频www 日本高清色视频在线观看免费 | 精品成人 | 六月激情 | 国产第一页在线播放 | 中文字幕一区2区3区 | 国产99精品视频 | 缴情啪啪三级小说网 | 777福利| 青草社区在线 | 男人私gay视频网站的 | 午夜hhh视频在线观看hhhh | 91日韩视频 | 天天干天天射天天爽 | 91欧美激情一区二区三区成人 | 色在线免费视频 | 国产精选视频在线观看 | avwww在线| 五月月色开心婷婷久久合 | 久久中文字幕久久久久 | 成人精品视频在线 | 亚洲第一夜| 九九99九九精彩网站 | 99在线视频观看 | 51国产偷自视频区视频手机播器 | 成年女人男人免费视频播放 | 国产成人精品永久免费视频 | 91久久国产视频 | 久久亚洲精品无码观看不卡 | 成人亚洲视频 | 日本xxxⅹ色视频在线观看网站 | 亚洲图片另类图片 | 国产精品极品美女自在线观看免费 | 欧美日韩国产亚洲一区二区三区 | 欧美色88| 国产福利合集 | 精品精品精品 | 亚洲免费视频网站 | 婷婷丁香四月 | 色婷婷精品视频 |