php 無限分類的樹類代碼
更新時(shí)間:2009年12月03日 14:45:23 作者:
php tree 無限分類代碼,需要的朋友可以參考下。
復(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í)目錄.如果沒有指定目錄就由根目錄開始
$category = $Tree->getChilds();
//遍歷輸出
foreach ($category as $key=>$id)
{
echo $Tree->getLayer($id, '|-').$Tree->getValue($id)."<br />\n";
}
?>
您可能感興趣的文章:
- php 無限級(jí)分類,超級(jí)簡單的無限級(jí)分類,支持輸出樹狀圖
- PHP+Mysql樹型結(jié)構(gòu)(無限分類)數(shù)據(jù)庫設(shè)計(jì)的2種方式實(shí)例
- PHP超牛逼無限極分類生成樹方法
- PHP無限分類(樹形類)
- php+mysql實(shí)現(xiàn)無限級(jí)分類 | 樹型顯示分類關(guān)系
- php實(shí)現(xiàn)從ftp服務(wù)器上下載文件樹到本地電腦的程序
- php無限分類且支持輸出樹狀圖的詳細(xì)介紹
- php實(shí)現(xiàn)的樹形結(jié)構(gòu)數(shù)據(jù)存取類實(shí)例
- 用PHP實(shí)現(xiàn)多級(jí)樹型菜單
- php生成無限欄目樹
相關(guān)文章
PHP實(shí)現(xiàn)HTML頁面靜態(tài)化的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)HTML頁面靜態(tài)化的方法,分享了靜態(tài)處理的方法,靜態(tài)處理后的優(yōu)勢(shì),并提供了多種靜態(tài)的方法,感興趣的小伙伴們可以參考一下2015-11-11php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證
這篇文章主要介紹了php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證的方法,結(jié)合實(shí)例形式分析了PHP使用Header函數(shù)調(diào)用登錄驗(yàn)證及PHP_AUTH_PW和PHP_AUTH_USER進(jìn)行驗(yàn)證處理的相關(guān)技巧,需要的朋友可以參考下2016-05-05解決PHP程序運(yùn)行時(shí):Fatal error: Maximum execution time of 30 seconds
最近做的程序中涉及到的循環(huán)比較多且處理的情況較復(fù)雜,在運(yùn)行程序時(shí)出現(xiàn)執(zhí)行超時(shí)提示如下:Fatal error: Maximum execution time of 30 seconds exceeded in D:\php\AppServ\www\sum3\test.php on line 3通過在網(wǎng)上搜索,找到了解決方法和大家分享,下面來一起看看吧。2016-11-11實(shí)測(cè)在class的function中include的文件中非php的global全局環(huán)境
正如標(biāo)題所言經(jīng)測(cè)試的結(jié)果為:在class中include后,被include文件變量域已經(jīng)變成func中了,非全局.但是可以通過global提升,感興趣的朋友可以參考下哈,希望對(duì)大家有所幫助2013-07-07詳解WordPress中簡碼格式標(biāo)簽編寫的基本方法
這篇文章主要介紹了詳解WordPress中簡碼格式標(biāo)簽編寫的基本方法,文中講到了添加和移除簡碼等的一些PHP函數(shù)的用法,需要的朋友可以參考下2015-12-12