Copy Code code as follows:
<?php
/**
* @author Yanghuan
* @datetime
* @version 1.0.0
*/
/**
* Short description.
*
* Detail Description
* @author
* @version 1.0
* @copyright
* @access Public
*/
Class Tree
{
/**
* Description
* @var
* @since 1.0
* @access Private
*/
var $data = array ();
/**
* Description
* @var
* @since 1.0
* @access Private
*/
var $child = array ( -1=>array ());
/**
* Description
* @var
* @since 1.0
* @access Private
*/
var $layer = array ( -1=>-1);
/**
* Description
* @var
* @since 1.0
* @access Private
*/
var $parent = array ();
/**
* Short description.
*
& nbsp; * Detail Description
* @param None
* @global None
* @since 1.0
* @access private
* @return void
* @update Date Time
*/
function tree ($value)
{
& nbsp; $this->setnode (0,-1, $value);
}//End Func
/**
* Short description.
*
& nbsp; * Detail Description
* @param None
* @global None
* @since 1.0
* @access private
* @return void
* @update Date Time
/*
function Setnode ($id, $parent, $value)
{
$parent = $parent? $parent: 0;
$this->data[$id] = $value;
$this->child[$id] = array ();
$this->child[$parent] = $id;
$this->parent[$id] = $parent;
if (!isset ($this->layer[$parent]))
{
$this->layer[$id] = 0;
}
Else
{
$this->layer[$id] = $this->layer[$parent] + 1;
}
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
Function GetList (& $tree, $root = 0)
{
foreach ($this->child[$root] as $key => $id)
{
$tree [] = $id;
if ($this->child[$id]) $this->getlist ($tree, $id);
}
}//End Func
/**
* Short description.
*
& nbsp; * Detail Description
* @param None
* @global None
* @since 1.0
* @access private
* @return void
* @update Date Time
*/
function GetValue ($id)
{
& nbsp; return $this->data[$id];
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Getlayer ($id, $space = False)
{
Return $space str_repeat ($space, $this->layer[$id]): $this->layer[$id];
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function GetParent ($id)
{
return $this->parent[$id];
}//End Func
/**
* Short description.
*
* Detail Description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Getparents ($id)
{
while ($this->parent[$id]!=-1)
{
$id = $parent [$this->layer[$id]] = $this->parent[$id];
}
Ksort ($parent);
Reset ($parent);
return $parent;
}//End func
/**
* Short description.& nbsp
*
* Detail description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Getchild ($ ID)
{
return $this->child[$id];
}//End Func
/**
* Short description.
*
* Detail description
* @param None
* @global None
* @since 1.0
* @access Private
* @return void
* @update Date Time
*/
function Getchilds ( $id = 0)
{
$child = Array ($id);
$this->getlist ($child, $id);
return $child;
}//End Func
}//End Class
?>
How to use
PHP Code:
Copy Code code as follows:
<?php
New Tree (the name of the root directory);
The ID of the root directory is automatically assigned to 0
$Tree = new tree (' root directory ');
Setnode (directory ID, superior ID, directory name);
$Tree->setnode (1, 0, ' directory 1 ');
$Tree->setnode (2, 0, ' Directory 2 ');
$Tree->setnode (3, 0, ' Directory 3 ');
$Tree->setnode (4, 3, ' directory 3.1 ');
$Tree->setnode (5, 3, ' Directory 3.2 ');
$Tree->setnode (6, 3, ' directory 3.3 ');
$Tree->setnode (7, 2, ' Directory 2.1 ');
$Tree->setnode (8, 2, ' directory 2.2 ');
$Tree->setnode (9, 2, ' directory 2.3 ');
$Tree->setnode (10, 6, ' catalogue 3.3.1 ');
$Tree->setnode (11, 6, ' catalogue 3.3.2 ');
$Tree->setnode (12, 6, ' catalogue 3.3.3 ');
Getchilds (specify directory ID);
Gets the specified directory subordinate directory. If no directory is specified, it starts with the root directory.
$category = $Tree->getchilds ();
Traverse output
foreach ($category as $key => $id)
{
echo $Tree->getlayer ($id, ' | | '). $Tree->getvalue ($id). " <br>\n ";
}
PHP Infinite Classification-php100 code
Copy Code code as follows:
<?php
Infinite categories, looking for all the parent classes from subclasses
$ID Child class ID
function Php100_xd ($id) {
$sql = "SELECT * from FL where id= ' $id '";
$q =mysql_query ($sql);
$rs =mysql_fetch_array ($q);
$rs [' FID ']==0?] "": FL ($rs [' FID ']);
echo $rs [' name ']. -";
}
Read the subclass below all parent classes
$f What the top-level category starts with, $s style
function Php100_dx ($f =0, $s = "") {
$sql = "SELECT * from FL where fid= $f";
$q =mysql_query ($sql);
$s = $s. " -";
while ($rs =mysql_fetch_array ($q)) {
echo "<br> $s". $rs [' name '];
Flt ($rs [' id '], $s);
}
}