Send a self-written PHP tree class tree for PHP, how to solve

Source: Internet
Author: User
Tags erro
Send a PHP tree class tree for PHP
I often do infinite pole classification.
Need a tree of legendary data structures
But I got a look.
I can't seem to find the right one for myself.
I wrote one today.
Get the Sun.
Infinite Polar tree classes that require database support

I hope that all the friends to guide, add,
-------------Open Source Manifesto--------------
Open source brings us the need to reduce repetitive work
Achieve common improvement

PHP Code
  
  ' localhost ', ' db_user ' = ' root ', ' db_pass ' = ' 123456 ', ' db_name ' = ' sy_speedphp '); function __construct ($mode = ' db ', $name, $tb _file_name= ' tree ', $db _data=null) 2, add node//Insert successful return insert Id$data for data domain function AddNode ($fid =0, $data) 3, modify the information of a node can only modify the FID and data domain//if not specified. Default function Mdfnode ($node _id, $fid =false, $data =false) 4, delete node and its child node ID as Node ID mode is delete mode ture for direct deletion. False to move the specified node and child nodes into a new tree and return the name of a random new tree function Deletenode ($id, $mode =true) 5, Gets the child node function does not include the specified root node recursive call functions Getsub ($ FID, $deep =8888, $storage _mode=true, $recall =null) $fid as root node Id$deep: Traversal depth starting from 1 1 that is child node 2 is the Sun node $storage_mode=true Use a multidimensional array to return a parent-child relationship $storage_mode=false When you use a one-dimensional array to return a $recall callback function that does not log a parent-child relationship if it is not empty during the traversal each data is called by the function callback function that defines the following functions Function_name ( $node _data, $deep); $node _data is the deep depth of the node data, starting from 1 indicates that the distance from the node to the specified root node (FID) is passed when the function MyFunction ($node _data, $deep) {code};//The custom function, as shown below. $tree 1->getsub (0,8888,true, "myfunction")//Use function name as parameter when saving as a multidimensional array the structure is as follows Array (Fid1=>array (), fid2=& Gt;array (fid21=>array (Id,name,fid,data), Fid22=>array (Id,name,fid,data),) $fid 3=>data; ) Where Fid1,fid2 ... The ID of the root node whose corresponding value is child node information//NOTE: Only the leaf node can return the specific data//$deep to the depth default to 1, which returns all such as Deep=1, returns the first layer of child node 6, gets the node information returns the node information and adds a key value of Sub_count to the array. = = Number of child nodes function getnode (4); 7,//Get root up category information function getfarlist ($node _id, $fadeep)//@param: $fadeep up level returns data from most Near the row in front Fadeep returns a two-dimensional array starting from 1, and commits the query if the INSERT update delete only returns TRUE or False if Selec returns a two-dimensional array function db_query ($sq L) 9, error message function can be customized modify currently just display error message function Erro ($msg); *///set debug mode switch true to open output SQL statement define (' Debug_mode ', false); class CLS _tree{//Data storage mode ' file ' | ' DB ' | '        Mem ' are files and databases respectively//If you need to specify a file location as a file//If you save as a database, you need to create a table and specify the database link related configuration information private $mode; Private $db _host;//Host private $db _port;//port default to 3306 private $db _user;//user name private $db _pass;//password private $db _n        ame;//database name; Private $TB _name;//tree structure table name private $file _name;//data file location private $link _id;//database link ID//private $link _tb;//and the table name associated with the tree node is used to modify the name of the private $tree _name;//tree Each tree must have one of its own names in a database table where multiple tree public $debug are necessarily present;//When set to True Wait for debug state//constructor//Link database use database table as datacenter//database or data file name to construct array{$db _host=null, $db _user, $db _pass, $db _name, $filename} */Public function __construct ($mode = ' db ', $name, $TB _file_na Me= ' tree ', $db _data=null) {if ($mode = = ' db ')//database {$this->li                        Nk_id=mysql_connect ($db _data[' db_host '), $db _data[' Db_user '], $db _data[' Db_pass ']);                if (! $this->link_id) {echo ' Can not connect to MySQL Server ';            return false;  } if (! mysql_select_db ($db _data[' db_name '), $this->link_id) {echo ' Can                      Not use MySQL Database ';            return false; }//Set debug mode output SQL statement $this->debug=debug_mode;            $this->tb_name= $TB _file_name;                        $this->tree_name= $name;                    mysql_query ("Set names ' gb2312 ';", $this->link_id);        }//If you use the database to store data else if ($mode = = ' file ') {$this->file_name= $tb _file_name; } else if ($mode = = ' mem ')//Other way memory {} else {die (' Erro to con        struct object without storage mode '); }}//__construct//Add a node//if the parent node ID is 0 insert for root node//insert successfully returns INSERT ID function AddNode ($fid =0, $data) {$ Data=mysql_escape_string ($data);//msyql filter if ($FID)//If parent node ID is not 0 {//If parent node ID is nonzero need to check for existence of parent section Point $sql = ' SELECT count (*) as sum from '. $this->tb_name. ' Where c_id= '. $fid. ' and c_name=\ '. $this->tree_name. '            \';';            $re = $this->db_query ($sql); if ($re [0][' Sum ']!=1) {$this->erro (' not exist PARent node, parent ID: '. $fid. ' 
'); return; The//////performs an insert node $sql = ' insert INTO '. $this->tb_name. ' Values (NULL, '. ') {$this->tree_name} ', ' {$fid} ', ' $data '); "; if ($this->db_query ($sql)) {return mysql_insert_id ($this->link_id); } else {return false; }}//Insert as root node requires query tree name is already used a tree only one root node executes the Insert node else {$sql = ' select count (*) as Sum F Rom '. $this->tb_name. ' Where c_name=\ '. $this->tree_name. ' \';'; $re = $this->db_query ($sql); if ($re [0][' Sum ']!=0) {$this->erro (' Can not create Tree,even existed tree name: '. $this->tr Ee_name. '
'); return; } else {$sql = ' insert INTO '. $this->tb_name. ' Values (NULL, '. ') {$this->tree_name} ', ' {$fid} ', ' $data '); "; if ($this->db_query ($sql)) {return mysql_insert_id ($this->link_id); } } } }
  • Related Article

    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.