The infinite classification class I wrote a few days ago_PHP tutorial

WBOY
Release: 2016-07-21 15:57:46
Original
774 people have browsed it

I wrote this a week ago, and it works pretty well in use.

The main idea comes from: http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url]

I won’t explain the principle here, just send the code directly.

PS: This code cannot be used directly and must be combined with some of my other library classes. It should be said that thought is the most important. Here we mainly provide a classification idea.

Copy code The code is as follows:

/** 
--  
-- 表的结构 `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);
}

/**
* Add child node
* @param array $node The attributes of the child node to be added
* @param int $pid The ID of the parent node
*/
function add($node,$pid){
//Check whether the node already exists
if($node_exist=$this- >module->detail('where pid='.$pid.' and cname=''.$node['cname'].''')){
                                                                                                                                                 __FUNCTION__.'(): The node '.$node['cname'].' already exists! ');
           //print_r($node_exist); >                                                                                                                                                                                                                    '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']);
//Insert new node
$node['pid' ]=$pid;
$node['lft']=$pnode['rgt'];
$node['rgt']=$pnode['rgt']+1;
$node ['level']=$pnode['level']+1; // Add one to the level
return $this->module->add($node);
}

/**
* Delete node
* @param $cid ID of the node to be deleted
* @param $delete_childern If the node has child nodes, whether to force deletion.If the setting is not true, when there is a child node, the deletion fails and returns false
* *
*/
function delete($cid,$delete_childern=false)
{
//Get Node information
$node=$this->get_by_cid($cid);
if(($this->child_num($node)>0)&&(!$delete_childern))$this-> ;error(__FUNCTION__.'(): This node has child nodes!');
//Delete the node and all its child nodes
$this->module->delete('where lft between '.$node['lft'].' and '.$node ['rgt']);
//Modify the corresponding left and right key values ​​
$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;                                                       $set,'where cid='.$cid);
}

/**
* Update a node
* @param array $set update set
* @param int $cid The primary key ID of the updated node
*/
function select($cid,$deep=0)
{
                                                                                                                                                                                                                                                                                                    ['rgt'];
if(!empty($deep))$where.=' and level<'.$node['level']+$deep;
if($deep==1) {
          $where.=' order by orderstyle desc';                                                           🎜> return $this->module- >select($where);
}                                      get_by_cid($cid);
return $this->module->select('where lft<='.$node['lft'].' and rgt>='.$node['rgt'] .' order by lft asc');

    /**
* Select child nodes*/ 
    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); 
    } 

    /**
* Get the deep layer sub-node
* @param int $cid node’s primary key ID
* @param int $deep selection depth
*/ 
    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); 
    } 

    /**
* Get node information
* @param $cid The primary key ID of the node
* @return array $node
*/ 
    function get_by_cid($cid){ 
        $node=$this->module->detail('where cid='.$cid); 
        if(!$node)$this->error(__FUNCTION__.'():获取节点'.$cid.'失败!'); ['lft']-1)/2;
}
/**
* Get the number of child nodes
* @param array $node node information
* @return num
*/
function display($cid)
{ select($cid);
foreach($nodes as $node){
echo str_repeat(' ',$node['level']-1).$node['cname']."n";
                                                                                                             ------*/

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





http://www.bkjia.com/PHPjc/317731.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/317731.html
TechArticle

I wrote it a week ago, and the effect is pretty good in use. The main idea comes from: http://www.phpobject.net/b...[url=http://www.phpobject.net/blog/read.php?49][/url] I won’t explain the principle here. ...

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!