主要思想來自:http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]

這里就不多解釋原理了,直接發代碼。

PS:這里代碼是不能直接使用的,必須結 " /> 日本精品久久电影,国产精品白丝jk白祙,亚洲精品欧美

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

自己前幾天寫的無限分類類

前一周寫的吧,使用中效果還不錯。

 主要思想來自:http://www.phpobject.NET/b...[url=http://www.phpobject.NET/blog/read.php?49][/url]

  這里就不多解釋原理了,直接發代碼。

  PS:這里代碼是不能直接使用的,必須結合我的一些其他庫類。應該說思想才是最重要的,這里主要提供一種分類的思路。
復制代碼 代碼如下:
<? 
/** 
--  
-- 表的結構 `daxue8_category` 
--  

CREATE TABLE `daxue8_category` ( 
  `cid` smallint(6) NOT NULL auto_increment, 
  `pid` smallint(6) NOT NULL default '0', 
  `level` smallint(6) NOT NULL default '0', 
  `cname` char(64) NOT NULL default '', 
  `lft` smallint(6) NOT NULL default '0', 
  `rgt` smallint(6) NOT NULL default '0', 
  `uid` mediumint(8) NOT NULL default '0', 
  `username` char(32) NOT NULL default '', 
  `ctime` int(10) NOT NULL default '0', 
  `cstate` tinyint(1) NOT NULL default '0', 
  `gnum` mediumint(8) NOT NULL default '0', 
  `orderstyle` smallint(3) NOT NULL default '0', 
  PRIMARY KEY  (`cid`) 
) TYPE=MyISAM AUTO_INCREMENT=2 ; 

--  
-- 導出表中的數據 `daxue8_category` 
--  

INSERT INTO `daxue8_category` VALUES (1, 0, 1, 'root', 1, 2, 0, '管理員', 1163608814, 1, 0, 0); 
*/ 
class category 

    var $module; 

    var $tbname; 

    function category() 
    { 
        $this->tbname=TB_PREX.'_category'; 
        $this->module=new module($this->tbname); 
    } 

    /** 
      * 增加子節點 
      * @param array $node 待增加子節點的屬性 
      * @param int $pid 父節點的ID 
    */ 
    function add($node,$pid){ 
        //檢查是否已經存在該節點 
        if($node_exist=$this->module->detail('where pid='.$pid.' and cname=/''.$node['cname'].'/'')){ 
            //$this->error(__FUNCTION__.'():該節點'.$node['cname'].'已經存在!'); 
            //print_r($node_exist); 
            return $node_exist['cid']; 
        } 
        //獲取父節點信息 
        $pnode=$this->get_by_cid($pid); 
        //更新其他節點 
        $this->module->query('update `'.$this->tbname.'` set lft=lft+2 where lft>'.$pnode['rgt']); 
        $this->module->query('update `'.$this->tbname.'` set rgt=rgt+2 where rgt>='.$pnode['rgt']); 
        //插入新節點 
        $node['pid']=$pid; 
        $node['lft']=$pnode['rgt']; 
        $node['rgt']=$pnode['rgt']+1; 
        $node['level']=$pnode['level']+1;//層次加一 
        return $this->module->add($node); 
    } 

    /** 
      * 刪除節點 
      * @param $cid 待刪除的節點的ID 
      * @param $delete_childern 如果該節點存在子節點,是否強制刪除。設置未true,則當存在子節點的時候,刪除失敗,返回false 
      * 
    */ 
    function delete($cid,$delete_childern=false) 
    { 
        //獲取節點信息 
        $node=$this->get_by_cid($cid); 
        if(($this->child_num($node)>0)&&(!$delete_childern))$this->error(__FUNCTION__.'():該節點存在子節點!'); 
        //刪除該節點及其所有子節點 
        $this->module->delete('where lft between '.$node['lft'].' and '.$node['rgt']); 
        //修改相應的左右鍵值 
        $plus=$node['rgt']-$node['lft']+1; 
        $this->module->query('update `'.$this->tbname.'` set lft=lft-'.$plus.' where lft>'.$node['rgt']); 
        $this->module->query('update `'.$this->tbname.'` set rgt=rgt-'.$plus.' where rgt>'.$node['rgt']); 
        return true; 
    } 

    /** 
      * 更新一個節點 
      * @param array $set更新集 
      * @param int $cid 更新的節點的主鍵ID 
    */ 
    function update($set,$cid){ 
        return $this->module->update($set,'where cid='.$cid); 
    } 

    /** 
      * 選取節點及其子節點 
      * @param int $cid節點的主鍵ID 
      * @param int $deep選取深度 
    */ 
    function select($cid,$deep=0) 
    { 
        //獲取節點信息 
        $node=$this->get_by_cid($cid); 
        $where='where lft between '.$node['lft'].' and '.$node['rgt']; 
        if(!empty($deep))$where.=' and level<'.$node['level']+$deep; 
        if($deep==1){ 
            $where.=' order by orderstyle desc'; 
        }else{ 
            $where.=' order by lft asc';             
        } 
        return $this->module->select($where); 
    } 

    /** 
      * 獲取父節點路徑 
      * @param int $cid 節點的ID  
    */ 
    function get_parent($cid) 
    { 
        $node=$this->get_by_cid($cid); 
        return $this->module->select('where lft<='.$node['lft'].' and rgt>='.$node['rgt'].' order by lft asc'); 
    } 
    /** 
      * 選取子節點 
      * @param int $cid節點的主鍵ID 
      * @param int $deep選取深度 
    */ 
    function get_children($pid,$deep=0){ 
        //獲取節點信息 
        $pnode=$this->get_by_cid($pid); 
        $where='where lft>'.$pnode['lft'].' and rgt<'.$pnode['rgt']; 
        if(!empty($deep))$where.=' and level<='.($pnode['level']+$deep); 
        if($deep==1){ 
            $where.=' order by orderstyle desc'; 
        }else{ 
            $where.=' order by lft asc';             
        } 
        return $this->module->select($where); 
    } 

    /** 
      * 獲取第deep層子節點 
      * @param int $cid節點的主鍵ID 
      * @param int $deep選取深度 
    */ 
    function get_level_children($pid,$deep){ 
        //獲取節點信息 
        $pnode=$this->get_by_cid($pid); 
        $where='where lft>'.$pnode['lft'].' and rgt<'.$pnode['rgt']; 
        $where.=' and level='.($pnode['level']+$deep); 
        $where.=' order by orderstyle desc'; 
        return $this->module->select($where); 
    } 

    /** 
      * 獲取節點信息 
      * @param $cid 節點的主鍵ID 
      * @return array $node 
    */ 
    function get_by_cid($cid){ 
        $node=$this->module->detail('where cid='.$cid); 
        if(!$node)$this->error(__FUNCTION__.'():獲取節點'.$cid.'失敗!'); 
        return $node; 
    } 
    /** 
      * 獲取子節點的數目 
      * @param array $node 節點信息 
      * @return num 
    */ 
    function child_num($node){ 
        return ($node['rgt']-$node['lft']-1)/2; 
    } 
    /** 
      * 按照層次顯示分類 
      * @param int $cid節點的主鍵ID 
      * @output 
    */ 
    function display($cid) 
    { 
        $nodes=$this->select($cid); 
        foreach($nodes as $node){ 
            echo str_repeat('   ',$node['level']-1).$node['cname']."/n"; 
        } 
    } 
/*-------private-----------------------------------*/ 

    function error($msg){ 
        die('ERROR : file '.__FILE__.' function '.$msg); 
    } 

?> 

php技術自己前幾天寫的無限分類類,轉載需保留來源!

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

主站蜘蛛池模板: 遂溪县| 古丈县| 鲜城| 荣成市| 长沙市| 阜南县| 台东市| 饶平县| 太仓市| 永福县| 汨罗市| 荣昌县| 铜梁县| 延川县| 凯里市| 蒲江县| 锡林浩特市| 汽车| 台前县| 方城县| 剑河县| 仙游县| 钟祥市| 沙雅县| 虹口区| 遵义县| 哈巴河县| 烟台市| 日照市| 武宁县| 孝昌县| 沂南县| 德惠市| 黑龙江省| 利辛县| 静海县| 佛坪县| 高陵县| 天台县| 康定县| 乐清市|