欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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";
}
?>

相關(guān)文章

  • discuz的php防止sql注入函數(shù)

    discuz的php防止sql注入函數(shù)

    最早開始學(xué)習(xí)php的時(shí)候根本沒考慮過安全方面的問題,那時(shí)候就是想能做出功能就是萬歲了。隨著做項(xiàng)目的時(shí)間慢慢加長,越來越感覺到網(wǎng)站安全方面的問題十分重要。
    2011-01-01
  • PHP實(shí)現(xiàn)HTML頁面靜態(tài)化的方法

    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-11
  • php使用Header函數(shù),PHP_AUTH_PW和PHP_AUTH_USER做用戶驗(yàn)證

    php使用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使用curl簡單抓取遠(yuǎn)程url的方法

    php使用curl簡單抓取遠(yuǎn)程url的方法

    這篇文章主要介紹了php使用curl簡單抓取遠(yuǎn)程url的方法,涉及php操作curl的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • 解決PHP程序運(yùn)行時(shí):Fatal error: Maximum execution time of 30 seconds exceeded in的錯(cuò)誤提示

    解決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
  • php 冒泡排序 交換排序法

    php 冒泡排序 交換排序法

    冒泡排序-php版本-交換排序法,需要的朋友可以參考下。
    2011-05-05
  • PHP中Notice錯(cuò)誤常見解決方法

    PHP中Notice錯(cuò)誤常見解決方法

    本篇文章主要介紹了PHP中Notice錯(cuò)誤常見解決方法,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-04-04
  • 實(shí)測(cè)在class的function中include的文件中非php的global全局環(huán)境

    實(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
  • 基于PHP制作通用的Excel導(dǎo)入程序

    基于PHP制作通用的Excel導(dǎo)入程序

    這篇文章主要為大家介紹了如何利用PHP制作一個(gè)通用的Excel導(dǎo)入程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下
    2022-04-04
  • 詳解WordPress中簡碼格式標(biāo)簽編寫的基本方法

    詳解WordPress中簡碼格式標(biāo)簽編寫的基本方法

    這篇文章主要介紹了詳解WordPress中簡碼格式標(biāo)簽編寫的基本方法,文中講到了添加和移除簡碼等的一些PHP函數(shù)的用法,需要的朋友可以參考下
    2015-12-12

最新評(píng)論