写不下了

发表于:2007-06-21来源:作者:点击数: 标签:
================================================== file name: dboper.inc. php codes: ================================================== ? class dboper { var $items; var $result=0; function find($l_value) { for($j=1;$j=$this-items[info_all]

   
   ==================================================
file name:
dboper.inc.php
codes:
==================================================
<?
class dboper {
    var $items;

    var $result=0;
    
    function find($l_value) {
        for($j=1;$j<=$this->items["info_all"];$j++) {
            if($l_value==$this->items["d_id][$j]"])
            { return($j);exit; }
        
        }
            
    }
    
    function del($id) {
        //echo "del"." ".$id."<br>";
        include("conn_db.php");
        $Sql_Query="delete from department where d_id=$id";
        $Query_Db=mysql_query($Sql_Query,$Connect);
        
        //echo $Sql_Query;
        include("conn_close.php");
    }
    
    function update($d_id,$f_node,$l_node,$r_node) {
        include("conn_db.php");
        $Sql_Query="update department set f_node=".$f_node.",l_node=".$l_node.",r_node=".$r_node." where d_id=".$d_id;
        $Query_Db=mysql_query($Sql_Query,$Connect);
        
        //echo $Sql_Query;
        include("conn_close.php");
    }
        
    function d_tree($id,$base_id){//
        //echo "do<br>";
        $m=$this->find($base_id);//m is the position of $base_id in arrays
        $i=$this->find($id);
        
        if(($this->items["l_node][$i]"]!=0))
        {    
            //echo "向左下一级";
            //echo "<br>";
            $i=$this->find($this->items["l_node][$i]"]);
            $this->d_tree($this->items["d_id][$i]"],$base_id);
            //exit;
        }
        if(($this->items["r_node][$i]"]!=0)&&($this->items["l_node][$i]"]==0))
        {    
            //echo "向右下一级";
            //echo "<br>";
            $i=$this->find($this->items["r_node][$i]"]);
            $this->d_tree($this->items["d_id][$i]"],$base_id);
            //exit;
        }        
        
        if(($this->items["r_node][$i]"]==0)&&($this->items["l_node][$i]"]==0))
        {
            if($this->result==1){//very important method to exit my function
                return true;
            }
            $this->del($this->items["d_id][$i]"]);
            $j=$i;//save this node information
            $i=$this->find($this->items["f_node][$i]"]);//find it's parent node
            //begin-----------------------------------------------------
            //if this node is the node you want to delete,delete it and exit function
            if(($this->items["r_node][$i]"]==$base_id)||($this->items["l_node][$i]"]==$base_id))
            {
                $this->result=1;
                return true;
            }
            //end--------------------------------------------------------
            else {
            //begin------------------------------------------------------
            //if this node is the child node of you wanted to deleted node
            //up to parent node and continue find
                if($this->items["r_node][$i]"]==$this->items["d_id][$j]"])
                {
                    //echo "左上一级";
                    //echo "<br>";
                    $this->items["r_node][$i]"]=0;
                    $this->d_tree($this->items["d_id][$i]"],$base_id);
                    //exit;
                }
                
                if($this->items["l_node][$i]"]==$this->items["d_id][$j]"])
                {
                    //echo "右上一级";
                    //echo "<br>";
                    $this->items["l_node][$i]"]=0;
                    $this->d_tree($this->items["d_id][$i]"],$base_id);
                    //exit;
                }
            //end----------------------------------------------------------    
            }
            
        }        
    }    

    function d_node_tree($id,$base_id) {
        $m=$this->find($base_id);//m is the position of $base_id in arrays
        $i=$this->find($id);
        
        if(($this->items["l_node][$m]"]!=0))
        {
            $i=$this->find($this->items["l_node][$m]"]);//current node's left child node
            $j=$this->find($this->items["f_node][$m]"]);//current node's parent
            $this->d_tree($this->items["d_id][$i]"],$this->items["d_id][$i]"]);//del whole left child tree
            if($this->items["r_node][$m]"]!=0) {
                if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
                {
                    $i=$this->find($this->items["r_node][$m]"]);
                    $this->items["r_node][$j]"]=$this->items["d_id][$i]"];
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
                    //update where id = $this->items["d_id][$j]"]
                    $this->items["f_node][$i]"]=$this->items["d_id][$j]"];
                    $this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
                    //update where id = $this->items["d_id][$i]"]
                }
                
                if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
                {
                    $i=$this->find($this->items["r_node][$m]"]);
                    $this->items["l_node][$j]"]=$this->items["d_id][$i]"];
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
                    //update where id = $this->items["d_id][$j]"]
                    $this->items["f_node][$i]"]=$this->items["d_id][$j]"];
                    $this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
                    //update where id = $this->items["d_id][$i]"]
                }
                $this->del($this->items["d_id][$m]"]);
            }
            
            if($this->items["r_node][$m]"]==0) {
                if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
                {
                    $this->items["r_node][$j]"]=0;
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
                    //update where id = $this->items["d_id][$j]"]
                }
                
                if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
                {
                    $this->items["l_node][$j]"]=0;
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
                    //update where id = $this->items["d_id][$j]"]
                }
                $this->del($this->items["d_id][$m]"]);//del node it self
            }
            return true;
        }
        
        if(($this->items["l_node][$m]"]==0))
        {
            if($this->items["r_node][$m]"]!=0) {
                
                $j=$this->find($this->items["f_node][$m]"]);//current node's parent
                if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
                {
                    $i=$this->find($this->items["r_node][$m]"]);//current node's right child node
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["d_id][$i]"]);
                    //update where id = $this->items["d_id][$j]"]
                    $this->update($this->items["d_id][$i]"],$this->items["d_id][$j]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
                    //update where id = $this->items["d_id][$i]"]
                }
                
                if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])//if it's parent's right child node
                {
                    $i=$this->find($this->items["r_node][$m]"]);
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["d_id][$i]"],$this->items["r_node][$j]"]);
                    //update where id = $this->items["d_id][$j]"]
                    $this->items["f_node][$i]"]=$this->items["d_id][$j]"];
                    $this->update($this->items["d_id][$i]"],$this->items["d_id][$j]"],$this->items["l_node][$i]"],$this->items["r_node][$i]"]);
                    //update where id = $this->items["d_id][$i]"]
                }
            }
            if($this->items["r_node][$m]"]==0) {
                $j=$this->find($this->items["f_node][$m]"]);//current node's parent
                if($this->items["r_node][$j]"]==$this->items["d_id][$m]"])
                {
                    $this->items["r_node][$j]"]=0;
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
                    //update where id = $this->items["d_id][$j]"]
                }
                if($this->items["l_node][$j]"]==$this->items["d_id][$m]"])
                {
                    $this->items["l_node][$j]"]=0;
                    $this->update($this->items["d_id][$j]"],$this->items["f_node][$j]"],$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
                    //update where id = $this->items["d_id][$j]"]
                }
            }        
            $this->del($this->items["d_id][$m]"]);
            return true;
        }
    }
    
    function showinfo_0($status){//
        include("conn_db.php");
        $Sql_Query="select * from department order by d_id";
        $Query_Db=mysql_query($Sql_Query,$Connect);
        $info_all=mysql_num_rows($Query_Db);
        $this->items["info_all"]=$info_all;
        if($status==1) {//when status=1 show all data gets from database
            $MNums=$info_all;
            }
        if($status==0) {//when status=0 show 6 messages gets from database
            $MNums=6;
            }
        for($i=1;$i<=$MNums;$i++)
            {
            $Messages=mysql_fetch_array($Query_Db);
            $this->items["d_id][$i]"]=$Messages["d_id"];
            $this->items["f_node][$i]"]=$Messages["f_node"];
            $this->items["node_value][$i]"]=$Messages["node_value"];
            $this->items["l_node][$i]"]=$Messages["l_node"];
            $this->items["r_node][$i]"]=$Messages["r_node"];
            }
        include("conn_close.php");
        }
}
?>

==================================================
file name:
conn_db.php
codes:
==================================================
<?
$Connect = @mysql_connect('localhost','root','') or die("could not connet to server please try later");
mysql_select_db("testxxx",$Connect);
?>

==================================================
file name:
addnode.inc.php
codes:
==================================================

<?
class addnode{
    
    function find($l_value) {
        for($j=1;$j<=$this->items["info_all"];$j++) {
            if($l_value==$this->items["d_id][$j]"])
            { return($j);exit; }
        
        }
            
    }
    
    function search_id() {
        include("conn_db.php");
        $Sql_Query="select * from department order by d_id desc";
        $Query_Db=mysql_query($Sql_Query,$Connect);
        $Messages=mysql_fetch_array($Query_Db);
        include("conn_close.php");
        return $Messages["d_id"];
        }    
        
    function insertnode($f_node,$node_value,$r_node) {
        include("conn_db.php");
        $Sql_Query="insert into department values('','$f_node','$node_value','0','$r_node')";
        $Query_Db=mysql_query($Sql_Query,$Connect);
        include("conn_close.php");
    }

    function update($d_id,$f_node,$l_node,$r_node) {
        include("conn_db.php");
        $Sql_Query="update department set f_node=".$f_node.",l_node=".$l_node.",r_node=".$r_node." where d_id=".$d_id;
        $Query_Db=mysql_query($Sql_Query,$Connect);
        
        //echo $Sql_Query;
        include("conn_close.php");
    }
        
    function add($id,$node_value) {
        $i=$this->find($id);
        if(($this->items["l_node][$i]"]!=0))
        {        
            $j=$this->find($this->items["l_node][$i]"]);
            
            $this->update($this->items["d_id][$j]"],0,$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
            $this->insertnode($this->items["d_id][$i]"],$node_value,$this->items["d_id][$j]"]);
            $c_d_id=$this->search_id();
            $this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$c_d_id,$this->items["r_node][$i]"]);
            $this->update($this->items["d_id][$j]"],$c_d_id,$this->items["l_node][$j]"],$this->items["r_node][$j]"]);
        }
        if(($this->items["l_node][$i]"]==0))
        {    
            $this->insertnode($this->items["d_id][$i]"],$node_value,0);
            $this->update($this->items["d_id][$i]"],$this->items["f_node][$i]"],$this->search_id($this->items["d_id][$i]"]),$this->items["r_node][$i]"]);
            
        }
        
        
    }

    function showinfo_0($status){//
        include("conn_db.php");
        $Sql_Query="select * from department order by d_id";
        $Query_Db=mysql_query($Sql_Query,$Connect);
        $info_all=mysql_num_rows($Query_Db);
        $this->items["info_all"]=$info_all;
        if($status==1) {//when status=1 show all data gets from database
            $MNums=$info_all;
            }
        if($status==0) {//when status=0 show 6 messages gets from database
            $MNums=6;
            }
        for($i=1;$i<=$MNums;$i++)
            {
            $Messages=mysql_fetch_array($Query_Db);
            $this->items["d_id][$i]"]=$Messages["d_id"];
            $this->items["f_node][$i]"]=$Messages["f_node"];
            $this->items["node_value][$i]"]=$Messages["node_value"];
            $this->items["l_node][$i]"]=$Messages["l_node"];
            $this->items["r_node][$i]"]=$Messages["r_node"];
            }
        include("conn_close.php");
        }

}
?>

原文转自:http://www.ltesting.net