php 系統樹形類,以樹形方式顯示

來源:互聯網
上載者:User

標籤:io   ar   sp   for   on   資料   bs   cti   ad   

<?php
/**
*系統樹形類,以樹形方式顯示
*
*
*/
header("Content-type: text/html; charset=utf-8;");
class TreeModel{
    /**
    *樹形標識
    */
    public $icon = array(‘│‘,‘├‘,‘└‘);
    public $nbsp = "&nbsp;&nbsp;&nbsp;";
    /**
    *樹形的二維數組資料
    */
    public $arr = array();
    public $arrTree = array();
    public $ret;
    
    /**
    *初始化數組,傳入的數組必須是
    * array(
    *      1 => array(‘id‘=>‘1‘,‘parentid‘=>0,‘name‘=>‘一級欄目一‘),
    *      2 => array(‘id‘=>‘2‘,‘parentid‘=>0,‘name‘=>‘一級欄目二‘),
    *      3 => array(‘id‘=>‘3‘,‘parentid‘=>1,‘name‘=>‘二級欄目一‘)
    *)這種形式 數組下標與當前資料ID相同
    */
    public function init($arr = array()){
        if(!empty($arr) && is_array($arr)) {
             $this->arr = $arr;
        }
        return $this->arr;
    }
    
    //-------------------------------------------------------------------
    
    /**
    *獲得全部的樹形資料
    *每個父類的子類在$arr[son]資料中
    *佔用空間比較大
    */
    public function GetArrayTreeSpace()
    {
        $tmpArray    = array();  //臨時數組
        $tmpSonArray = array();
        $tmp         = array();

            $tmpArray = $this->arr;

        $key = ‘‘;
        foreach($tmpArray as $key=>$tmp) {
            if($tmp[‘parentid‘]) {//如果存在父級ID
                $tmpArray[$tmp[‘parentid‘]][‘son‘][] = &$tmpArray[$key];
                $tmpSonArray[] = $key;
            }
        }
        foreach($tmpSonArray as $key) {
            unset($tmpArray[$key]);
        }
        unset($tmpSonArray);
        return $this->arrTree = $tmpArray;
    }
    /**
    *獲得此類的同級父數組,
    *
    */
    public function GetParent($myid) {
        $tmp = array();
        $parentid = $this->arr[$myid][‘parentid‘];
        $parentid = $this->arr[$parentid][‘parentid‘];
        foreach($this->arr as $v) {
            if($parentid == $v[‘parentid‘]) {
                $tmp[] = $v;
            }
        }
    }
    
    /**
    *獲得此類的子類
    *
    */
    public function GetChild($myid) {
        $tmp = array();
        foreach($this->arr as $key=>$v) {
            if($v[‘parentid‘] == $myid){
                $tmp[$key] = $v;
            }
        }
        return $tmp;
    }
    /**
    *輸出html格式的樹形
    *@param 資料
    *@param 輸出的格式
    */
    public function GetViewTreeSpace($arr,$str,$adds=‘‘) {
        $ret = ‘‘;
        $nbsp = $this->nbsp;
        $spacer = $adds ? $this->icon[1].$nbsp.$adds : ‘‘;
        foreach($arr as $tmp) {
            @extract($tmp); //轉換數組下標為變數
            eval("\$nstr = \"$str\";");
            $ret .= $nstr;
            if(isset($tmp[‘son‘])) {
                $ret .= $this->GetViewTreeSpace($tmp[‘son‘],$str,$nbsp.$this->icon[2].$adds);
            }
        }
        return $ret;
    }
    /**
    *輸出html格式的樹形
    *@param 父級ID
    *@param 輸出的格式
    */
    public function GetViewTree($myid,$str,$adds=‘‘) {
        $child  = $this->GetChild($myid);
        $nbsp = $this->nbsp;
        $spacer = $adds ? $this->icon[1].$nbsp.$adds : ‘‘;
        foreach($child as $key=>$tmp) {
            @extract($tmp); //轉換數組下標為變數
            eval("\$nstr = \"$str\";");
            $this->ret .= $nstr;
            $this->GetViewTree($key,$str,$nbsp.$this->icon[2].$adds);
        }
        return $this->ret;
    }

}


    $array =  array(
          1 => array(‘id‘=>‘1‘,‘parentid‘=>0,‘name‘=>‘一級欄目一‘),
          2 => array(‘id‘=>‘2‘,‘parentid‘=>0,‘name‘=>‘一級欄目二‘),
          3 => array(‘id‘=>‘3‘,‘parentid‘=>1,‘name‘=>‘二級欄目一‘),
          4 => array(‘id‘=>‘4‘,‘parentid‘=>0,‘name‘=>‘一級欄目一‘),
          5 => array(‘id‘=>‘5‘,‘parentid‘=>2,‘name‘=>‘二級欄目二‘),
          6 => array(‘id‘=>‘6‘,‘parentid‘=>5,‘name‘=>‘三級欄目一‘),
          7 => array(‘id‘=>‘7‘,‘parentid‘=>3,‘name‘=>‘三級欄目一‘),
          8 => array(‘id‘=>‘8‘,‘parentid‘=>7,‘name‘=>‘四級欄目二‘),
          9 => array(‘id‘=>‘9‘,‘parentid‘=>1,‘name‘=>‘二級欄目一‘),
          10 => array(‘id‘=>‘10‘,‘parentid‘=>0,‘name‘=>‘一級欄目一‘),
          11 => array(‘id‘=>‘11‘,‘parentid‘=>2,‘name‘=>‘二級欄目二‘)
          );
        $Trees = new TreeModel();
        $Trees->init($array);
        //print_r($array);
        $str = ‘<li>$spacer $name [id] $id</li>‘;
        echo $Trees->GetViewTree(0, $str);
        echo ‘<br/><br/><br/><br/>‘;
        $arr = $Trees->GetArrayTreeSpace();
        
        echo $Trees->GetViewTreeSpace($arr,$str);

 

 

兩次結果輸出如下:

 

  • 一級欄目一 [id] 1
  • ├      └ 二級欄目一 [id] 3
  • ├      └   └ 三級欄目一 [id] 7
  • ├      └   └   └ 四級欄目二 [id] 8
  • ├      └ 二級欄目一 [id] 9
  • 一級欄目二 [id] 2
  • ├      └ 二級欄目二 [id] 5
  • ├      └   └ 三級欄目一 [id] 6
  • ├      └ 二級欄目二 [id] 11
  • 一級欄目一 [id] 4
  • 一級欄目一 [id] 10





  • 一級欄目一 [id] 1
  • ├      └ 二級欄目一 [id] 3
  • ├      └   └ 三級欄目一 [id] 7
  • ├      └   └   └ 四級欄目二 [id] 8
  • ├      └ 二級欄目一 [id] 9
  • 一級欄目二 [id] 2
  • ├      └ 二級欄目二 [id] 5
  • ├      └   └ 三級欄目一 [id] 6
  • ├      └ 二級欄目二 [id] 11
  • 一級欄目一 [id] 4
  • 一級欄目一 [id] 10

 

歡迎大家討論更好的方法

php 系統樹形類,以樹形方式顯示

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.