간단한 원시 데이터를 전달함으로써 노드 간 N개의 관계를 도출하고, 트리 형태의 DOM을 출력할 수 있습니다 참고: git.oschina.net에서 최신 코드를 참조하세요("소스 코드 소스" 링크 클릭).
/** * 출력 무제한 분류, 제가 직접 작성했습니다~ * * @author binny_w@qq.com * @since 2013-09-24 AM */ /* 사용 예*/ /* $arrAll = array( array('id' => 1, 'name' => '열 카테고리_1', 'name_en' => 'cat_1', 'parent_id' => 0),
array(' id' => 2, '이름' => '열 분류_2', 'name_en' => 'cat_2', 'parent_id' => 0),
array('id' => 3, '이름' ' => '열 분류_3', 'name_en' => 'cat_3', 'parent_id' => 1),
array('id' => 4, 'name' => '열 분류_4 ', 'name_en' => 'cat_4', 'parent_id' => 1),
array('id' => 5, 'name' => '열 Category_5', 'name_en' => ; 'cat_5', 'parent_id' => 2),
array('id' => 6, 'name' => '열 카테고리_6', 'name_en' => 'cat_6', 'parent_id ' => 4),
array('id' => 7, 'name' => '열 분류_7', 'name_en' => 'cat_7', 'parent_id' => 6),
array('id' => 8, 'name' => '열 분류_8', 'name_en' => 'cat_8', 'parent_id' => 7),
array('id ' => 9, 'name' => '열 분류_9', 'name_en' => 'cat_9', 'parent_id' => ; 6)
);
$objT = new TreeList($ arrAll);
print_r($objT->arrAll);
print_r($objT->arrIdAll);
print_r($objT->arrIdChildren);
print_r($objT- >arrIdSon);
print_r($objT->arrIdLeaf);
print_r($objT->arrIdRelation);
print_r($objT->arrIdRelationSimple);
print_r($ objT->arrIdRoot);
print_r($objT->arrIdBackPath);
print($objT->getTable( ));
print($objT->getSelect('cat' , array(1, 8), true));
*/
// !definated('IN_FRAME') && die( '404 Page');
class TreeList {
/**
* 가능한 모든 데이터를 분석
*/
public $arrAll = array(); // 원본 데이터
public $arrIdRelation = array( ) // _ID를 키 이름으로 사용하는 다차원 관계
public $ arrIdRelationSimple = array(); // _ID를 키 이름으로 사용하여 다차원 관계 단순화, 트리 다이어그램 출력에 사용됨
public $arrIdAll = array() // _ID를 키로 사용하여 원본 데이터를 배열로 변환 name
public $arrIdSon = array(); // 모든 부모-자식 관계
public $arrIdLeaf = array(); // 리프 노드의 _ID
public $arrIdRoot = array(); of root node
public $arrIdChildren = array(); // 각 노드 아래의 자손의 _ID
public $ arrIdBackPath = array() // 각 노드는 루트로 돌아갑니다.
public $strItem = ' {$strSep}{$name}'; // 트리 구조 출력
/**
* 생성자, 원본 데이터 전달
*/
public function __construct($arrData) {
$this->arrAll = $arrData;
$this->processData();
}
/**
* 단순 트리
*/
공용 함수 getHtml() {
return $this->genHtml();
}
/**
* 테이블을 사용하여 나무를 그립니다
*/
public function getTable() {
$this->strItem = '{$strSep}{$name} {$name} {$name_en} ';
$strRe = '';
$strRe .= '구조 중국어 이름 영어 이름 ';
$strRe .= $this->genHtml( );
$strRe .= '
';
return $strRe;
}
/**
* 드롭다운 상자에
표시 * 예:
* $objTreeList->getSelect('parent_id', 0, false, 'class="span5"', array(0, 'EMA) 레벨 열로 ‚')))
*/
공개 함수 getSelect($ strName = 'tree', $arrValue = array(), $blmMulti = false, $strExt = '', $arrFirst = null) {
!is_array($arrValue) && $arrValue = array($arrValue);
foreach ($this->arrIdAll as $strTemp => $arrTemp) {
$ this->arrIdAll[$strTemp]['selected'] = '';
if (in_array($ arrTemp['id'], $arrValue)) {
$this->arrIdAll[$strTemp ]['selected'] = ' selected="선택됨"';
}
}
$this->strItem = '<옵션 값="{$id}"{$selected} title="{$name_en}">{$strSep}{$name}< /option>';
$strRe = ' $strRe .= ($blmMulti ? ' multiple="multiple"' : '') . (empty($strExt) ? '' : ' ' . $strExt) '>';
if (is_array($arrFirst) && count($arrFirst) == 2) {
$strRe .= '' .' ';
}
$strRe .= $this->getHtml() '';
return $strRe;
}
/* ----- 다음은 재귀, 반복 등 데이터 처리를 위한 매우 복잡한 프라이빗 함수입니다. ----- */
프라이빗 함수 helpForGetRelation($arrData) {
$arrRe = array();
foreach ($arrData as $strTemp => $arrTemp) {
$arrRe[$strTemp] = $arrTemp;
if (isset($this-> ; arrIdRelation[$strTemp])) {
$arrRe[$strTemp] = $this->arrIdRelation[$strTemp];
}
if (count($arrRe[$strTemp]) > 0 ) {
$arrRe[$strTemp] = $this->helpForGetRelation($arrRe[$strTemp]);
} else {
array_push($this->arrIdLeaf, $strTemp);
}
}
return $arrRe;
}
비공개 함수 helpForGetChildren($arrData) {
$arrRe = array_keys($arrData);
foreach( $ arrData as $arrTemp) {
$arrRe = array_merge($arrRe, $this->helpForGetChildren($arrTemp));
}
return $arrRe;
}
개인 함수 helpForGetBackPath($str) {
$arrRe = array();
$intTemp = $this->arrIdAll[$str]['parent_id'];
if ($intTemp > 0 ) {
$intTemp = '_' . $intTemp;
array_push($arrRe, $intTemp);
$arrRe = array_merge($arrRe, $this->helpForGetBackPath($intTemp)) ;
}
return $arrRe;
}
비공개 함수 processData() {
foreach ($this->arrAll as $arrTemp) {
$strTemp = ' _' . $arrTemp['id'];
$this->arrIdAll[$strTemp] = $arrTemp;
if ($arrTemp['parent_id'] > 0) {
$ strTemp_ = '_' . $arrTemp['parent_id'];
!isset($this->arrIdRelation[$strTemp_]) && $this->arrIdRelation[$strTemp_] = array();
$ this->arrIdRelation[$strTemp_][$strTemp] = array();
!isset($this->arrIdSon[$strTemp_]) && $this->arrIdSon[$strTemp_] = array( ) ;
array_push($this->arrIdSon[$strTemp_], $strTemp);
} else {
!isset($this->arrIdRelation[$strTemp]) && $this-> ; arrIdRelation[$strTemp] = array();
array_push($this->arrIdRoot, $strTemp);
}
}
$this->arrIdRelation = $this-> helpForGetRelation ($this->arrIdRelation);
$this->arrIdLeaf = array_unique($this->arrIdLeaf);
foreach ($this->arrIdRelation as $strTemp => $arrTemp) {
$this->arrIdChildren[$strTemp] = $this->helpForGetChildren($arrTemp);
in_array($strTemp, $this->arrIdRoot) && $this->arrIdRelationSimple[$ strTemp ] = $arrTemp;
}
$arrTemp = array_keys($this->arrIdAll);
foreach ($arrTemp as $strTemp) {
$this->arrIdBackPath[$strTemp ] = $this->helpForGetBackPath($strTemp);
}
}
비공개 함수 genSeparator($intLen) {
$strRe = '';
$i = 0 ;
while ($i < $intLen) { $strRe .= ' ' . (($i 1 == $intLen) ? '├' : '│'); $i ; } !empty($strRe) && $strRe .= '─'; return $strRe; } 비공개 함수 genHtml($arrRelation = null, $ intSep = 0) { $strRe = ''; null === $arrRelation && $arrRelation = $this->arrIdRelationSimple;
foreach ($arrRelation as $strKey => $arrTemp) {
if (count($this->arrIdAll[$strKey]) > 0) {
$strSep = $this->genSeparator($intSep);
extract($this-> ; arrIdAll[$strKey]);
eval('$strRe .= "' . $this->strItem . '";');
count($arrTemp) > $ this->genHtml($arrTemp, ($intSep 1));
}
}
return $strRe;
}
}
코드 복사