php 無(wú)限分類的樹類代碼
更新時(shí)間:2009年12月03日 14:45:23 作者:
php tree 無(wú)限分類代碼,需要的朋友可以參考下。
復(fù)制代碼 代碼如下:
<?php
/**
by lenush;
*/
class Tree
{
var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
function Tree ($value)
{
$this->setNode(0, -1, $value);
} // end func
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
function getList (&$tree, $root= 0)
{
foreach ($this->child[$root] as $key=>$id)
{
$tree[] = $id;
if ($this->child[$id]) $this->getList($tree, $id);
}
} // end func
function getValue ($id)
{
return $this->data[$id];
} // end func
function getLayer ($id, $space = false)
{
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
} // end func
function getParent ($id)
{
return $this->parent[$id];
} // end func
function getParents ($id)
{
while ($this->parent[$id] != -1)
{
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
} // end func
function getChild ($id)
{
return $this->child[$id];
} // end func
function getChilds ($id = 0)
{
$child = array($id);
$this->getList($child, $id);
return $child;
} // end func
} // end class
//new Tree(根目錄的名字);
//根目錄的ID自動(dòng)分配為0
$Tree = new Tree('目錄導(dǎo)航');
//setNode(目錄ID,上級(jí)ID,目錄名字);
$Tree->setNode(1, 0, '目錄1');
$Tree->setNode(2, 1, '目錄2');
$Tree->setNode(3, 0, '目錄3');
$Tree->setNode(4, 3, '目錄3.1');
$Tree->setNode(5, 3, '目錄3.2');
$Tree->setNode(6, 3, '目錄3.3');
$Tree->setNode(7, 2, '目錄2.1');
$Tree->setNode(8, 2, '目錄2.2');
$Tree->setNode(9, 2, '目錄2.3');
$Tree->setNode(10, 6, '目錄3.3.1');
$Tree->setNode(11, 6, '目錄3.3.2');
$Tree->setNode(12, 6, '目錄3.3.3');
//getChilds(指定目錄ID);
//取得指定目錄下級(jí)目錄.如果沒(méi)有指定目錄就由根目錄開始
$category = $Tree->getChilds();
//遍歷輸出
foreach ($category as $key=>$id)
{
echo $Tree->getLayer($id, '|-').$Tree->getValue($id)."<br />\n";
}
?>
您可能感興趣的文章:
- php 無(wú)限級(jí)分類,超級(jí)簡(jiǎn)單的無(wú)限級(jí)分類,支持輸出樹狀圖
- PHP+Mysql樹型結(jié)構(gòu)(無(wú)限分類)數(shù)據(jù)庫(kù)設(shè)計(jì)的2種方式實(shí)例
- PHP超牛逼無(wú)限極分類生成樹方法
- PHP無(wú)限分類(樹形類)
- php+mysql實(shí)現(xiàn)無(wú)限級(jí)分類 | 樹型顯示分類關(guān)系
- php實(shí)現(xiàn)從ftp服務(wù)器上下載文件樹到本地電腦的程序
- php無(wú)限分類且支持輸出樹狀圖的詳細(xì)介紹
- php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類實(shí)例
- 用PHP實(shí)現(xiàn)多級(jí)樹型菜單
- php生成無(wú)限欄目樹
相關(guān)文章
使用PHP訪問(wèn)RabbitMQ消息隊(duì)列的方法示例
這篇文章主要介紹了使用PHP訪問(wèn)RabbitMQ消息隊(duì)列的方法,結(jié)合實(shí)例形式分析了RabbitMQ消息隊(duì)列的相關(guān)擴(kuò)展安裝、隊(duì)列建立、隊(duì)列綁定、消息發(fā)送、消息接收等相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
PHP使用mysql_fetch_row查詢獲得數(shù)據(jù)行列表的方法
這篇文章主要介紹了PHP使用mysql_fetch_row查詢獲得數(shù)據(jù)行列表的方法,涉及php中使用mysql_fetch_row操作數(shù)據(jù)庫(kù)的技巧,需要的朋友可以參考下2015-03-03
探討如何使用SimpleXML函數(shù)來(lái)加載和解析XML文檔
本篇文章是對(duì)使用SimpleXML函數(shù)來(lái)加載和解析XML文檔進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
基于win2003虛擬機(jī)中apache服務(wù)器的訪問(wèn)
下面小編就為大家?guī)?lái)一篇基于win2003虛擬機(jī)中apache服務(wù)器的訪問(wèn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

