php無限級分頁代碼

來源:互聯網
上載者:User

php無限級分頁代碼
class Tree
{
        public $data=array();
        public $cateArray=array();
        
        function Tree()
        {
               
        }
        function setNode ($id, $parent, $value)
    {
        $parent = $parent?$parent:0;
        $this->data[$id]                = $value;
        $this->cateArray[$id]        = $parent;
    }
    function getChildsTree($id=0)
    {
            $childs=array();
            foreach ($this->cateArray as $child=>$parent)
            {
                    if ($parent==$id)
                    {
                            $childs[$child]=$this->getChildsTree($child);
                    }
                    
            }
            return $childs;
    }
    function getChilds($id=0)
    {
            $childArray=array();
            $childs=$this->getChild($id);
            foreach ($childs as $child)
            {
                    $childArray[]=$child;
                    $childArray=array_merge($childArray,$this->getChilds($child));
            }
            return $childArray;
    }
    function getChild($id)
    {
            $childs=array();
            foreach ($this->cateArray as $child=>$parent)
            {
                    if ($parent==$id)
                    {
                            $childs[$child]=$child;
                    }
            }
            return $childs;
    }
    //單線擷取父節點
    function getNodeLever($id)
    {
            $parents=array();
            if (key_exists($this->cateArray[$id],$this->cateArray))
            {
                    $parents[]=$this->cateArray[$id];
                    $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
            }
            return $parents;
    }
    function getLayer($id,$preStr='|-')
    {
            return str_repeat($preStr,count($this->getNodeLever($id)));
    }
    function getValue ($id)
    {
        return $this->data[$id];
    } // end func
}

/*$Tree = new Tree("請選擇分類");
//setNode(目錄ID,上級ID,目錄名字);
$Tree->setNode(1, 0, '目錄1');
$Tree->setNode(2, 1, '目錄2');
$Tree->setNode(5, 3, '目錄5');
$Tree->setNode(3, 0, '目錄3');
$Tree->setNode(4, 2, '目錄4');
$Tree->setNode(9, 4, '目錄9');
$Tree->setNode(6, 2, '目錄6');
$Tree->setNode(7, 2, '目錄7');
$Tree->setNode(8, 3, '目錄8');

//print_r($Tree->getChildsTree(0));
//print_r($Tree->getChild(0));
//print_r($Tree->getLayer(2));

$category = $Tree->getChilds();

//遍曆輸出
foreach ($category as $key=>$id)
{
    echo $id.$Tree->getLayer($id, '|-').$Tree->getValue($id)."\n";
}*/

?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.