class Tree

Class Tree - the class for working with tree structures. It is optimized for high performance and work with lots of child elements. It can easily process a tree-like structure containing 25000-30000 elements in less than a second.

<?php

class Tree

{

/**
* Set elements sorting order by ID
* @param mixed $id — element identifier
* @param integer $order — sorting order
*/

public function setItemOrder($id , $order)

/**
* Sort child elements
* @param mixed $parentId — nor required;  a parent identifier -
* is the root node by default, which sorts all other nodes
*/

public function sortItems($parentId = false)

/**
* Check if the node exists by its identifier
* @param mixed $id
* @return boolean
*/

public function itemExists($id)

/**
* Get the number of elements in a tree
* @return integer
*/

public function getItemsCount()

/**
* Add a node to the tree
* @param mixed $id — unique identifier
* @param mixed $parent — parent node identifier
* @param mixed $data — node data
* @param integer $order  - sorting order, not required
* @return boolean —  successfully invoked
*/

public function addItem($id , $parent , $data , $order = false)

/**
* Update the node data
* @param mixed $id — node identifier
* @param mixed $data — node data
* @return boolean —  successfully invoked
*/

public function updateItem($id , $data)

/**
* Get node structure by ID
* @param mixed $id
* @return array массив с ключами ('id','parent','order','data')
*/

public function getItem($id)

/**
* Get node data by ID
* @param string $id
* @return mixed
*/

public function getItemData($id)

/**
* Check if the node has child elements
* @param string $id — node identifier
* @return boolean
*/

public function hasChilds($id)

/**
* Get data on all child elements (recursively)
* @var mixed id - parent node identifier
* @return array - an array with keys ('id','parent','order','data')
*/

public function getChildsR($id)

/**
* Get child nodes’ structures
* @var mixed id — parent node identifier
* @return array - an array with keys ('id','parent','order','data')
*/

public function getChilds($id)

/**
* Get the parent node identifier by the child node identifier
* @param string $id — child node identifier
* @return mixed string or false
*/

public function getParentId($id)

/**
* Change the parent node for the node
* @param mixed $id — node identifier
* @param mixed $newParent — new parent node identifier
* @return boolean
*/

public function changeParent($id , $newParent)

/**
* Delete node
* @param mixed $id
* @return void
*/

public function removeItem($id)

/**
* Get structures of the tree elements (nodes)
* @return array - an array with keys ('id','parent','order','data')
*/

public function getItems()

}

Tree Example:

<?php // Create node function function createNode($tree , $parent) { $s=''; if(!$tree->hasChilds($parent)) return ''; $childs = $tree->getChilds($parent); foreach ($childs as $k=>$v) { $s.='<div style="padding-left: 15px;"> <div class="menuItem"> <a href="'.$v['data']['code'].'">'.$v['data']['title'].'</a> <p>'.$v['data']['somedata'].'</p> </div>'; if($tree->hasChilds($v['id'])) $s.=createNode($tree , $v['id']); $s.='</div>'; } return $s; } // collecting data $data = array( array('id'=>1,'code'=>'index','parent'=>0,'order'=>0,'title'=>'Main','somedata'=>'text 1'), array('id'=>2,'code'=>'store','parent'=>0,'order'=>1,'title'=>'Store','somedata'=>'text 2'), array('id'=>3,'code'=>'catalog','parent'=>2,'order'=>0,'title'=>'Store - Catalog','somedata'=>'text 3'), array('id'=>4,'code'=>'goods','parent'=>2,'order'=>1,'title'=>'Store - Catalog - Goods','somedata'=>'text 4'), array('id'=>5,'code'=>'price','parent'=>4,'order'=>2,'title'=>'Store - Catalog - Goods - Price','somedata'=>'text 5'), ); $tree = new Tree(); // Inserting data into the tree foreach ($data as $item) { $tree->addItem( $item['id'], $item['parent'], array( 'title'=>$item['title'] , 'code'=> $item['code'] , 'somedata'=>$item['somedata'] ), $item['order'] ); } // Displaying the tree echo createNode($tree , 0);

comments powered by Disqus