Home > php教程 > php手册 > php 树型结构操作类代码(1/4)

php 树型结构操作类代码(1/4)

WBOY
Release: 2016-06-13 11:24:11
Original
833 people have browsed it

树型结构是很多程序会员会用到的,上面这款关于树型结构操作类,很好的解决了这个问题哦。

/php教程 树型结构操作类代码
/***************************************************************
* 树型结构操作类(如果可以写成存储过程最理想)
*
*  ***************************************************************/

class treenode {
        var $f_id = 'id';
        var $f_pid = 'pid';
        var $f_lft = 'lft';
        var $f_rgt = 'rgt';
        var $f_s = 'sequence';
        var $f_level = 'lev';
        var $f_child_num = 'child_num';
        var $table;
        var $db;
        /**
         * 构造函数
         * @param string $table 表名
         * @param object $dbhanle adodb数据库教程操作句柄
         */
        function treenode($table, $dbhandle) {
                $this->db = $dbhandle;
                $this->table = $table;
                //$this->db->debug = true;
        }
        /**
         * 增加子节点
         * @param array $data 节点数据
         * @return bool
         */
        function addchild($data){
                $pid = $data[$this->f_pid];
                $sql = "select max({$this->f_s}) from {$this->table} where {$this->f_pid}=$pid";
                $data[$this->f_s] = $this->db->getone($sql) + 1;//得到待插入节点的序号
                $sql = "select * from {$this->table} where {$this->f_id} = -1";
                $rs = $this->db->execute($sql);
                $sql = $this->db->getinsertsql($rs, $data);
                $this->db->execute($sql); //插入节点数据
                if(!$this->db->affected_rows()){
                        return false;
                }
               
                $this->buildtree(1,1);        //重建节点左右值
                $this->updatelevel(1);        //生成节点级数值
                return true;
        }
        /**
         * 修改节点的数据
         * @param int $id 节点id号
         * @param array $data 节点数据
         * @return bool
         */1 2 3 4

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template