PHP Tree-type structure operation Class Code (_php) tutorial

Source: Internet
Author: User
Tree structure is a lot of program members will use, the above on the tree-type structure of the Operation class, a good solution to this problem oh.

/php Tutorial Tree-type structure Operation class code
/***************************************************************
* Tree structure operation class (ideal if it can be written as a stored procedure)
*
* ***************************************************************/

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;
/**
* Constructor function
* @param string $table table name
* @param object $dbhanle ADODB Database tutorial action handle
*/
Function TreeNode ($table, $dbhandle) {
$this->db = $dbhandle;
$this->table = $table;
$this->db->debug = true;
}
/**
* Add child nodes
* @param array $data node 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;//Gets the ordinal number of the node to be inserted
$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); Inserting node data
if (! $this->db->affected_rows ()) {
return false;
}

$this->buildtree (a); Rebuilding node left and right values
$this->updatelevel (1); Generate node-level values
return true;
}
/**
* Modify the data of the node
* @param int $id Node ID number
* @param array $data node data
* @return BOOL
*/1 2 3 4

http://www.bkjia.com/PHPjc/444898.html www.bkjia.com true http://www.bkjia.com/PHPjc/444898.html techarticle tree structure is a lot of program members will use, the above on the tree-type structure of the Operation class, a good solution to this problem oh. /php Tutorial Tree-type structure operation class Code/************** ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.